[vba] Run time error with the match function

lcblank

New Member
Joined
Sep 2, 2014
Messages
2
Hello everyone! I am new to vba and have given myself a project to start learning. I am getting a run time error trying to use the match function. The code I have so far is:

Sub refresh()
Dim n As Double
Dim i As Integer, k As Integer, x As Integer
Dim wb1 As Workbook, wb2 As Workbook
Dim exercise As String


Set wb1 = Workbooks("training_goals.xlsm")
Set wb2 = Application.Workbooks.Open("C:\Users\Logan\Downloads\strength.csv", False, True)


n = wb2.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row

'For k = 1 To 7

i = wb1.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row

exercise = wb1.Sheets(1).Cells(2, 1).Text

x = Application.WorksheetFunction.Match(exercise, wb2.Worksheets(1).Range("a1:f5000"), 0)

'Next k

MsgBox (exercise)


'value = Application.WorksheetFunction.Match(Application.Workbooks(file1).Worksheets(7).Cells(2, 1), _


MsgBox (x)
End Sub

At the moment it does not make much sense as I have just started and am mostly troubleshooting. As you can see, there are two workbooks. Workbook 2 has data in a1:fn, where n is defined as above. I am trying to use the exercise criteria from workbook 1 to find data from workbook 2. Originally my code was

x = Application.WorksheetFunction.Match(exercise, wb2.Worksheets(1).Range("a1:f" & n), 0)

I have confirmed that the string assigned to exercise is also found in workbook 2. I have also confirmed that the rest of the code works. If you guys can point me in the right direction it would be much appreciated!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
The Match function array argument should be confined to a single column or a single row. So, this:

Match(exercise, wb2.Worksheets(1).Range("a1:f5000"), 0)

will cause an error.
 
Upvote 0

Forum statistics

Threads
1,224,416
Messages
6,178,511
Members
452,853
Latest member
philipnjk64

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