excel - Sub跳到第二个Sub,为什么
问题描述
我正在尝试这样做,以便当您单击组合框时,它会隐藏不必要的列,以便用户可以选择她/他的首选项,然后点击添加按钮将信息传输到打印页面(单独的工作表)。在添加隐藏列代码之前,我的一切工作正常,现在当我运行命令按钮时,它会从命令按钮代码跳转到列代码,我不知道为什么。如果有人可以帮助我,那就太好了。添加了注释以帮助解决编码内部的问题。这是代码:
Private Sub CommandButton1_Click()
Dim cnt As Integer
Dim ra As Range
Dim a As String
Dim y As Long
Dim Target As Range
'Variable list:
'titl = title of header,
'ra = cell address of found title name,
'aa = simplified cell address,
'rw = splic row address
'clm = splic column address,
'x = counting range,
'cnt = counted cell numbers,
'a = imput value 1,
'b = imput value 2,
'c = a and b joined
'sa = second title address
titl = Me.ComboBox4.Value
MsgBox (titl)
'this would be to go until another title
If titl = "Caulking" Then
e = "Frame system"
ElseIf titl = "Frame system" Then
e = "?*"
ElseIf titl = "Color" Then
e = "Caulking"
End If
'these to sets find the titles locations
Set ra = Worksheets("Print page").Cells.Find(What:=titl, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If ra Is Nothing Then
MsgBox ("Not found")
Else
aa = ra.Address
End If
rw = Range(aa).Row
clm = Range(aa).Column
x = aa + ":B44"
cnt = WorksheetFunction.CountIf(Worksheets("Print page").Range(x), "?*")
'setting values for Do Until loop below
tr = Worksheets("Print page").Cells(rw, clm).Value
'Reads cell values, maybe could change to a for loop to do larger quantities or set directly to
drop down menu
a = Cells(4, 1).Value
b = Cells(4, 2).Value
d = Cells(4, 3).Value
'Combines values
c = a & " " & b
If Worksheets("Print page").Cells(rw, clm).Value = tr Then
Do Until Worksheets("Print page").Cells(rw, clm).Value = ""
rw = rw + 1
Loop
End If
'*(this is where is starts jumping if I comment it out it jumps on number 2 i marked and then if I
'comment that one out it goes to 3, if I take these out it runs, but I can't write my
'information to the print page)*
Worksheets("Print page").Cells(rw, clm).Insert Shift:=xlDown
' writes it to correct location
If Worksheets("Print page").Cells(rw, clm).Value = "" Then
Worksheets("Print page").Cells(rw, clm).Value = c '*(2)*
Else: Worksheets("Print page").Cells(rw + cnt, clm).Value = c '*(3)*
End If
End Sub'''
(下面的代码用于隐藏依赖于组合框的列)
Private Sub ComboBox3_Change()
If ComboBox3 = "Frame system" Then
Cells(7, 1).EntireColumn.Hidden = False
Cells(7, 2).EntireColumn.Hidden = False
Cells(7, 3).EntireColumn.Hidden = True
Cells(7, 4).EntireColumn.Hidden = True
Cells(7, 5).EntireColumn.Hidden = True
Cells(7, 6).EntireColumn.Hidden = True
Cells(7, 7).EntireColumn.Hidden = True
ElseIf ComboBox3 = "Color" Then
Columns("A").EntireColumn.Hidden = True
Columns("B").EntireColumn.Hidden = True
Columns("C").EntireColumn.Hidden = False
Columns("D").EntireColumn.Hidden = True
Columns("E").EntireColumn.Hidden = True
Columns("F").EntireColumn.Hidden = True
Columns("G").EntireColumn.Hidden = True
ElseIf ComboBox3 = "Door" Then
Columns("A").EntireColumn.Hidden = True
Columns("B").EntireColumn.Hidden = True
Columns("C").EntireColumn.Hidden = True
Columns("D").EntireColumn.Hidden = False
Columns("E").EntireColumn.Hidden = False
Columns("F").EntireColumn.Hidden = False
Columns("G").EntireColumn.Hidden = False
Else
Columns("A").EntireColumn.Hidden = False
Columns("B").EntireColumn.Hidden = False
Columns("C").EntireColumn.Hidden = False
Columns("D").EntireColumn.Hidden = False
Columns("E").EntireColumn.Hidden = False
Columns("F").EntireColumn.Hidden = False
Columns("G").EntireColumn.Hidden = False
End If
'This macro scrolls to the top left of your spreadsheet (cell A1)
ActiveWindow.ScrollRow = 1 'the row you want to scroll to
ActiveWindow.ScrollColumn = 1 'the column you want to scroll to
End Sub
如果有帮助,这是我的 Excel 工作表的图片: 在此处输入图片描述
解决方案
我找到了答案。Private Sub ComboBox3_Change()
这是因为你们中的一些人出于我的目的提到的第二个代码上的更改过程,我将其更改为Private Sub ComboBox3_DropButtonClick()
并且它工作正常,但如果你不能这样做,这个解决方案如何防止 ComboBox Change 事件在源时被调用列表已更改可能是您的下一个最佳选择。
FYIApplication.EnableEvents = False
不适用于组合框,请参阅上面的链接了解更多信息。