Excel VBA - Find last used cell of a variable column number?

Alan_P

Well-known Member
Joined
Jul 8, 2014
Messages
596
Hi All,

Just wondering if anyone knows if there is a better way of finding the last used row when the column is variable?

So normally I would just use the below code to find the last row;

Code:
Range("[COLOR=#ff0000]A[/COLOR]" & Rows.Count).End(xlUp).Row


But in the code I've done I am finding the column via it's number (x) rather than letter and struggled to fit that into the above piece of code...

I've managed to get around this by finding the address and then using Mid to extract the column letter, but I can't help but think there must be a better way of doing this?

Code:
For i = 2 To 6
    RecipeNum = Sheets("Meals").Cells(3, i).Value
    
    For x = 1 To LastRecipe
               
        If RecipeNum = Sheets("Recipes").Cells(3, x).Value Then
        
            ColAddress = Sheets("Recipes").Cells(3, x).Address
            ColLetter = Mid(ColAddress, 2, 1)
        
            LastRecipeIngredient = Sheets("Recipes").Range(ColLetter & Rows.Count).End(xlUp).Row
                       
        End If
    Next
Next

Any suggestions would be appreciated,
Cheers,
Alan.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Use Cells instead of Range, like you already have done:

Code:
LastRecipeIngredient = Sheets("Recipes").Cells(Rows.Count, x).End(xlUp).Row
 
Upvote 0
Thanks Andrew!! So simple yet my brain just would not come to that conclusion!! :oops:
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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