No cells were found - trying to copy a hidden row range

Forsberg

New Member
Joined
Apr 17, 2024
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
Hello,

I am fighting with No cells were found issue. I have a filter on A17 and data begins at the 18th row. There is no issue until I filter some data and rows go like this:

18
19
41
45

Then the macro will copy only the first 2 rows, while the rest will give an error. All sources I found didn't help me unfortunately.

I tried this:

VBA Code:
Set copyRange = srcWS.Range("A" & destLastRow & ":I" & (destLastRow + rowsToCopy - 1)).SpecialCells(xlCellTypeVisible)

I thought SpecialCells would solve this but it is no go. I used also On error resume next, but it just hides the error, it doesn't copy cells the way I want.

The whole idea is: I have a table with people name's. I choose people by just writing a name in the B4:B13 range. Then I paste data rows below A18 (filter on A17. I filter some data leaving those rows I want. Then some calculation is being done and macro should spread the visible rows across all people in the table (equally and without repeating the same data).


My whole sheet code looks like this:
VBA Code:
Sub CreateSheetsStyled()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet
    Dim destWS As Worksheet
    Dim cell As Range
    Dim copyRange As Range
    Dim pasteRange As Range
    Dim lastRow As Long
    Dim destLastRow As Long
    Dim colorIndex As Long 
    Dim rowsToCopy As Long

   Set srcWS = ThisWorkbook.Sheets("Cover_data")
    
    lastRow = srcWS.Cells(srcWS.Rows.Count, "A").End(xlUp).Row
    destLastRow = 18 
    
    Dim tabColors As Variant
    tabColors = Array(3, 4, 5, 6, 7, 8, 9, 10, 11, 12) 
    
    For Each cell In srcWS.Range("B4:B13") 'people names
        If cell.Value <> "" Then
            If WorksheetExists(cell.Value) Then
                Set destWS = ThisWorkbook.Sheets(cell.Value)
            Else
                Set destWS = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
                destWS.Name = cell.Value
            End If
            
            rowsToCopy = WorksheetFunction.RoundUp(srcWS.Range("G4").Value, 0) 'rows count per people (how many per single person)
            
            Set copyRange = srcWS.Range("A" & destLastRow & ":I" & (destLastRow + rowsToCopy - 1)).SpecialCells(xlCellTypeVisible)
            
            Set pasteRange = destWS.Range("A" & destWS.Cells(destWS.Rows.Count, "A").End(xlUp).Row + 1)
            
            copyRange.Copy pasteRange
            destLastRow = destLastRow + rowsToCopy 
            Application.CutCopyMode = False
            
            If colorIndex <= UBound(tabColors) Then
                destWS.Tab.colorIndex = tabColors(colorIndex)
                colorIndex = colorIndex + 1
            Else
                destWS.Tab.colorIndex = tabColors(UBound(tabColors))
            End If
        End If
    Next cell
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Not sure, but from other source I've read you need to copy the filtered range (using visible as you have done) then .PasteSpecial into the target range.
I'm basing that on the first line you posted not being the line that generates an error.
 
Upvote 0
I tried many approaches and all of them fail when they find a hidden row. I even removed all people and left only 1 so script wouldn't have to spread all cases across all members.
There is no error then, but it still copies only the 18 and 19 rows while leaving 41 and 45 uncopied. I used pastespecial.
 
Upvote 0

Forum statistics

Threads
1,215,239
Messages
6,123,817
Members
449,127
Latest member
Cyko

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