[英]Paste onto visible cells in Excel
我想將一個普通的值列(未隱藏)粘貼到包含隱藏值的選擇中。 在執行此操作時,我希望excel忽略它們之間的單元格,就像它們已被刪除一樣。 例如:
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
測試表-所有行可見
測試表-僅可見行
。
結果-僅可見行
結果-所有行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.