Only copy visible range in VBA?

后端 未结 4 1877
情话喂你
情话喂你 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:20

    You cannot perform a direct value transfer without cycling though the areas of the SpecialCells(xlCellTypeVisible) collection.

    Sometimes it is easier to copy everything and get rid of what you don't want.

    Sub Importe()
        Dim lr As Long
    
        Worksheets("Sheet1").Copy after:=Worksheets("Sheet1")
        With ActiveSheet
            .Name = "xyz"
            .Cells(1, 1).CurrentRegion = .Cells(1, 1).CurrentRegion.Value2
            For lr = .Cells(.Rows.Count, "A").End(xlUp).Row To 1 Step -1
                If .Cells(lr, "A").EntireRow.Hidden Then
                    .Cells(lr, "A").EntireRow.Delete
                End If
            Next lr
            lr = .Cells(.Rows.Count, "A").End(xlUp).Row
            .Cells(1, 1).CurrentRegion.Resize(lr, 1) = .Cells(1, 1).CurrentRegion.Resize(lr, 1).Offset(0, 7).Value2
            .Cells(1, 1).CurrentRegion.Offset(0, 1).Resize(lr, 1) = .Cells(1, 1).CurrentRegion.Resize(lr, 1).Offset(0, 4).Value2
            .Columns("C:XFD").EntireColumn.Delete
        End With
    
    End Sub
    

提交回复
热议问题