Объединить 2 изменения события в 1 листе - PullRequest
0 голосов
/ 04 февраля 2019

Не могли бы вы сообщить мне, как я могу иметь оба кода в листе?Они прекрасно работают по отдельности.

Я пробовал называть 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

Могут ли работать оба кода независимо от того, какой из них выбран первым?

1 Ответ

0 голосов
/ 04 февраля 2019

Вы можете включить оба 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...