在我的工作簿(Book1)中,我有一个CommandButton,它可以打开一个新的工作簿(Book2),其中包含几张工作表和带有代码的CommandButtons。 Book2中的CommandButtons将打开一个UserForm并将Shapes添加到Sheet中。在每个CommandButton中都有一个计数器。
现在我的问题是,如果我单击Sheet1中的CommandButton,在Sheet2中,计数器也会上升。
有人可以告诉我我只有在ActiveSheet计数器增加的情况下才能写我的VBA代码吗?
我在每个工作表中都对此进行了尝试。
Private Sub CommandButton1_Click()
Static cnt As Long
With ActiveWorkbook.ActiveSheet
cnt = cnt + 1
End With
Call FillFormWKA
End Sub
编辑:
在Book2的每张图纸上都有一个CommandButton,它是从Book1的CommandButton中添加的。 Book2中的每个CommandButton都会打开一个用户窗体。在此用户窗体上,我有一个CommandButton,用于保存添加内容并将Shapes添加到工作表中。现在我的问题是,如果我单击Sheet1上的CommandButton,计数器也会在Sheet2的CommandButton上上升。
整个VBA代码:
Book1使用命令按钮和代码打开新的工作簿:
Private Sub PM_Controlling_Click()
Dim relativePath As String
Dim nws As Worksheet
Dim a As Integer
Dim b As Integer
a = 1
b = 11
Workbooks.Add
relativeString = ThisWorkbook.Path & "\Test2"
ActiveWorkbook.SaveAs Filename:=relativeString & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
' Add New Sheet if more than one machine is in the Project
a = a + 1
b = b + 1
If Workbooks("Book1.xlsm").Worksheets(b).Visible = xlSheetVisible Then
With Workbooks(ActiveWorkbook.Name)
Set nws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
nws.Name = "Working Plan " & Workbooks("LPA_Projectevaluation_changing_for_PM_Toolbox.xlsm").Worksheets(b).Name
End With
End If
Call Add_CommandButton
End Sub
CommandButton Modul
Sub Add_CommandButton()
Dim btn As Object
Dim btn1 As Object
Dim Code As String
Dim Code2 As String
Dim a As Integer
Dim b As Integer
Static cnt As Long
cnt = cnt + 1
a = 1
b = 45
' Add First Button (Opens Userform)
Set btn = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=b, Top:=175, Width:=50, Height:=25)
ActiveSheet.OLEObjects(a).Object.Caption = "Add"
btn.Name = "Add_WKA_" & cnt
Code = "Private Sub Add_WKA_" & cnt & "_Click()" & vbCrLf
Code = Code & " With ActiveWorkbook.ActiveSheet" & vbCrLf
Code = Code & " Call Add_WKA_Sheet" & vbCrLf
Code = Code & " End With" & vbCrLf
Code = Code & "End Sub"
With ActiveWorkbook.VBProject.VBComponents(Worksheets(ActiveSheet.Name).CodeName).CodeModule
.insertlines .CountOfLines + 1, Code
End With
b = b + 60
a = a + 1
' Button to look at UserForm
Set btn1 = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=b, Top:=175, Width:=50, Height:=25)
ActiveSheet.OLEObjects(a).Object.Caption = "Watch"
btn1.Name = "Watch_WKA_" & cnt
Code = "Private Sub Watch_WKA_" & cnt & "_Click()" & vbCrLf
Code = Code & " Call Watch_WKA_Sheet" & vbCrLf
Code = Code & "End Sub"
With ActiveWorkbook.VBProject.VBComponents(Worksheets(ActiveSheet.Name).CodeName).CodeModule
.insertlines .CountOfLines + 1, Code
End With
用户窗体中的命令按钮:
Private Sub CommandButton1_Click()
Static cnt As Long
With ActiveWorkbook.ActiveSheet
cnt = cnt + 1
End With
Call FillFormWKA
If cnt = 1 Then
Call AddShape1
ElseIf cnt = 2 Then
Call AddShape2
End If
End Sub
答案 0 :(得分:1)
据我了解,您要附加数据的问题。您想要在先前数据后面输入那些。因为您是通过代码激活工作表的,所以不会选择任何范围。如果不是您的解决方案,请解释您的问题。
Private Sub CommandButton1_Click()
Static cnt As Long
cnt = Cells(Rows.Count, "a").End(xlUp).Row + 1
ActiveWorkbook.ActiveSheet.Range("A" & cnt).Select
Call FillFormWKA
结束子