Using Active.Match to do a double match?

chipsworld

Board Regular
Joined
May 23, 2019
Messages
169
Office Version
  1. 365
Hi all... Not sure if this is even a thing but...

I am trying to do what I can only describe as a VLOOKUP and a HLOOKUP and copy data to the appropriate cell from one workbook to another.

Basically, I want to open a window, select the import file, and copy the data to the root workbook based off of the column (VLOOKUP) and row (HLOOKUP) and then add the results to the quantity already there. like an x - y coordinate thing...

This is how far I have gotten thus far... I know it isn't very far, and it is returning an error message already, so any help would be great...

The source and destination workbooks/sheets are basically the same....all row titles are in column A and the date is in row 2 across top. this format can be changed to meet whatever I need it to be.



VBA Code:
Private Sub cmd_Import_Click()
Dim wbThisWB As Workbook
Dim wbImportWB As Workbook
Dim strFullPath As String
Dim i, m As Long

Set wbThisWB = ThisWorkbook

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Please select a file to import:"
.Filters.Add "Excel and CSV files", "*.csv; *.xls; *.xls*", 1
.Show
On Error Resume Next 'In case the user has clicked the <Cancel> button
strFullPath = .SelectedItems(1)
If Err.Number <> 0 Then
Exit Sub 'Error has occurred so quit
End If
On Error GoTo 0
End With

Set wbImportWB = Workbooks.Open(strFullPath)
    Application.ScreenUpdating = False



' copy data into master sheet
i = wbThisWB.Sheets("s1 daily metrics").Range("a1").Value
m = Application.Match(i, wbImportWB.Sheets("demob ind").Range("A:A"), 0) [B] 'currently getting error message at this point "Run Time Error 9 - Script out of range"[/B]
'If IsError(m) = True Then
'MsgBox "The Value Is Not Found"
'Exit Sub
'End If
wbImportWB.Activate
Sheets("demob ind").Select
Range("B" & m).Select
Selection.Copy
wbThisWB.Activate
Sheets("s1 daily metrics").Select
Range("E1").PasteSpecial xlPasteSpecialOperationAdd
Application.CutCopyMode = False

    'wbImportWB.Close False 'Close the Import WB without saving any changes. Uncomment when the code goes live.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Change m from Long to Variant.
 
Upvote 0
You shouldn't if you have declared m like
VBA Code:
Dim m As Variant
then you can use your error checking
VBA Code:
m = Application.Match(i, wbImportWB.Sheets("demob ind").Range("A:A"), 0)
If IsError(m) = True Then
MsgBox "The Value Is Not Found"
Exit Sub
End If
 
Upvote 0
Yes...that is exactly what I did.

VBA Code:
Dim wbThisWB    As Workbook
    Dim wbImportWB  As Workbook
    Dim strFullPath As String
    Dim i As Long
    Dim m As Variant

VBA Code:
m = Application.Match(i, wbImportWB.Sheets("demob ind").Range("A:A"), 0)
 
Upvote 0
I could understand if you still got a run time error 9 subscript out of range, but not a run time error 13 type mismatch.
If the value cannot be found then m would have a value of error 2042
 
Upvote 0
Thanks for that.
The error is on the previous line & is because A1 is text not a number, you will need to declare i as String
 
Upvote 0
OK...now I am getting that same "runtime error 9, subscript out of range" on the Application.match line...

I did notice that I gave you the wrong source file though... the data tab in the workbook you have should be named "DEMOB ind" or changed in the code to "DEMOB Units"

The end goal is to make it so that when you select the source file, you would select the tab to import, but just trying to get it to work hardcoded for now.

Still not sure how I would integrate the (HLOOKUP) into this so that I can put the data into the right column. This one is a little bigger than I have gone before, and I have not been able to find any examples to work from online...
 
Upvote 0
now I am getting that same "runtime error 9, subscript out of range"
That means that the tab name in the code & on the actual sheet are not the same. They need to match exactly.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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