error object variable block when match between two files and replace

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
174
Office Version
  1. 2019
Platform
  1. Windows
Hello
I no know if this code fits my project. I got this code from the internet . I want matching column B between two files . the file1 should match with file 2. if there is different the data in file 2 then should replace new data in file 1 the range is from A:E and the headers in row1
I got error object variable block in this line
VBA Code:
 m = Application.Match(rw.Cells(1).Value, wsDest.Columns("B"), 0)
VBA Code:
Sub Button2_Click()
    
    Dim OpenFileName As String
    Dim wb As Workbook
    Dim wsCopy As Worksheet
    Dim wsDest As Worksheet
    Dim m, rw As Range
    
    OpenFileName = Application.GetOpenFilename 'Select and Open workbook
    If OpenFileName = "False" Then Exit Sub
    
    Set wb = Workbooks.Open(OpenFileName, ReadOnly:=True)
    Set wsCopy = wb.Worksheets("Data") 'for example
    
    For Each rw In wsCopy.Range("A2:E" & wsCopy.Cells(Rows.Count, "A").End(xlUp).Row).Rows
        'matching row based on Id ?
        m = Application.Match(rw.Cells(1).Value, wsDest.Columns("B"), 0)
        'if we didn't get a match then we add a new row
        If IsError(m) Then m = wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row 'new row
        rw.Copy wsDest.Cells(m, "A") 'copy row
    Next rw
    
    wb.Close False 'no save
        
End Sub
any suggestion guy to do that
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
thanks for this notice . I modified but doesn't show any changes without any error
VBA Code:
Set wb = Workbooks.Open(OpenFileName, ReadOnly:=True)
    Set wsCopy = wb.Worksheets("Data") 
    Set wsDest = Worksheets("main")
 
Upvote 0
What workbook is Worksheets("main") in ?
If it is the same as copy then make the line
VBA Code:
Set wsDest = wb.Worksheets("main")

if not and it is in the same workbook as the code then where I have wb in the above use ThisWorkbook.

I modified but doesn't show any changes without any error
I can't tell from your comment if you are getting and error or not.
 
Upvote 0
if not and it is in the same workbook as the code then where I have wb in the above use ThisWorkbook.
the second file is existed in the same directory . I thought the second file can open by this line ,no need set inside the code
VBA Code:
    OpenFileName = Application.GetOpenFilename 'Select and Open workbook
 
Upvote 0
That line does not open the workbook, this line opens the workbook:
VBA Code:
Set wb = Workbooks.Open(OpenFileName, ReadOnly:=True)

Is the Sheet Data and the Sheet Main in the same workbook ?
If not what workbook is "main" in ?
 
Upvote 0
The code needs to know what workbook the sheet main is in.
Is it wb (same as sheet data) ?
Is it the workbook with the code in it (ThisWorkbook) ?
If not what is it because there are no other workbooks that are referenced in the code.
 
Upvote 0
Can you try changing this line:
VBA Code:
Set wsDest = Worksheets("main")

To this:
VBA Code:
Set wsDest = ThisWorkbook.Worksheets("main")
 
Upvote 0
Solution

Forum statistics

Threads
1,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top