Only copy visible range in VBA?

后端 未结 4 1880
情话喂你
情话喂你 2021-01-15 12:52

I\'m running into an issue where I\'m unable to copy only visible cells to a new sheet. I\'m able to get the lastrow, but I get #N/A on every cell except the first for each

4条回答
  •  走了就别回头了
    2021-01-15 13:32

    just to throw in an alternative version:

    Sub Importe()
        Dim sht1Rng As Range, sht1VisibleRng As Range
    
        With Worksheets("Sheet1")
            Set sht1Rng = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
        End With
        Set sht1VisibleRng = sht1Rng.SpecialCells(xlCellTypeVisible)
    
        With Worksheets.Add
            .Range("A1").Resize(sht1Rng.Rows.Count).Value2 = sht1Rng.Offset(, 7).Value2
            .Range("B1").Resize(sht1Rng.Rows.Count).Value2 = sht1Rng.Offset(, 4).Value2
            .UsedRange.EntireRow.Hidden = True
            .Range(sht1VisibleRng.Address(False, False)).EntireRow.Hidden = False
        End With
    End Sub
    

    which may have the drawback of Address() maximum "capacity "

提交回复
热议问题