vba use match function result as column number

akramer08

Active Member
Joined
May 2, 2012
Messages
265
I currently have a macro that loops through all each column on a sheet. Within that loop it will find the value of the column header and row header. It then will find that column header value in a table on a different sheet. The next point of the macro should look for the original row header value and return the row number, but I seem to be getting caught up in using the returned column number as a column reference number in the remainder of the code. Any ideas?

Code:
Sub Macro3()


Dim r As Long
Dim c As Long
Dim cols As Range
Dim rows As Range
Dim colmatch As Long
Dim rowcheck As Range
Dim rowmatch As Long
Dim rFind As Range


For c = 2 To 245 Step 1
    For r = 2 To 611 Step 1
        colhead = Cells(1, c).Value
        rowhead = Cells(r, 1).Value
        
        On Error Resume Next
        colmatch = WorksheetFunction.Match(colhead, Sheets("usethis").Range("A1:FDX1"), 0)
        Set rowcheck = Sheets("usethis").Range(Cells(2, colmatch), Cells(3821, colmatch))


        rowmatch = WorksheetFunction.Match(rowhead, rowcheck, 0)
        If rowmatch = 0 Then
            Cells(r, c).Value = "-"
        Else
            Cells(r, c).Value = Sheets("usethis").Cells(rowmatch, colmatch).Value
        End If


    Next r
Next c


End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
The first thing I would suggest doing is declaring colmatch and rowmatch as Variant rather than Long.

Then remove the On Error Resume Next and use Application.Match instead of WorksheetFunction.Match.

Now when a match isn't found you'll get error values in colmatch/rowmatch and you can add code to check for those.
 
Upvote 0
Made the changes you suggested. The issue seems to still be roughly the same. When setting the "rowcheck" it seems like it doesnt accept the values returned from the previous match function as a column number. I ran the code below. colmatch is returned as value 265, then the error is on the line containing "Set rowcheck" highlighted in red below and states that it equals nothing. If I hover over "colmatch" on that line it even shows 265.

Code:
Sub Macro3()


Dim r As Long
Dim c As Long
Dim cols As Range
Dim rows As Range
Dim colmatch As Variant
Dim rowcheck As Range
Dim rowmatch As Variant
Dim rFind As Range


For c = 2 To 245 Step 1
    For r = 2 To 611 Step 1
        colhead = Cells(1, c).Value
        rowhead = Cells(r, 1).Value
        
        colmatch = Application.WorksheetFunction.Match(colhead, Sheets("usethis").Range("A1:FDX1"), 0)
[COLOR=#ff0000]        Set rowcheck = Sheets("usethis").Range(Cells(2, colmatch), Cells(3821, colmatch))[/COLOR]


        rowmatch = WorksheetFunction.Match(rowhead, rowcheck, 0)
        If rowmatch = 0 Then
            Cells(r, c).Value = "-"
        Else
            Cells(r, c).Value = Sheets("usethis").Cells(rowmatch, colmatch).Value
        End If


    Next r
Next c


End Sub
 
Upvote 0
The problem could be that you don't have a worksheet reference for Cells here.
Code:
Set rowcheck = Sheets("usethis").Range(Cells(2, colmatch), Cells(3821, colmatch))
Try this.
Code:
With Sheets("usethis")
    Set rowcheck = .Range(.Cells(2, colmatch), .Cells(3821, colmatch))
End With


By the way, Match doesn't return 0 if there's no match, it returns an error.
 
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