VBA to replace row numbers in a range with variables e.g Range("$A$RowFirst:$B$RowLast")

NotBillGates

New Member
Joined
Jul 1, 2016
Messages
13
Hi
I have 35 rows and 2 columns A and B
I want to filter by TopTen values in column B in rows 8 to 25

I have used the following to find the first and last row number (thanks to WBD)

Dim rowNumberFirst As Double
On Error Resume Next
Err.Clear
rowNumberFirst = WorksheetFunction.Match("FirstApples", ActiveSheet.Range("$A:$A"), 0)
If Err.Number <> 1004 Then
End If

This sets rowNumberFirst to 8

Dim rowNumberLast As Double
On Error Resume Next
Err.Clear
rowNumberFirst = WorksheetFunction.Match("LastApples", ActiveSheet.Range("$A:$A"), 0)
If Err.Number <> 1004 Then
End If

This sets rowNumberLast to 25

The following hard coding works
ActiveSheet.Range("$A$8:$B$25").AutoFilter Field:=2, Criteria1:="10", _
Operator:=xlTop10Items

but how do I replace the 8 with rowNumberFirst and 25 with rowNumberLast e.g.

ActiveSheet.Range("$A$rowNumberFirst:$B$2rowNumberLast).AutoFilter Field:=2, Criteria1:="10", _
Operator:=xlTop10Items

Any help greatly appreciated

Thanks
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this.
Code:
ActiveSheet.Range("$A$" & rowNumberFirst & ":$B$" & rowNumberLast).AutoFilter Field:=2, Criteria1:="10", _
Operator:=xlTop10Items
 
Upvote 0

Forum statistics

Threads
1,213,568
Messages
6,114,348
Members
448,570
Latest member
rik81h

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