【发布时间】:2014-11-06 17:37:58
【问题描述】:
我对编程并不陌生,但我对在 Excel 中使用宏并不陌生。我正在使用 Excel 2010,尝试运行以下宏:
Sub HideUnhideCells(ByVal Target As Range)
Dim keyCell As Range
Set keyCell = Range("C9")
Dim Cells1 As Range
Dim Cells2 As Range
'Call the function on C9 cell change
If Target.Address = "$C$9" Then
'Make Data Source available for for DRG and UCR
If keyCell.Value = "DRG" Or keyCell.Value = "UCR" Then
Set Cells1 = Range("C33")
Cells1.EntireRow.Hidden = False
Else
Set Cells1 = Range("C33")
Cells1.EntireRow.Hidden = True
End If
'Make MSA special cells available if MSA is selected
If keyCell.Value = "MSA" Then
Set Cells1 = Range("B34:C35")
Cells1.EntireRow.Hidden = False
Else
Set Cells1 = Range("B34:C35")
Cells1.EntireRow.Hidden = True
End If
'Make UCR cells available if UCR is selected
If keyCell.Value = "UCR" Then
Set Cells1 = Range("B36:C39")
Cells1.EntireRow.Hidden = False
Else
Set Cells1 = Range("B36:C39")
Cells1.EntireRow.Hidden = True
End If
'Remove extra name cells for 1-file and 2-file values
If keyCell.Value = "DRG" Or keyCell.Value = "ICD-9" Or keyCell.Value = "NCCI_Edits" Or keyCell.Value = "UB04" Then
Set Cells1 = Range("B21:C25")
Set Cells2 = Range("B28:C32")
Cells1.EntireRow.Hidden = True
Cells2.EntireRow.Hidden = True
ElseIf keyCell.Value = "ICD-10" Or keyCell.Value = "NDC" Then
Set Cells1 = Range("B22:C25")
Set Cells2 = Range("B29:C32")
Cells1.EntireRow.Hidden = True
Cells2.EntireRow.Hidden = True
Else
Set Cells1 = Range("B21:C25")
Set Cells2 = Range("B28:C32")
Cells1.EntireRow.Hidden = False
Cells2.EntireRow.Hidden = False
End If
End If
End Sub
我看过一些帖子和教程讨论这个问题,但我不明白为什么这不起作用。单元格 C9 是一个下拉列表,我希望运行此宏,以便根据列表中选择的内容显示/不显示单元格。但是,如果我给它参数(如上所示)我无法在 UI 中运行它,如果我不给它参数我只能手动运行它,这对我没有多大帮助。
现在,当我从 C9 下拉列表中选择某些内容时,什么也没有发生。谁能帮我弄清楚为什么?
【问题讨论】:
-
将
Worksheet_Change事件与If not intersect(target,range("C9")) is nothing then一起使用 -
如前所述,使用
Worksheet_Change事件宏。这些进入工作表的代码表,而不是模块代码表。 (右键单击工作表的名称选项卡并为工作表代码表选择查看代码。 -
感谢 Siddharth Rout 和 Jeeped!不知道工作表的代码表,也不知道 Worksheet_Change 是一个特定事件。把两者放在一起,我们就赢了!你们当中有人可以把它作为答案,我可以将此问题标记为已回答吗?谢谢!