I am trying to insert all rows from one excel doc called "Coding", which is 71 rows, into another excel doc named "Master", then deleting all of the matching old data (matching rows that are already in the master doc) using VBA. So far I have had success with the deleting duplicates portion, but I cannot get the data to import. This is my current code for the import:
Sub GetData()
Dim wbkS As Workbook
Dim wshS As Worksheet
Dim wshT As Worksheet
Dim strFile As String
With Application.FileDialog(1) ' msoFileDialogOpen
.Filters.Clear
.Filters.Add "Excel workbooks", "*.xls;*.xlsx;*.xlsb;*.xlsm"
.InitialFileName = "C:\documents\Master"
If .Show Then
strFile = .SelectedItems(1)
Else
MsgBox "No file selected", vbCritical
Exit Sub
End If
End With
With Sheets("Master")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
Application.ScreenUpdating = False
Set wshT = ActiveSheet
Set wbkS = Workbooks.Open(Filename:=strFile, ReadOnly:=True)
Set wshS = wbkS.Worksheets(1)
wshT.Range("A" & lastRow).Value = wshS.Range("Number").Value
wshT.Range("C" & lastRow).Value = wshS.Range("Injury").Value
wshT.Range("D" & lastRow).Value = wshS.Range("PA").Value
wshT.Range("E" & lastRow).Value = wshS.Range("PA2").Value
' Optional: close source workbook
wbkS.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
I am excluding column B from "Coding" intentionally. The doc "Master" already has 19913 rows, so I will need to insert the information into row 19914. I am getting the error:
Run-time error "1004":
Method "Range" of object "_Worksheet" failed
I am very new to VBA so it may just be a small error I'm not noticing. Any suggestions are welcome, thanks in advance!