我正在尝试替换ctrl + c,因此它只会复制受保护工作表上的可见单元格。试图解决这个问题我偶然发现了这篇文章(vba excel copy only visible cells on key press ctrl+c)
以下代码(由Siddharth-Rout建议)有效但仅适用于未受保护的表格:
Private Sub Workbook_Open()
Application.OnKey "^c", "Copy"
End Sub
Sub Copy()
Dim rng As Range
On Error GoTo Whoa
If Not Selection Is Nothing Then
Set rng = Selection.Cells.SpecialCells(xlCellTypeVisible)
rng.Copy
End If
LetsContinue:
Exit Sub
Whoa:
MsgBox Err.Description, vbCritical, "Error Number : " & Err.Number
Resume LetsContinue
End Sub
我尝试了取消保护,复制,然后重新保护,但它删除了副本。我需要保护最终的表格。任何帮助将不胜感激。
答案 0 :(得分:5)
啊!从过去的爆炸:P
您需要在复制之前取消保护和保护。我也使用ActiveSheet
进行演示。如果需要,将其更改为相关表格。
这是你在尝试的吗?
Sub Copy()
Dim rng As Range
Dim MyPassword As String
'~~> Change password as applicable
MyPassword = "Sid"
On Error GoTo Whoa
If Not Selection Is Nothing Then
ActiveSheet.Unprotect MyPassword
Set rng = Selection.Cells.SpecialCells(xlCellTypeVisible)
ActiveSheet.Protect MyPassword
rng.Copy
End If
LetsContinue:
Exit Sub
Whoa:
MsgBox Err.Description, vbCritical, "Error Number : " & Err.Number
Resume LetsContinue
End Sub