Thanks for your quick reply. I've had a lot of time off and a lot of other projects going on so I am just getting back to this. I am having trouble with the loop. I didn't give you all of the code that I should have. It is telling me that there is a next without for, but as far as I can tell it should be working. Any suggestions?
Private Function GetData() As Boolean
On Error GoTo ERROR_TRAP
Dim sStartCSN As String
Dim sEndCSN As String
Dim dtStartDate As String
Dim dtEndDate As String
Dim sSQL As String
Dim ProcStateUserID As String
Dim ws As Worksheet
Dim LastCol As Integer
Dim c As Integer
'Loop for each sheet where sheet name ends with PROD For Each ws In Worksheets
If Right(ws.Name, 4) = "PROD" Then
'Determine last column filled with data on row 16
LastCol = ws.Cells(16, Columns.Count).End(xlToLeft).Column
If LastCol < 4 Then LastCol = 4
'Display Status
ws.Cells(3, 6) = "Getting Data"
GetData = False
'Start loop
For c = 4 To LastCol
'Create Query
sSQL = "SELECT CAST(CSN as varchar(25)) as CSN, CAST(VIN as varchar(25)) as VIN, ContID, ProcDefName, ProcStateStart, ProcStateEnd, ProcGrpName, ProcStateUserID"
sSQL = sSQL & " FROM vblReportIMProcStateWithContAttrib"
' Start CSN is in Row 16 on Run Worksheet
dtStartDate = ws.Cells(16, LastCol - 1)
' End CSN is in Row 16 on Run Worksheet
dtEndDate = ws.Cells(16, LastCol)
If dtStartDate <> #12:00:00 AM# Then
If dtEndDate <> #12:00:00 AM# Then 'apply both
sSQL = sSQL & " WHERE ProcStateEnd >= '" & dtStartDate & "' AND ProcStateEnd <= '" & dtEndDate & "'"
Else 'apply just start
sSQL = sSQL & " WHERE ProcStateEnd = '" & dtStartDate & "'"
End If
sSQL = sSQL & " AND ProcGrpName in ('FrontSuspension')"
sSQL = sSQL & " AND ProcStateComp = 'YES'"
sSQL = sSQL & " AND ProcStateTranType = 'Completed'"
sSQL = sSQL & " AND ProcDefName <> 'SMSSV'"
sSQL = sSQL & " AND ProcDefName <> 'SMRSV'"
sSQL = sSQL & " AND ProcDefName <> 'SMSSV2'"
sSQL = sSQL & " AND ProcDefName <> 'RSSSV'"
sSQL = sSQL & " ORDER BY CSN"
'Paste result to ws.Cells(10, LastCol)
Next c 'End loop
End If
Next
Set moRSWips = New Recordset
moRSWips.Open sSQL, moDBConn
Set moRSWips.ActiveConnection = Nothing
GetData = True
Else
MsgBox "No Start Date Entered", vbInformation, "GetData"
End If
Exit_Function:
Exit Function
ERROR_TRAP:
Select Case Err.Number
Case 0
Case Else
DisplayError msModName & "GetData", Err.Number, True, Err.Description
Err.Clear
Resume Exit_Function
End Select
End Function
Bookmarks