I am using the attached code however when my database query does not return any data I dont want the Dim function to perform. can anyone help?
Code
Sub macro3()
Worksheets("Data 1").Range("I3:BU65536").ClearContents
'declare a variable to hold date
Dim strDate As String
strDate = Format(CDate(Range("startDate").Value), "yyyy-mm-dd") & " " & FormatDateTime(CDate(Range("startDate").Value), vbLongTime)
Sheets("Data 1").Select
Range("A1").Select
With Selection.QueryTable
.Connection = _
"ODBC;DSN=ewd;UID=gents;APP=Microsoft® Query;WSID=CUSXP02;DATABASE=EWD;Trusted_Connection=Yes"
.CommandText = Array( _
"SELECT EWD_Object.Department, EWD_Object.Doctype, EWD_Object.Gendate, EWD_Object.ArchiveEntryDate, EWD_Object.Docorigin, EWD_Object.AccountNo, EWD_Object.EnquiryNo" & Chr(13) & "" & Chr(10) & "FROM EWD.dbo.EWD_Object EWD_Object" & Chr(13) & "" _
, _
"" & Chr(10) & "WHERE (EWD_Object.Department In ('BBI','BBA')) AND (EWD_Object.ArchiveEntryDate={ts " & "'" & strDate & "'" & "}) AND (EWD_Object.Docorigin<>'Folder')" & Chr(13) & "" & Chr(10) & "ORDER BY EWD_Object.Doctype" _
)
.Refresh BackgroundQuery:=False
End With
Dim Last_Row As Long
Last_Row = Range("A65535").End(xlUp).Row
Range("I2:BU2").AutoFill Destination:=Range("I2:BU" & Last_Row), Type:=xlFillDefault
End Sub
Display More