1
我已經使用一個按鈕(通過單擊)來創建一個新工作表並在其上插入一個按鈕,但是我想將代碼導入到新按鈕(這裏是MyPrecodedButton)。如何將代碼導入另一頁上的Activex控件按鈕?
Private Sub CommandButton1_Click()
Dim z As Integer
Dim wb As Workbook
Dim ws2 As Worksheet, wsnew As Worksheet
Set wb = ThisWorkbook
Set ws2 = wb.Sheets("Sheet2")
z = ws2.Cells(2, 1).Value
Set wsnew = Sheets.Add ' Declare your New Sheet in order to be able to work with after
wsnew.Name = "PIAF_Summary" & z
z = z + 1
With wsnew.Range("A1:G1")
.Merge
.Interior.ColorIndex = 23
.Value = "Project Name (To be reviewed by WMO)"
.Font.Color = vbWhite
.Font.Bold = True
.Font.Size = 13
End With
ws2.Cells(2, 1).Value = z
Dim Rngc As Range: Set Rngc = wsnew.Range("F35")
With ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False, Left:=Rngc.Left, Top:=Rngc.Top, Width:=205, Height:=20)
.Name = "MyPrecodedButton" ' change the name
End With
End sub
這裏是MyPrecodedButton
Public Sub MyPrecodedButton_Click()
MsgBox "Co-Cooo!"
End Sub
如果你想得很好,你不必注入代碼(讓你的代碼適用於任何情況)。 – PatricK
簡單的方法是通過創建模板工作表添加所需的所有控件,並根據需要將其格式化(合併,顏色等)並隱藏此工作表,並且每次需要添加新工作表時只需複製模板並給它一個新的名字,並取消隱藏它。 – Fadi