AggieJess27
New Member
- Joined
- Jul 5, 2024
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
- MacOS
- Mobile
Im trying to build a vba code that will take the item name from Workbook1 and use that to lookup the column in Workbook2, then use that column to compare data between the two workbooks. I have most of the code working but im now getting the "Run-time error '1004': Application-defined or object-defined error" in the range lookup. Here is the code I am working with. Im sure its something simple but Im still relatively new to VBA.
Option Explicit
Public ITEM As String
Sub FindChg()
Dim File1 As Workbook, File2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Dim i As Long
Dim need As String
'specify path and file names'
Set File1 = Workbooks.Open("C:\Users\......
Set File2 = Workbooks.Open("C:\Users\......
'specify the name of the sheets to be compared'
Set sh1 = File1.Sheets("Sheet 1")
Set sh2 = File2.Sheets("Sheet 2")
need = sh1.Range("B7")
'find column in summary to compare with'
With sh2
With .Rows(9)
On Error Resume Next
ITEM = Split(.Find("Name", .Cells(.Cells.Count), xlValues, , xlByColumns, xlPrevious).Address, "$")(1)
On Error GoTo 0
End With
End With
End Sub
Sub test1()
MsgBox ITEM
End Sub
Sub CompareExcelFiles()
Dim File1 As Workbook, File2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Dim i As Long
Dim need As String
Dim ITEM As String
'specify path and file names'
Set File1 = Workbooks.Open("C:\Users\....
Set File2 = Workbooks.Open("C:\Users\....
'specify the name of the sheets to be compared'
Set sh1 = File1.Sheets("Sheet 1")
Set sh2 = File2.Sheets("Sheet 2")
need = sh1.Range("B7")
'loop through lines in both files'
For i = 1 To sh1.Range("D" & Rows.Count).End(xlUp).Row
'checks if the line in Workbook1 is same as in Workbook2'
If sh1.Range("D" & i).Value <> sh2.Range(ITEM & i).Value Then >>this is where the error is occurring
'Copy line from Workbook2 to Workbook1'
sh2.Range(ITEM & i).Copy sh1.Range("D" & i)
End If
Next
End Sub
Option Explicit
Public ITEM As String
Sub FindChg()
Dim File1 As Workbook, File2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Dim i As Long
Dim need As String
'specify path and file names'
Set File1 = Workbooks.Open("C:\Users\......
Set File2 = Workbooks.Open("C:\Users\......
'specify the name of the sheets to be compared'
Set sh1 = File1.Sheets("Sheet 1")
Set sh2 = File2.Sheets("Sheet 2")
need = sh1.Range("B7")
'find column in summary to compare with'
With sh2
With .Rows(9)
On Error Resume Next
ITEM = Split(.Find("Name", .Cells(.Cells.Count), xlValues, , xlByColumns, xlPrevious).Address, "$")(1)
On Error GoTo 0
End With
End With
End Sub
Sub test1()
MsgBox ITEM
End Sub
Sub CompareExcelFiles()
Dim File1 As Workbook, File2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Dim i As Long
Dim need As String
Dim ITEM As String
'specify path and file names'
Set File1 = Workbooks.Open("C:\Users\....
Set File2 = Workbooks.Open("C:\Users\....
'specify the name of the sheets to be compared'
Set sh1 = File1.Sheets("Sheet 1")
Set sh2 = File2.Sheets("Sheet 2")
need = sh1.Range("B7")
'loop through lines in both files'
For i = 1 To sh1.Range("D" & Rows.Count).End(xlUp).Row
'checks if the line in Workbook1 is same as in Workbook2'
If sh1.Range("D" & i).Value <> sh2.Range(ITEM & i).Value Then >>this is where the error is occurring
'Copy line from Workbook2 to Workbook1'
sh2.Range(ITEM & i).Copy sh1.Range("D" & i)
End If
Next
End Sub