【发布时间】: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事件由更改它的其他代码触发...