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
'
'
 

patel45

Well-known Member
Joined
Jul 15, 2012
Messages
1,953
please upload a sample file for testing on a share site then paste the link here
 

Dryver14

Well-known Member
Joined
Mar 22, 2010
Messages
2,396
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.
 

daggobear

New Member
Joined
Mar 31, 2013
Messages
2
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.
 

Forum statistics

Threads
1,082,274
Messages
5,364,171
Members
400,785
Latest member
Mahar92

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top