Below is the code for my macro. The issue is my macro runs fine(executes query and puts data on the spreadsheet) but I get this error
RunTime Error '13' Type Mismatch
The macro executes the query and puts the data on the spreadsheet but I get the error with yellow highlight on this line
Strcode = Target.Value
Any suggestions?
Sub JobTaskHistory(ByVal Target As Range)
Dim sqlstring As String
Dim connstring As String
Dim Strcode As String
'Strcode = Trim(InputBox("Please enter a Job Number", "Job Task history"))
Strcode = Target.Cells(1, 1).Value
sqlstring = "select distinct m.JobNumber , cast(m.ExpectedDate as DATE) 'Ship Date' &_
" from ArchiveJobHeader m left join AuxiliaryInfoFile af (nolock) on af.jobnumber=m.jobnumber left join CostEntry ce (NOLOCK) on ce.sJobNumber = m.JobNumber left join CostCenterFile cc (nolock) ON cc.Code = ce.sDepartmentCode left join JobExtra j on j.JobNumber = m.JobNumber & _
" where m.JobNumber = '" & Trim(Strcode) & "'" & _
" order by 'Resulttime'"
connstring = "ODBC;DSN=Test;UID=Test;PWD=Test123"
Dim thisQT As QueryTable
Dim lastRow As Long, nextRow As Long
lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
nextRow = lastRow + 1
'Set thisQT = ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("a1", "a1000"))
Set thisQT = ActiveSheet.QueryTables.Add( _
Connection:=connstring, _
Destination:=Range("A" & nextRow))
thisQT.BackgroundQuery = False
thisQT.Sql = sqlstring
thisQT.Refresh
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Call JobTaskHistory(Target.Cells(1, 1).Value)
End Sub