Using vba to copy only visible rows

rammi125

New Member
Joined
Jun 3, 2015
Messages
22
I have a spreadsheet which each day will have a lot of hidden rows (different rows each day). I want to be able to copy the first ten row of the visible data only and paste it to another spreadsheet, when I currently try to do this with the code below the paste does the first 10 rows including the hidden rows...I thought SpecialCells(xlCellTypeVisible) would help only copy the visible rows, but I was wrong.


Rows("17:3000").Select
Selection.Sort Key1:=Range("v16"), Order1:=xlDescending, Header:=xlGuess _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Limit = 10
Idx = 1
Set myRange4 = Range("b17:b3000").SpecialCells(xlCellTypeVisible)
For Each myArea In myRange4.Areas
For Each rw In myArea.Rows
If Idx <= Limit Then
strFltrdRng4 = strFltrdRn4g & rw.Address & ","
Idx = Idx + 1
End If
Next
Next
strFltrdRng4 = Left(strFltrdRng, Len(strFltrdRng) - 1)
Set myFltrdRange4 = Range(strFltrdRng4)
myFltrdRange4.Copy
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try:
Code:
Sub copyVisRows()
    Application.ScreenUpdating = False
    ActiveSheet.Range("$A$1:$A$100").AutoFilter Field:=1, Criteria1:=Array("a", _
        "c", "e", "g", "h", "i", "j", "k", "l"), Operator:=xlFilterValues
    Dim x As Long
    Dim y As Long
    x = 1
    For y = 2 To Rows.Count
        If Sheets("Sheet1").Cells(y, 1).EntireRow.Hidden = False Then
            Sheets("Sheet1").Cells(y, 1).EntireRow.Copy Sheets("Sheet2").Cells(x, 1)
            x = x + 1
            If x = 11 Then Exit Sub
        End If
    Next y
    Application.ScreenUpdating = True
End Sub
You'll have to change the autofilter range and criteria1 array to match your filter criteria. This also assumes that you have headers in row 1.
 
Last edited:
Upvote 0
Maybe something like this:

Code:
Sub test()

Dim lngLastRow As Long
Dim x As Long
Dim rngMyRange As Range


lngLastRow = Range("A" & Rows.Count).End(xlUp).Row
x = 1 ' change to row you want to start with


On Error Resume Next
Do Until rngMyRange.Cells.Count = 10 Or x = lngLastRow + 1
On Error GoTo 0
    If Rows(x).Hidden = False Then
        If rngMyRange Is Nothing Then
            Set rngMyRange = Range("A" & x)
        Else
            Set rngMyRange = Union(rngMyRange, Range("A" & x))
        End If
    End If
    x = x + 1
Loop


rngMyRange.EntireRow.Copy Sheets(2).Range("A1")


End Sub

Dom
 
Upvote 0
hey dom, your code worked great in terms of only highlighting the rows that are visible, thank you. I only want to copy column B,F, and R. Do you know the best way to change the code to only copy those rows if they are part of the first 10 visible rows?
 
Upvote 0
hey dom, your code worked great in terms of only highlighting the rows that are visible, thank you. I only want to copy column B,F, and R. Do you know the best way to change the code to only copy those rows if they are part of the first 10 visible rows?
 
Upvote 0
Hi,

Maybe like this:

Code:
Sub test()

Dim lngLastRow As Long
Dim x As Long
Dim rngMyRange As Range




lngLastRow = Range("B" & Rows.Count).End(xlUp).Row
x = 1 ' change to row you want to start with




On Error Resume Next
Do Until rngMyRange.Cells.Count = 10 Or x = lngLastRow + 1
On Error GoTo 0
    If Rows(x).Hidden = False Then
        If rngMyRange Is Nothing Then
            Set rngMyRange = Range("B" & x)
        Else
            Set rngMyRange = Union(rngMyRange, Range("B" & x))
        End If
    End If
    x = x + 1
Loop




rngMyRange.Copy Sheets(2).Range("A1")
rngMyRange.Offset(0, 4).Copy Sheets(2).Range("B1")
rngMyRange.Offset(0, 16).Copy Sheets(2).Range("C1")


End Sub

Dom
 
Upvote 0

Forum statistics

Threads
1,215,949
Messages
6,127,880
Members
449,411
Latest member
AppellatePerson

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