我如何让我的macros运行单元格select?
我不是新手编程,但我是在Excel中使用macros的新手。 我正在使用Excel 2010,试图运行下面的macros:
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是一个下拉列表,我希望这个macros运行,以便单元显示/不显示基于在列表中select什么。 但是,如果我给它的参数(如上所示),我不能在UI中运行它,如果我不给它的参数,我只能手动运行它,这并没有多大帮助。
现在,当我从C9下拉列表中select一些东西时,什么都不会发生。 谁能帮我弄清楚为什么?
你的代码看起来已经成熟了Select Case
处理过程,并且还有几件事需要添加关于Worksheet_Change
事件macros(太多的评论),所以我继续写下Sub Worksheet_Change
的草稿。 我不知道如果我已经解释了所有的If ElseIf Else End If
但也许你可以看到我想要做什么。
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$C$9" Then Application.ScreenUpdating = False Application.EnableEvents = False On Error GoTo Whoa Rows("21:25").EntireRow.Hidden = False Rows("28:32").EntireRow.Hidden = False Rows("33:39").EntireRow.Hidden = True Select Case Target.Value Case "DRG" Rows("33").EntireRow.Hidden = False Case "MSA" Rows("34:35").EntireRow.Hidden = False Case "UCR" Rows("33").EntireRow.Hidden = False Rows("36:39").EntireRow.Hidden = False Case "DRG", "ICD-9", "NCCI_Edits", "UB04" Rows("21:25").EntireRow.Hidden = True Rows("28:32").EntireRow.Hidden = True Case "ICD-10", "NDC" Rows("22:25").EntireRow.Hidden = True Rows("29:32").EntireRow.Hidden = True Case Else 'do nothing End Select End If FallThrough: Application.EnableEvents = True Application.ScreenUpdating = True Exit Sub Whoa: MsgBox Err.Description Resume FallThrough End Sub
回复评论与任何问题,你已经抄录这个为自己的目的,我会尽力协助。