如何让我的宏运行细胞选择?

时间:2014-11-06 17:37:59

标签: excel vba excel-vba

我不是编程新手,但我不熟悉在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

我已经看过几篇有关此内容的帖子和教程,但我无法理解为什么这样做不会奏效。 Cell C9是一个下拉列表,我想要运行这个宏,以便根据列表中选择的内容显示/不显示单元格。但是,如果我给它参数(如上所示),我无法在用户界面中运行它,如果我不给它参数,我只能手动运行它,这对我们没有帮助我很喜欢。

现在,当我从C9下拉列表中选择某些内容时,没有任何反应。任何人都可以帮我找出原因吗?

1 个答案:

答案 0 :(得分:2)

您的代码看起来已经成熟Select Case处理了,有关Worksheet_Change事件宏的一些事情要添加(评论太多了)所以我继续编写了一份{ {1}}。我不确定我是否解释了所有Sub Worksheet_Change,但也许你可以看到我试图用这个做什么。

If ElseIf Else End If

发回评论,如果您出于自己的目的而转录此问题,我会尝试提供帮助。