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