您能告诉我如何在工作表中同时包含两个代码吗?它们可以单独很好地工作。
我尝试命名为Macro1和Macro2,但是它不起作用。我可能做得不好。
'Code 1
Private Sub Worksheet_Change(ByVal Target As Range)
If [B28] = "Singapore" Then
Sheets("Singapore (2017)").Visible = True
Else
Sheets("Singapore (2017)").Visible = False
End If
If [B28] = "HongKong" Then
Sheets("Hong Kong (2017)").Visible = True
Else
Sheets("Hong Kong (2017)").Visible = False
End If
If [B28] = "Australia" Then
Sheets("Australia (2017)").Visible = True
Else
Sheets("Australia (2017)").Visible = False
End If
End Sub
'Code 2
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Row = 30 Then
If Target.Value <> "" Then
Application.Rows("32:33").Select
Application.Selection.EntireRow.Hidden = False
ElseIf Target.Value = "" Then
Application.Rows("32:36").Select
Application.Selection.EntireRow.Hidden = True
End If
End If
If Target.Column = 2 And Target.Row = 32 Then
If Target.Value <> "" Then
Application.Rows("33:34").Select
Application.Selection.EntireRow.Hidden = False
ElseIf Target.Value = "" Then
Application.Rows("33:36").Select
Application.Selection.EntireRow.Hidden = True
End If
End If
If Target.Column = 2 And Target.Row = 34 Then
If Target.Value <> "" Then
Application.Rows("35:36").Select
Application.Selection.EntireRow.Hidden = False
ElseIf Target.Value = "" Then
Application.Rows("35:36").Select
Application.Selection.EntireRow.Hidden = True
End If
End If
End Sub
无论先选择哪个代码,这两个代码都可以工作吗?
答案 0 :(得分:0)
您可以将两个if都包含在一个子框中:
'Code 1
Private Sub Worksheet_Change(ByVal Target As Range)
If [B28] = "Singapore" Then
Sheets("Singapore (2017)").Visible = True
Else
Sheets("Singapore (2017)").Visible = False
End If
If [B28] = "HongKong" Then
Sheets("Hong Kong (2017)").Visible = True
Else
Sheets("Hong Kong (2017)").Visible = False
End If
If [B28] = "Australia" Then
Sheets("Australia (2017)").Visible = True
Else
Sheets("Australia (2017)").Visible = False
End If
'Code 2
If Target.Column = 2 And Target.Row = 30 Then
If Target.Value <> "" Then
Application.Rows("32:33").Select
Application.Selection.EntireRow.Hidden = False
ElseIf Target.Value = "" Then
Application.Rows("32:36").Select
Application.Selection.EntireRow.Hidden = True
End If
End If
If Target.Column = 2 And Target.Row = 32 Then
If Target.Value <> "" Then
Application.Rows("33:34").Select
Application.Selection.EntireRow.Hidden = False
ElseIf Target.Value = "" Then
Application.Rows("33:36").Select
Application.Selection.EntireRow.Hidden = True
End If
End If
If Target.Column = 2 And Target.Row = 34 Then
If Target.Value <> "" Then
Application.Rows("35:36").Select
Application.Selection.EntireRow.Hidden = False
ElseIf Target.Value = "" Then
Application.Rows("35:36").Select
Application.Selection.EntireRow.Hidden = True
End If
End If
End Sub