根据工作表1中的单元格值隐藏工作表1和2中的行

时间:2017-01-27 02:17:56

标签: excel vba excel-vba

我需要一个VBA,它会根据Sheet(“Progression”)的值隐藏sheet1上的34:53行(称为“Progression”) sheet2(称为“Map”)。 ( “A8”)

Cell A8是一个显示澳大利亚城市的下拉菜单。如果我需要隐藏行,我已经安排在城市名称后面输入一个空格(例如“墨尔本”将需要隐藏的行,“墨尔本”将需要可见的行)。我目前有代码通过每个位置有和没有空格有点烦人...有没有办法说“如果A8有”*“然后隐藏行34:53(”进展“)<工作表(“地图”)上的强>和行34:53“

我当前的代码位于下方(位于工作表(“进展”))。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(False, False) = "A8" Then
    Select Case Target.Value
        Case "Queensland": Rows("34:53").Hidden = False
        Case "Queensland ": Rows("34:53").Hidden = True
        Case "Melbourne": Rows("34:53").Hidden = False
        Case "Melbourne ": Rows("34:53").Hidden = True
        Case "Ballarat": Rows("34:53").Hidden = False
        Case "Ballarat ": Rows("34:53").Hidden = True
        Case "Brisbane": Rows("34:53").Hidden = False
        Case "Brisbane ": Rows("34:53").Hidden = True
        Case "Canberra": Rows("34:53").Hidden = False
        Case "Canberra ": Rows("34:53").Hidden = True
        Case "North Sydney": Rows("34:53").Hidden = False
        Case "North Sydney ": Rows("34:53").Hidden = True
        Case "Strathfield": Rows("34:53").Hidden = False
        Case "Strathfield ": Rows("34:53").Hidden = True
    End Select
End If

End Sub

任何帮助将不胜感激:)

3 个答案:

答案 0 :(得分:2)

由于如果所选值中的最后一个字符有空格,则需要隐藏行,以下代码应该可以使用

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim trigger As Boolean
    If Target.Address(False, False) = "A8" Then
        trigger = False
        If Right(Target.Value, 1) = " " Then
            trigger = True
        End If
    End If

    ThisWorkbook.Worksheets("Progression").Rows("34:53").Hidden = trigger
    ThisWorkbook.Worksheets("Map").Rows("34:53").Hidden = trigger

End Sub

答案 1 :(得分:0)

也许是这样的:

在select语句中设置变量而不是隐藏行。

然后使用单独的语句设置变量的行可见性,并为其他工作表执行此操作。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim trigger As Boolean
    If Target.Address(False, False) = "A8" Then
    Select Case Target.Value
        Case "Queensland": trigger = False
        Case "Queensland ": trigger = True
        Case "Melbourne": trigger = False
        Case "Melbourne ": trigger = True
        Case "Ballarat": trigger = False
        Case "Ballarat ": trigger = True
        Case "Brisbane": trigger = False
        Case "Brisbane ": trigger = True
        Case "Canberra": trigger = False
        Case "Canberra ": trigger = True
        Case "North Sydney": trigger = False
        Case "North Sydney ": trigger = True
        Case "Strathfield": trigger = False
        Case "Strathfield ": trigger = True
    End Select
End If

ThisWorkbook.Worksheets("Progression").Rows("34:53").Hidden = trigger
ThisWorkbook.Worksheets("Map").Rows("34:53").Hidden = trigger

End Sub

答案 2 :(得分:0)

你可以这样:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$8" Then
        Rows("34:53").Hidden = Right(Target.Value, 1) = " " ' <--| "Progression" is your active sheet
        Worksheets("Map").Rows("34:53").Hidden = Right(Target.Value, 1) = " "
    End If
End Sub