我希望直接分配范围并隐藏正在填充值的工作表,而不是复制选择粘贴单元格(较低的代码)。
我认为该工作表可以简单地从视图中隐藏,并且基于另一个工作表的范围填充值的宏仍然可以工作,对吗?
试图在另一张纸上分配值,我打算在这个工作代码的基础上构建(感谢 Jason Faulkner 和 aoswald)。(我必须将单元格放在最后一组值的一个空白列之后。理想情况下,代码将从 A13:C## (直到最后填充的行)以及紧随其后的 E13:E## 分配值(即删除 D 列将值分配到隐藏工作表时。)
Private Sub CommandButton1_Click()
Dim DataRange As Variant, Constraint_sheet As Worksheet, Private_sheet As Worksheet
Set Constraint_sheet = Sheets("Constraint Sheet")
Set Private_sheet = Sheets("Private")
DataRange = Constraint_sheet.Range("A13:C300").Value
With Private_sheet
.Range(.Range("XFD1").End(xlToLeft).Offset(0, 3), .Range("XFD1").End(xlToLeft).Offset(287, 2)) = DataRange
End With
End Sub
这是我试图替换和简化的工作代码,如上所述。是否可以进行其他简化?
Private Sub CommandButton1_Click()
Dim MyPassword As String, Private_sheet As Worksheet
Set Private_sheet = Sheets("Private")
MyPassword = "string"
If InputBox("Please enter the password to continue.", "Enter Password") <> MyPassword Then
Exit Sub
End If
Private_sheet.Unprotect MyPassword ' apparently causes clipboard to be erased so do before copying cells
Columns("B:E").Select
Application.CutCopyMode = False
Selection.Copy
Private_sheet.Select
Private_sheet.Range("XFD1").End(xlToLeft).Offset(0, 3).Select
ActiveCell.PasteSpecial
ActiveCell.CurrentRegion.EntireColumn.Locked = True
ActiveCell.CurrentRegion.Offset(0, -1).EntireColumn.Locked = True
Private_sheet.Protect MyPassword
ActiveWorkbook.Save
End Sub
编辑:这是我为替换上述代码而开发的工作代码。可以进行哪些进一步的改进和简化?
Private Sub AddTemplate_Click()
Dim Exposed_sheet As Worksheet, Hidden_sheet As Worksheet, MyPassword As String
Set Exposed_sheet = Sheets("Exposed Sheet")
Set Hidden_sheet = Sheets("Hidden")
MyPassword = "string"
'Reference: carriage return in msgbox http://www.ozgrid.com/forum/showthread.php?t=41581
If InputBox("Please enter the password to continue." & vbNewLine & vbNewLine _
& "Note: The string you type will be exposed, i.e. not '***'." & vbNewLine _
& "Note: This will save the Excel file!", "Enter Password: Enter the correct string.") <> MyPassword Then
Exit Sub
End If
' Reference: .Protect - https://stackoverflow.com/questions/11746478/excel-macro-run-time-error-1004
Hidden_sheet.Unprotect MyPassword
'References:
' dynamic referencing: https://stackoverflow.com/questions/45889866/how-to-assign-values-from-one-sheet-into-hidden-sheet-using-excel-vba-and-skip/45889960#45889960
' adding text: https://stackoverflow.com/questions/20612415/adding-text-to-a-cell-in-excel-using-vba
' Union to exclude column: https://stackoverflow.com/questions/2376995/exclude-some-columns-while-copying-one-row-to-other
With Hidden_sheet
.Cells(1, Columns.Count).End(xlToLeft).Offset(1, 3).Resize(UBound(Exposed_sheet.Range("B6", "D9").Value, 1), UBound(Exposed_sheet.Range("B6", "D9").Value, 2)).Value = Exposed_sheet.Range("B6", "D9").Value
.Cells(1, Columns.Count).End(xlToLeft).Offset(1, 6).Value = "Volume/Protocol"
.Cells(1, Columns.Count).End(xlToLeft).Offset(6, 3).Resize(UBound(Union(Exposed_sheet.Range("A13:C300"), Exposed_sheet.Range("E13:E300")).Value, 1), UBound(Union(Exposed_sheet.Range("A13:C300"), Exposed_sheet.Range("E13:E300")).Value, 2)).Value = Union(Exposed_sheet.Range("A13:C300"), Exposed_sheet.Range("E13:E300")).Value
' If you change the order putting this prior, you must change the offsets or the cell they count from. -- DB, Aug 28 2017
.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 3).Resize(1, 3).Merge
.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 3).Value = Exposed_sheet.Range("A1").Value
End With
Hidden_sheet.Protect MyPassword
ActiveWorkbook.Save
End Sub