我有一个 Excel 工作簿,其中包含两个工作表,分别名为“HVAC 工具”和“参考”。在“HVAC 工具”工作表上,我有 4 个下拉菜单,每个下拉菜单都有一个月份列表。需要发生的是,当其中一个下拉菜单上的月份更改时,其余下拉菜单也需要更改。因为它们每个都需要相隔 3 个月,所以我在“参考”工作表上有一个参考表。每个下拉菜单都使用表格的不同部分作为其来源。下拉列表 1 列表从一月开始,到十二月结束。下拉列表 2 列表从四月开始,到三月结束。下拉列表 3 列表从七月开始,到六月结束。下拉列表 4 从十月开始,到九月结束。
每个下拉菜单都是一个 ActiveX 表单控件。它们有一个链接单元格,用于输出它们的值。
我尝试做的是创建一个宏,在更改时会找到所选值的索引位置,然后使用该索引从其他下拉列表中选择正确的月份。每个月是 3 个月后。因此,如果在下拉列表 1 中选择了一月(索引位置 1),则下拉列表 2 将是四月(也是索引位置 1),依此类推。
我认为逻辑是合理的,但是当我尝试执行时遇到了错误。
有人能想到更好的方法来做到这一点或者我可以得到帮助来编写这个宏吗?
谢谢!
'''
Private Sub Q1_MONTH_Change()
Dim q1 As Integer 'will store the index position of the new selection
Dim q2 As Variant 'will store the correct month name of q2
Dim q3 As Variant 'will store the correct month name of q3
Dim q4 As Variant 'will store the correct month name of q4
Dim q1_months As Range 'q1 list of months stored here
Dim q1_value As Range 'q1 drop down linked to this cell
Dim q2_months As Range 'q2 list of months stored here
Dim q2_value As Range 'q2 drop down linked to this cell this is what needs to change
q1_months = Sheets("Labor Rate Import").Range("$F$5:$F$16") 'q1 list of months stored here
q1_value = Sheets("HVAC TOOL").Range("J4") 'q1 drop down linked to this cell
q2_months = Worksheets("Labor Rate Import").Range("Q2_MONTHS") 'q2 list of months stored here
q2_value = Worksheets("HVAC TOOL").Range("U4") 'q2 drop down linked to this cell
'find index value of new selection
q1 = Application.WorksheetFunction.Match(q1_value, q1_months, 0)
'use index value to choose correct month from q2 dropdown
q2 = Application.WorksheetFunction.Index(Worksheets("Labor Rate Import").Range("Q2_MONTHS"), q1, 1)
q2_value.Value = q2
End Sub
'''