Do not worry about the calculations. I will deal with this. I just need to know what is best to get from the first table above to the second table above to allow the calculations that I want. Thanks!
Sub TestIt() Dim x Set x = GetOrOpenAndGetExcel Dim rst As DAO.Recordset Dim sSql As String Dim q As String q = VBA.Chr(34) sSql = "SELECT " & _ "DCalcForQueries(" & q & "NaPct" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS NaPct ," & _ "DCalcForQueries(" & q & "Mean" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Mean ," & _ "DCalcForQueries(" & q & "Sd" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Sd ," & _ "DCalcForQueries(" & q & "Low" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Low ," & _ "DCalcForQueries(" & q & "Q1" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Q1 ," & _ "DCalcForQueries(" & q & "Median" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Median ," & _ "DCalcForQueries(" & q & "Q3" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Q3 ," & _ "DCalcForQueries(" & q & "High" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS High ," & _ "DCalcForQueries(" & q & "IQR" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS IQR ," & _ "DCalcForQueries(" & q & "Kurt" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Kurt ," & _ "DCalcForQueries(" & q & "Skew" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Skew ," & _ "DCalcForQueries(" & q & "Obs" & q & ", " & q & "tbl_DatedModel_2015_0702_0" & q & ", " & q & "Rk-IU Mkt Cap" & q & ", " & q & "[Rk-IU Mkt Cap] IS NOT NULL AND [GICS Sector] = 'Consumer Discretionary'" & q & ") AS Obs " & _ "FROM tbl_DatedModel_2015_0702_0;" Debug.Print sSql Set rst = CurrentDb.OpenRecordset(sSql, dbOpenSnapshot) rst.MoveFirst Debug.Print rst.RecordCount Debug.Print rst.Fields("NaPct") Debug.Print rst.Fields("Mean") Debug.Print rst.Fields("Sd") Debug.Print rst.Fields("Low") Debug.Print rst.Fields("Q1") Debug.Print rst.Fields("Median") Debug.Print rst.Fields("Q3") Debug.Print rst.Fields("High") Debug.Print rst.Fields("IQR") Debug.Print rst.Fields("Kurt") Debug.Print rst.Fields("Skew") Debug.Print rst.Fields("Obs") End Sub Public Function DCalcForQueries(sCalc As String, Optional sTbl As String = "", Optional sMainFld As String = "", Optional sWhereClause As String = "", Optional k As Double) As Variant Dim dblData() As Double Dim oxl As Object On Error Resume Next Set oxl = GetObject(, "Excel.Application") If Err.Number <> 0 Then MsgBox "Excel object must be openned by the calling sub of DCalcForQueries so it isn't opened over and over, which is very slow" GoTo cleanup End If Dim x As Integer Dim aV() As Variant Dim tmp Dim lObsCnt As Long Dim lNaCnt As Long Dim i As Long Dim vTmp As Variant Dim lTtl As Long Dim bDoCalc As Boolean aV = a2dvGetSubsetFromQuery(sTbl, sMainFld, sWhereClause, "Numeric") If aV(0, 0) = "Not Numeric" Then MsgBox "Data returned by query was not numeric. Press OK to Stop and debug." Stop End If If sCalc = "Percentile" Or sCalc = "Q1" Or sCalc = "Q2" Or sCalc = "Q3" Or sCalc = "Q4" Then DCalcForQueries = oxl.WorksheetFunction.Percentile_Exc(aV, k) ElseIf sCalc = "Median" Then DCalcForQueries = oxl.WorksheetFunction.Median(aV) ElseIf sCalc = "Kurt" Or sCalc = "Kurt" Then DCalcForQueries = oxl.WorksheetFunction.Kurt(aV) ElseIf sCalc = "Minimum" Or sCalc = "Low" Then DCalcForQueries = oxl.WorksheetFunction.Min(aV) ElseIf sCalc = "Maximum" Or sCalc = "High" Then DCalcForQueries = oxl.WorksheetFunction.Max(aV) ElseIf sCalc = "IQR" Then DCalcForQueries = oxl.WorksheetFunction.Quartile_Exc(aV, 3) - oxl.WorksheetFunction.Quartile_Exc(aV, 1) ElseIf sCalc = "Obs" Then lObsCnt = 0 For Each tmp In aV If Not IsNull(tmp) Then lObsCnt = lObsCnt + 1 End If Next DCalcForQueries = lObsCnt ElseIf sCalc = "%NA" Or sCalc = "PctNa" Or sCalc = "NaPct" Or sCalc = "%Null" Or sCalc = "PctNull" Then lNaCnt = 0 lTtl = UBound(aV, 2) + 1 For Each tmp In aV If IsNull(tmp) Then lNaCnt = lNaCnt + 1 End If Next DCalcForQueries = (lNaCnt / lTtl) * 100 ElseIf sCalc = "Skewness" Or sCalc = "Skew" Then DCalcForQueries = oxl.WorksheetFunction.Skew(aV) ElseIf sCalc = "StDev" Or sCalc = "Sd" Then DCalcForQueries = oxl.WorksheetFunction.StDev_S(aV) ElseIf sCalc = "Mean" Then DCalcForQueries = oxl.WorksheetFunction.Average(aV) Else MsgBox "sCalc parameter not recognized: " & sCalc End If cleanup: End Function Function a2dvGetSubsetFromQuery(sTbl As String, sMainFld As String, sWhereClause As String, sTest As String) As Variant() 'sTest can be "Numeric" or "None" ...will implement more as needed Dim iFieldType As Integer Dim rst As DAO.Recordset Dim db As Database Set db = CurrentDb Dim sMainFldFull As String Dim sSubSetFldFull As String Dim sSql As String sMainFldFull = "[" & sMainFld & "]" sSubSetFldFull = "" sSql = "" sSql = "SELECT " & sMainFldFull & " FROM " & sTbl If Len(sWhereClause) > 0 Then sSql = sSql & " WHERE " & sWhereClause End If Set rst = db.OpenRecordset(sSql, dbOpenSnapshot) 'make sure the data is the right type iFieldType = rst(sMainFld).Type If sTest = "Numeric" Then If iFieldType = dbByte Or _ iFieldType = dbInteger Or _ iFieldType = dbLong Or _ iFieldType = dbCurrency Or _ iFieldType = dbSingle Or _ iFieldType = dbDouble _ Then rst.MoveLast rst.MoveFirst a2dvGetSubsetFromQuery = rst.GetRows(rst.RecordCount) Else Dim aV(0 To 1, 0 To 1) As Variant aV(0, 0) = "Not Numeric" a2dvGetSubsetFromQuery = aV End If ElseIf sTest = "None" Then 'don't do any testing rst.MoveLast rst.MoveFirst a2dvGetSubsetFromQuery = rst.GetRows(rst.RecordCount) Else MsgBox "Test type (sTest) can only be 'None' or 'Numeric'. It was: " & sTest Stop End If cleanup: rst.Close Set rst = Nothing End Function Public Function GetOrOpenAndGetExcel() As Object 'if excel is open it will return the excel object 'if excel is not open it will open excel and return the excel object On Error GoTo 0 On Error Resume Next Set GetOrOpenAndGetExcel = GetObject(, "Excel.Application") If Err.Number <> 0 Then Set GetOrOpenAndGetExcel = CreateObject("Excel.Application") End If On Error GoTo 0 End Function