1

I'm new to VBA and would like to learn fast :)

I have 6 Excel Sheets (months 1-6) with the same data (shown as table) but different row counts.

enter image description here

  1. I would like to create a table with the months (1-6) as rows and volumes and reserves of as columns

enter image description here

  1. Also, is there a way I can have write a code that gives me the top 3 customer groups in terms of reserves (column F) that also gives the corresponding volume (column E) -- like sumif() but in vba?

enter image description here

I've been researching for days and haven't found much...I'm happy with any help I can get! Thank you so much:)

1 Answer 1

1

This is how you use the adodb object. Just add 3 sheets and run it.

The data sheet serves as a temporary sheet to obtain top3.

enter image description here

Sub exeSQL(Ws As Worksheet, strSQL As String)

    Dim Rs As Object  'ADODB.Recordset
    Dim strConn As String
    Dim i As Integer

    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & ThisWorkbook.FullName & ";" & _
            "Extended Properties=Excel 12.0;"
    
    Set Rs = CreateObject("ADODB.Recordset") 'New ADODB.Recordset
    
    Rs.Open strSQL, strConn
    
    If Not Rs.EOF Then
         With Ws
            .Range("a2").CurrentRegion.ClearContents
            For i = 0 To Rs.Fields.Count - 1
               .Cells(1, i + 1).Value = Rs.Fields(i).Name
            Next
            .Range("a2").CopyFromRecordset Rs
            .Columns.AutoFit
        End With
    End If
    Rs.Close
    Set Rs = Nothing
End Sub
Sub Main()
    Call sumVolume
    Call sumTop3
End Sub
Sub sumVolume()
    Dim Ws As Worksheet
    Dim strSQL As String, strU As String
    Dim sName(1 To 6) As Variant
    Dim i As Integer
    
    Set Ws = Sheets("Resultsum")

    For i = 1 To 6
        sName(i) = Sheets(i).Name
    Next i
    For i = 1 To 5
        strU = strU & "select '" & sName(i) & "' as sday, [Volume], [Reserves] from [" & sName(i) & "$] union all "
    Next i
        strU = strU & "select '" & sName(6) & "' as sday, [Volume], [Reserves] from [" & sName(6) & "$] "
        
    strSQL = "SELECT sday, sum([Volume]) as [sum of volume], sum([Reserves]) as [sum of Reserves ] "
    strSQL = strSQL & " FROM (" & strU & " )"
    strSQL = strSQL & "WHERE not isnull([Volume]) "
    strSQL = strSQL & "GROUP BY sday "
        
    exeSQL Ws, strSQL
    
    
End Sub
Sub sumTop3()
    Dim Ws As Worksheet
    Dim strSQL As String, strU As String
    Dim i As Integer
    
    Call sumData
    
    Set Ws = Sheets("ResultTop3")


    strSQL = "SELECT *   "
    strSQL = strSQL & " FROM [Data$] as a "
    strSQL = strSQL & "WHERE [Reserves] in ( "
    strSQL = strSQL & "SELECT TOP 3 [Reserves] FROM [Data$] as b "
    strSQL = strSQL & "ORDER BY b.[Reserves] DESC ) "
    strSQL = strSQL & "ORDER BY a.[Reserves] DESC  "
    
        
    exeSQL Ws, strSQL
    
    
End Sub

Sub sumData()
    Dim Ws As Worksheet
    Dim strSQL As String, strU As String
    Dim sName(1 To 6) As Variant
    Dim i As Integer
    
    Set Ws = Sheets("Data")

    For i = 1 To 6
        sName(i) = Sheets(i).Name
    Next i
    For i = 1 To 5
        strU = strU & "select [Customer Group Nr] as [Customer Group], [Volume], [Reserves] from [" & sName(i) & "$] union all "
    Next i
        strU = strU & "select [Customer Group Nr] as [Customer Group], [Volume], [Reserves] from [" & sName(6) & "$] "
        

    strSQL = "SELECT [Customer Group], sum([Volume]) as Volume , sum([Reserves]) as Reserves  "
    strSQL = strSQL & " FROM (" & strU & " )"
    strSQL = strSQL & "WHERE not isnull([Volume]) "
    strSQL = strSQL & "GROUP BY [Customer Group]  "
        
    exeSQL Ws, strSQL
    
    
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much!! however, excel is giving me an error message for Rs.Open strSQL, strConn do I need to download something? Thanks:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.