Select range in VBA, defined by cell value

jebenexcel

Board Regular
Joined
Mar 16, 2018
Messages
59
Hi.

Imagine you have a table that starts with the column A and ends with the column M.
The table has a variable amount of rows. Is it possible to select this variable range through VBA, using the COUNTA function?
Like, Range(A1: M(COUNTA(A:A)).select or something along these lines...
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi.

If you want to select the whole table you might just try :

Code:
Range("TableName").Select

If you want to do it your way :

Code:
Range("A1:M" & WorksheetFunction.CountA(Range("A:A"))).Select
 
Last edited:
Upvote 0
When it comes to VBA i tend to use lastrow as my go to for this

Code:
Dim lastrow As Integer
Dim r As Range

With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

Set r = Range("A1:A" & lastrow)
 
Upvote 0
Is it possible to refer to a count from a different sheet?
Range("A1:M" & WorksheetFunction.CountA(Range("othersheet!A:A"))).Select ?
 
Upvote 0
Yes, but with this syntax

Code:
Range("A1:M" & WorksheetFunction.CountA(Worksheets("Other Sheet").Range("A:A"))).Select
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,840
Messages
6,121,895
Members
449,058
Latest member
Guy Boot

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