我想将一个正常的值列(未隐藏)粘贴到包含隐藏值的选择中。在这样做时,我希望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的解决方案来实现这一目标吗?
答案 0 :(得分:0)
这是一种使用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
测试表 - 所有可见行
测试表 - 仅可见行
结果 - 仅可见行
结果 - 所有行