仅复制VBA中的可见范围?

时间:2018-02-14 13:24:36

标签: excel vba excel-vba

我遇到了一个问题,我无法将只有可见的单元格复制到新工作表中。我能够得到lastrow,但除了每列的第一个,我在每个单元格上得到#N / A.我想复制可见细胞。我还想在可能的情况下仅将信息放在可见行上?

请参阅下面的代码:

Sub Importe()
lastRow = Worksheets("Sheet1").Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row

Worksheets.Add

With ActiveSheet
  Range("A1:A" & lastRow).Value2 = _
  ActiveWorkbook.Worksheets("Sheet1").Range("H1:H" & lastRow).SpecialCells(xlCellTypeVisible).Value
  Range("B1:B" & lastRow).Value2 = _
  ActiveWorkbook.Worksheets("Sheet1").Range("E1:E" & lastRow).SpecialCells(xlCellTypeVisible).Value
End With

End Sub

4 个答案:

答案 0 :(得分:2)

.Value2 = .Value这样的东西对于可见类型的特殊单元格不起作用,因为......

...例如如果lastRow = 50并且有hiddenRows = 10则...

  • 您的来源Range("H1:H" & lastRow).SpecialCells(xlCellTypeVisible)
    lastRow - hiddenRows = 40
  • 但您的目的地Range("A1:A" & lastRow).Value2
    lastRow = 50行。

首先,您减去可见行,因此它们的大小不同。因此.Value2 = .Value不起作用,因为您只能用40个源行填充50行。

但您可以做的是CopySpecialPaste

Option Explicit

Sub Importe()
    Dim lastRow As Long

    lastRow = Worksheets("Sheet1").Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row

    Worksheets.Add

    With ActiveSheet
       ActiveWorkbook.Worksheets("Sheet1").Range("H1:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy
       .Range("A1").PasteSpecial xlPasteValues

       ActiveWorkbook.Worksheets("Sheet1").Range("E1:E" & lastRow).SpecialCells(xlCellTypeVisible).Copy
       .Range("B1").PasteSpecial xlPasteValues
    End With
End Sub

尽管如此,我仍建议您避免使用ActiveSheetActiveWorkbook并引用工作簿,例如ThisWorkbook。我的建议是:

Option Explicit

Sub Importe()
    Dim SourceWs As Worksheet
    Set SourceWs = ThisWorkbook.Worksheets("Sheet1")

    Dim DestinationWs As Worksheet
    Set DestinationWs = ThisWorkbook.Worksheets.Add

    Dim lastRow As Long
    lastRow = SourceWs.Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row

    SourceWs.Range("H1:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy
    DestinationWs.Range("A1").PasteSpecial xlPasteValues

    SourceWs.Range("E1:E" & lastRow).SpecialCells(xlCellTypeVisible).Copy
    DestinationWs.Range("B1").PasteSpecial xlPasteValues
End Sub

答案 1 :(得分:0)

要定义单元格是否可见,其列和行应该是可见的。这意味着,列和行的.Hidden属性应设置为False

以下是一些示例代码,说明如何仅复制两个工作表之间的可见范围。

想象一下,在Worksheets(1)中有这样的输入:

enter image description here

然后,您手动隐藏列B,并希望Worksheets(2)中的Range(A1:C4)中的每个单元格都进入B,而不需要列Sub TestMe() Dim myCell As Range For Each myCell In Worksheets(1).Range("A1:C4") If (Not Rows(myCell.Row).Hidden) And (Not Columns(myCell.Column).Hidden) Then Dim newCell As Range Set newCell = Worksheets(2).Cells(myCell.Row, myCell.Column) newCell.Value2 = myCell.Value2 End If Next myCell End Sub 中的单元格。像这样:

enter image description here

要执行此操作,您应检查范围中的每个单元格,无论其列或行是否可见。 一个可能的解决方案是:

Range("A1").Value2 = Range("A1").Value2

只是一般建议 - 每当您使用此类Value2之类的内容时,请确保两者都相同,而左侧不是.Value,右侧是 @Component({ selector: 'my-app', template: ` <kendo-grid [data]="gridData" [filter]="state.filter" filterable="menu" (dataStateChange)="dataStateChange($event)" > <kendo-grid-column field="ProductID" title="ID" width="40" [filterable]="false"> </kendo-grid-column> <kendo-grid-column field="ProductName" title="Product Name"> </kendo-grid-column> <kendo-grid-column field="FirstOrderedOn" title="First Ordered On" width="240" filter="date" format="{0:d}"> </kendo-grid-column> <kendo-grid-column field="UnitPrice" title="Unit Price" width="180" filter="numeric" format="{0:c}"> </kendo-grid-column> <kendo-grid-column field="Discontinued" width="120" filter="boolean"> <ng-template kendoGridCellTemplate let-dataItem> <input type="checkbox" [checked]="dataItem.Discontinued" disabled/> </ng-template> </kendo-grid-column> </kendo-grid> ` }) 。它可能不会带来你期待的东西。

答案 2 :(得分:0)

如果不通过SpecialCells(xlCellTypeVisible)集合的区域循环,则无法执行直接值传输。

有时候复制一切并摆脱你不想要的东西会更容易。

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

答案 3 :(得分:0)

只是投入另一个版本:

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

可能有Address()最大“容量”的缺点