Need some assistance. I have a vba that works on my work computer but it will not work on my home computer. Get a runtime error 9. Doing debugging everything worked until I got to Workbooks("40TH SIG LRR Master").Sheets("ZMPRPT").Range("A2").CopyFromRecordset rs. Please assist.
Code:
Sub GetData()
Dim cn As Object
Dim rs As Object
Dim sQuery As String
Dim sFile As String
Dim i As Long
Const adOpenStatic As Long = 3
Const adLockReadOnly As Long = 1
Const adCmdText As Long = 1
With Sheets("ZMPRPT")
.Range("Data").Clear
End With
Set cn = CreateObject("ADODB.Connection")
' change path as required
sFile = "C:\Users\anthony\Desktop\ZMPRPT.xlsx"
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & sFile & ";" & _
"Extended Properties=""Excel 12.0 XML;HDR=Yes"""
.Open
End With
sQuery = "SELECT * FROM [Sheet1$]"
Set rs = CreateObject("ADODB.Recordset")
rs.Open sQuery, cn, adOpenStatic, adLockReadOnly, adCmdText
With rs
If Not .EOF Then
' get headers
For i = 1 To .Fields.Count
Workbooks("40TH SIG LRR Master").Sheets("ZMPRPT").Cells(1, i) = .Fields(i - 1).Name
Next i
' Copy data
Workbooks("40TH SIG LRR Master").Sheets("ZMPRPT").Range("A2").CopyFromRecordset rs
End If
End With
rs.Close
cn.Close
End Sub