Match Function in VBA Returning same value

daggobear

New Member
Joined
Mar 31, 2013
Messages
2
Hello,

I am using Match in vba to select information from one workbook to another workbook. They are records with comments in three columns. I do not wish to copy the the entire row; just three selected columns for records in the old file can be found in the new file. The code below works only to a certain point.

For some reason, when I get to the last matched item, the Match function returns the same value to multiple other rows where the records in the new file are not found in the old file.

Sub ImporterYes()

' This Sub will import notes from the Report worksheet of older files
'
'
On Error Resume Next

' Declare workbook and worksheet
Dim new_wkb As Workbook, old_wkb As Workbook
Dim n_sheet As Worksheet, o_sheet As Worksheet

Dim nRow As Integer, gotcha As Integer, FinalRow As Integer


' Identify the current workbook as the base file to import data into
Set new_wkb = ActiveWorkbook
Set n_sheet = new_wkb.Sheets("Report")

FinalRow = Cells(Rows.Count, 2).End(xlUp).Row - 1



' Request user to select target file to extract data to import into the new inventory
Call openIt

' Connects variable with the current workbook as the target file to export data into new file
Set old_wkb = ActiveWorkbook
Set o_sheet = old_wkb.Sheets("Report")

'
' Stop screen updating to speed up calculations
Application.ScreenUpdating = False
'
'

'Start loop in cell with file numbers
nRow = 2


Do Until nRow = FinalRow

gotcha = Application.WorksheetFunction.Match(n_sheet.Cells(nRow, 2), o_sheet.Range("B:B"), 0)



n_sheet.Cells(nRow, 8).Value = o_sheet.Cells(gotcha, 8).Value

n_sheet.Cells(nRow, 9).Value = o_sheet.Cells(gotcha, 9).Value

n_sheet.Cells(nRow, 10).Value = o_sheet.Cells(gotcha, 10).Value



nRow = nRow + 1

Loop
'
'Turn the updating back on
Application.ScreenUpdating = True
'


' Close target file
' old_wkb.Close False
'
'
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
please upload a sample file for testing on a share site then paste the link here
 
Upvote 0
It's probably because FinalRow is measured in one worksheet and then being used in another.
I would say FinalRow = Cells(Rows.Count, 2).End(xlUp).Row - 1 to below the line
Set o_sheet = old_wkb.Sheets("Report")

FinalRow = 0_sheet.Cells(Rows.Count, 2).End(xlUp).Row - 1

Not sure why -1 either, probably a good reason.
 
Upvote 0
Thank you for your replies and help. I figured out the problem while traveling to work. I inserted: gotcha = 0 within the loop. I did follow the above advice and cleaned up FinalRow.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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