Hello, I have a VBA code I have been working on and I would like it to look at a row of information in sheet1 and plug in the identified fields into a query. I would like it to continue to loop through each row until there is no more criteria to be run. I also would like it to paste the output on sheet2 sheet. To my understanding, every time the code runs the query a row of data will be returned and it deletes the previous query results instead of adding the next query's data directly under the previous results. I am not sure how to make the code copy the previous data and paste the values to sheet2 while adding the next query's information to the next available row of the sheet. Any help is greatly appreciated.
Sub DairyQRY()
Dim Catergory As String
Worksheets("Sheet1").Activate
Category = Cells(2, "D").Value
If Category = "Bread" Then
Category = 1402
If Category = "Butter" Then
Category = 1403
If Category = "Creamers" Then
Category = 1405
If Category = "Yogurt" Then
Category = 1406
If Category = "Desserts" Then
Category = 1407
If Category = "Eggs/Pickles" Then
Category = "1408,1412,1413"
If Category = "Milk" Then
Category = "1410,1411"
If Category = CreamCheese Then
Category = 1415
If Category = Other Then
Category = "1409,1416,1495"
End If
End If
End If
End If
End If
End If
End If
End If
End If
'Query Assignment Strings
Dim DairyQRY As String
Sheet2.Select
Columns("A:F").Select
Selection.ClearContents
Range("A1").Select
'assign all items to IN clause in query
Sheet1.Select
Dim FacilityIDQRY As String
Dim cell1 As Range
FacilityIDQRY = Sheet1.Range("$A$2")
For Each cell1 In Range("OFFSET($A$3,0,0,COUNT($A:$A)-1,1)").Cells
FacilityIDQRY = FacilityIDQRY & "," & cell1.Value
Next
Sheet1.Select
Dim DateQRY As Long, i As Long, s As String
DateQRY = Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To DateQRY
s = s & Format(Range("B" & i).Value, "'yyyy-mm-dd'") & ", "
Next i
Range("C1").Value = Left(s, Len(s) - 2)
Sheet1.Select
Dim StartTimeQRY As String
Dim cell3 As Range
StartTimeQRY = Sheet1.Range("$E$2")
For Each cell1 In Range("OFFSET($E$3,0,0,COUNT($E:$E)-1,1)").Cells
StartTimeQRY = StartTimeQRY & "," & cell1.Value
Next
Sheet1.Select
Dim EndTimeQRY As String
Dim cell4 As Range
EndTimeQRY = Sheet1.Range("$F$2")
For Each cell1 In Range("OFFSET($F$3,0,0,COUNT($F:$F)-1,1)").Cells
EndTimeQRY = EndTimeQRY & "," & cell1.Value
Next
'build query in text format
DairyQRY = "(SELECT " & _
"STIL.FACILITY_ID, " & _
"STIL.SALES_TRANS_DT, " & _
"'02-Bread ' as Category, " & _
"'Afternoon' as TimePeriod, " & _
"( 'Between_' || Trim(Min(STIL.SALES_TRANS_TM)) || '_' || Trim(Max(STIL.SALES_TRANS_TM)) ) as TimeCriteria, " & _
"Sum(STIL.ITEM_QTY) as TotalItemQty " & _
"FROM " & _
"ProdDimV02C.SALES_TRANS_ITEM_LINE as STIL, " & _
"ITEM_DIM as ID " & _
"WHERE " & _
"STIL.ITEM_ID = ID.ITEM_ID " & _
"AND STIL.UPC_SALES_TRANS_TYPE_CD In ('0','2','8') " & _
"AND STIL.FACILITY_ID = " & FacilityIDQRY & " " & _
"AND STIL.SALES_TRANS_DT = " & Range("C1").Value & " " & _
"AND STIL.SALES_TRANS_TM Between " & StartTimeQRY & " and " & EndTimeQRY & " " & _
"AND ID.CATEGORY_ID = 1402 " & _
"GROUP BY " & _
"1,2,3,4) "
Sheet2.Select
'connect to ODBC and run query
Sub DairyQRY()
Dim Catergory As String
Worksheets("Sheet1").Activate
Category = Cells(2, "D").Value
If Category = "Bread" Then
Category = 1402
If Category = "Butter" Then
Category = 1403
If Category = "Creamers" Then
Category = 1405
If Category = "Yogurt" Then
Category = 1406
If Category = "Desserts" Then
Category = 1407
If Category = "Eggs/Pickles" Then
Category = "1408,1412,1413"
If Category = "Milk" Then
Category = "1410,1411"
If Category = CreamCheese Then
Category = 1415
If Category = Other Then
Category = "1409,1416,1495"
End If
End If
End If
End If
End If
End If
End If
End If
End If
'Query Assignment Strings
Dim DairyQRY As String
Sheet2.Select
Columns("A:F").Select
Selection.ClearContents
Range("A1").Select
'assign all items to IN clause in query
Sheet1.Select
Dim FacilityIDQRY As String
Dim cell1 As Range
FacilityIDQRY = Sheet1.Range("$A$2")
For Each cell1 In Range("OFFSET($A$3,0,0,COUNT($A:$A)-1,1)").Cells
FacilityIDQRY = FacilityIDQRY & "," & cell1.Value
Next
Sheet1.Select
Dim DateQRY As Long, i As Long, s As String
DateQRY = Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To DateQRY
s = s & Format(Range("B" & i).Value, "'yyyy-mm-dd'") & ", "
Next i
Range("C1").Value = Left(s, Len(s) - 2)
Sheet1.Select
Dim StartTimeQRY As String
Dim cell3 As Range
StartTimeQRY = Sheet1.Range("$E$2")
For Each cell1 In Range("OFFSET($E$3,0,0,COUNT($E:$E)-1,1)").Cells
StartTimeQRY = StartTimeQRY & "," & cell1.Value
Next
Sheet1.Select
Dim EndTimeQRY As String
Dim cell4 As Range
EndTimeQRY = Sheet1.Range("$F$2")
For Each cell1 In Range("OFFSET($F$3,0,0,COUNT($F:$F)-1,1)").Cells
EndTimeQRY = EndTimeQRY & "," & cell1.Value
Next
'build query in text format
DairyQRY = "(SELECT " & _
"STIL.FACILITY_ID, " & _
"STIL.SALES_TRANS_DT, " & _
"'02-Bread ' as Category, " & _
"'Afternoon' as TimePeriod, " & _
"( 'Between_' || Trim(Min(STIL.SALES_TRANS_TM)) || '_' || Trim(Max(STIL.SALES_TRANS_TM)) ) as TimeCriteria, " & _
"Sum(STIL.ITEM_QTY) as TotalItemQty " & _
"FROM " & _
"ProdDimV02C.SALES_TRANS_ITEM_LINE as STIL, " & _
"ITEM_DIM as ID " & _
"WHERE " & _
"STIL.ITEM_ID = ID.ITEM_ID " & _
"AND STIL.UPC_SALES_TRANS_TYPE_CD In ('0','2','8') " & _
"AND STIL.FACILITY_ID = " & FacilityIDQRY & " " & _
"AND STIL.SALES_TRANS_DT = " & Range("C1").Value & " " & _
"AND STIL.SALES_TRANS_TM Between " & StartTimeQRY & " and " & EndTimeQRY & " " & _
"AND ID.CATEGORY_ID = 1402 " & _
"GROUP BY " & _
"1,2,3,4) "
Sheet2.Select
'connect to ODBC and run query