簡體   English   中英

粘貼到Excel中的可見單元格上

[英]Paste onto visible cells in Excel

我想將一個普通的值列(未隱藏)粘貼到包含隱藏值的選擇中。 在執行此操作時,我希望excel忽略它們之間的單元格,就像它們已被刪除一樣。 例如:

1個

Cells A1-A3 contain 1,2,3. Cells C3-C5 contain A,B,C

在此處輸入圖片說明

We hide row 2, so that the '2' is no longer visible.

在此處輸入圖片說明

A normal paste will result in the 'B' overwriting the hidden '2'.

在此處輸入圖片說明

This is what the solution would look like, meaning the '2' is unaffected.

目前,我知道Alt+; 僅選擇可見單元格的功能,但是當嘗試在此處粘貼時,excel抱怨粘貼區域。

誰能想到具有excel基本功能,公式或VBA的解決方案來實現這一目標?

這是使用VBA的一種方法

它使用數組,因此不會復制單元格格式


Option Explicit

Public Sub CopyVisiblesOnlyAndPasteOverVisiblesOnly()
    Const FROM_COL = 3
    Const TO_COL = 1
    Dim arr As Variant, vis As Variant, ub As Long, i As Long, j As Long, itm As Variant

    With ThisWorkbook.Worksheets("Sheet1")
        arr = .UsedRange.Columns(FROM_COL)
        ub = UBound(arr)
        ReDim vis(1 To ub)
        j = 1
        For i = 1 To ub     'Copy visible cells in column 3
            If Not IsError(arr(i, 1)) Then  'skip errors, empty, and hidden rows to copy
                If Len(arr(i, 1)) > 0 And .Rows(i).Hidden = False Then
                    vis(j) = arr(i, 1)
                    j = j + 1
                End If
            End If
        Next
        ub = j - 1
        ReDim Preserve vis(1 To ub)

        If ub > 0 Then
            j = 1
            For i = 1 To ub
                While .Rows(j).Hidden = True    'skip hidden rows to paste over
                    j = j + 1
                Wend
                .Cells(j, TO_COL) = vis(i)
                j = j + 1
            Next
        End If
    End With
End Sub

測試表-所有行可見

TestSheetAllRows

測試表-僅可見行

TestSheetVisibleRows

結果-僅可見行

結果表可見行

結果-所有行

結果表全部行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM