2017-08-25 58 views
0

而不是複製粘貼單元格(較低代碼),我希望直接指定範圍並隱藏要填充值的表單。如何使用Excel VBA將值從一張表分配到隱藏表中? (並跳過範圍內的列?)

我認爲工作表可以簡單地從視圖中隱藏起來,而基於另一個工作表範圍填充值的宏仍然可以工作,對嗎?

試圖在另一張工作表上分配數值,我打算在此工作代碼上生成(使用thanks to Jason Faulkner and 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 

回答

0

你的問題是,(在普通的代碼模塊)Range()Cells()總會引用ActiveSheet,除非你有一個工作表預選賽

Private_sheet.Range(Range("XFD1").End(xlToLeft).Offset(0, 3), _ 
        Range("XFD1").End(xlToLeft).Offset(287, 2)) = DataRange 

因此,即使外Range()的作用域Private_sheet,即不會「通過」到呼叫的內部Range

應該是這樣的:

With Private_sheet 
    .Range(.Range("XFD1").End(xlToLeft).Offset(0, 3), _ 
      .Range("XFD1").End(xlToLeft).Offset(287, 2)) = DataRange 
End With 

有點簡單/更靈活:

​​

在表代碼模塊,範圍引用將默認的是表,但它仍然有資格好的做法與表單對象(例如)Me.Range()

+0

什麼是活躍的社區和快速響應!你在發佈這個答案之前,我可以編輯我的OP來實現這個相同的解決方案,我發現在相關的問題。我仍然希望對此代碼進行其他改進。 (一旦我有更完整的實現來簡化後面的代碼,我會對OP進行簡化。) – DBinJP

+0

當我嘗試'更簡單/更靈活'的代碼時,出現'運行時錯誤424:對象需要'的錯誤。我想知道是不是因爲DataRange是使用'with'爲不同的工作表定義的,並且在不同的'with'(即'with'指定不同的表單)中使用該連接時會失去連接。但是,當我指定表單sheetname.DataRange時,它表示'未找到方法或數據成員'。 – DBinJP

+0

是的,我搞砸了,忘記'DataRange'是一個數組而不是範圍。見上面的編輯。 –

相關問題