Macro to select a specific number of rows (below the header row) based on value in another cell

lifesgood

New Member
Joined
Feb 22, 2018
Messages
5
I'd like to select a specific number of cells in Sheet3 based on the count available in cell Z1 in Sheet2. The below code is somewhat of help, but not completely. The data in Sheet3 is a filtered range and I'd like to select only the visible cells (based on the count provided in Z1) below the header column in Sheet 3. Thanks in advance!
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sheets("Sheet3").Range("A1:X" & Sheets("Sheet2").Range("Z1").Value).Select
</code>
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
There might be a more elegant way, but this seems to work.

Code:
    Dim z As Long, i As Long, cell As Range
    
    z = Sheets("Sheet2").Range("Z1").Value
    With Sheets("Sheet3")
        With .Range("A2", .Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
            For Each cell In .Cells
                i = i + 1
                If i = z Or i = .Count Then Exit For
            Next cell
        End With
        .Range("A2:X" & cell.Row).Select
    End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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