【问题标题】:Sub jumping to second Sub, whySub跳到第二个Sub,为什么
【发布时间】:2020-02-25 20:20:23
【问题描述】:

我正在尝试这样做,以便当您单击组合框时,它会隐藏不必要的列,以便用户可以选择她/他的首选项,然后点击添加按钮将信息传输到打印页面(单独的工作表)。在添加隐藏列代码之前,我的一切工作正常,现在当我运行命令按钮时,它会从命令按钮代码跳转到列代码,我不知道为什么。如果有人可以帮助我,那就太好了。添加了注释以帮助解决编码内部的问题。代码如下:

    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 表的图片: enter image description here

【问题讨论】:

  • 我不明白“它跳到 2 号”是什么意思。如果你评论了什么?你用 F8 单步调试过代码吗?
  • Worksheets("Print page").Cells(rw, clm).Value = c 正在为某个工作表赋值。 “打印页面”表中是否包含Worksheet_Change 程序?如果是这样,那就是执行“跳跃”的地方?如果您不希望它运行该工作表的 Change 处理程序,请在更改工作表单元格值之前创建 Application.EnableEvents = False - 完成后不要忘记将其切换回 True
  • 您的ComboBox3 项目的来源是什么?
  • 还是跳转到ComboBox3_Change 处理程序?该组合框是否链接到您正在修改的单元格值?如果是这样,为什么没有预期的跳跃?
  • change 事件由更改它的其他代码触发...

标签: excel vba combobox


【解决方案1】:

我找到了答案。 这是因为第二个代码上的更改过程,你们中的一些人出于我的目的提到了Private Sub ComboBox3_Change(),我将其更改为Private Sub ComboBox3_DropButtonClick(),它工作正常,但如果你不能这样做,这个解决方案How to prevent ComboBox Change event from being called when source list is changed 可能是您的下一个最佳选择。

仅供参考Application.EnableEvents = False 不适用于组合框,请参阅上面的链接了解更多信息。

【讨论】:

  • 好的,你现在能不能喘口气,冷静下来,对于你的下一个问题,试着把信息放在问题中,而不是一连串的 cmets。请先浏览一下网站并了解它是如何工作的,然后再发布另一个像这样的 cmets 胡子的问题。
猜你喜欢
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 2020-01-19
  • 2023-04-06
  • 2021-10-17
  • 2017-09-02
  • 2021-09-05
相关资源
最近更新 更多