如果那么基于工作表名称的ELSE条件

时间:2018-01-12 14:37:50

标签: excel vba excel-vba

需要一些关于我的VBA代码的帮助。

我正在尝试根据工作表名称执行两个不同的代码:如果工作表名称为A,则执行“此代码”否则执行“其他代码”。 问题是,即使工作表名称不是A,VBA仍应在执行“其他代码”时执行“此代码”。 提前谢谢大家!

这是VBA代码:

Sub Macro2()

    Sheets(2).Activate
[a7] = ActiveSheet.Name
    ' Most frequent used City&Country

If Sheets(2).Range("a7") Like "Sender*" Then
    Sheets("Send Pivots").Visible = True
    Sheets("Send Pivots").Select
    Range("D4").Select
    ActiveSheet.PivotTables("SndAddPvt").PivotFields("Count of Sender Address"). _
        Orientation = xlHidden
    ActiveSheet.PivotTables("SndAddPvt").AddDataField ActiveSheet.PivotTables( _
        "SndAddPvt").PivotFields("Send Consumer City"), "Count of Send Consumer City", _
        xlCount
    With ActiveSheet.PivotTables("SndAddPvt").PivotFields("Send Consumer City")
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables("SndAddPvt").PivotFields("Sender Address").Orientation _
        = xlHidden
    Range("E4").Select
    ActiveSheet.PivotTables("SndAddPvt").PivotFields("Send Consumer City"). _
        AutoSort xlDescending, "Count of Send Consumer City", ActiveSheet.PivotTables( _
        "SndAddPvt").PivotColumnAxis.PivotLines(1), 1
    With ActiveSheet.PivotTables("SndAddPvt").PivotFields("Send Consumer Country")
        .Orientation = xlRowField
        .Position = 2
    End With
      Range("S4").Select
    With ActiveSheet.PivotTables("SndGIDPvt").PivotFields( _
        "Send Consumer ID Type Photo")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("SndGIDPvt").PivotFields( _
        "Send Consumer ID Issue Country")
        .Orientation = xlRowField
        .Position = 3
    End With
    Sheets("Send Pivots").Visible = False
Else
    Sheets("Receives Pivots").Visible = True
        Sheets("Receives Pivots").Select
    Range("D4").Select
    ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Count of Receiver Address"). _
        Orientation = xlHidden
    ActiveSheet.PivotTables("RcvAddPvt").AddDataField ActiveSheet.PivotTables( _
        "RcvAddPvt").PivotFields("Receive Consumer City"), "Count of Receive Consumer City", _
        xlCount
    With ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Receive Consumer City")
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Receiver Address").Orientation _
        = xlHidden
    Range("E4").Select
    ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Receive Consumer City"). _
        AutoSort xlDescending, "Count of Receive Consumer City", ActiveSheet.PivotTables( _
        "RcvAddPvt").PivotColumnAxis.PivotLines(1), 1
    With ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Receive Consumer Country")
        .Orientation = xlRowField
        .Position = 2
    End With
      Range("S4").Select
    With ActiveSheet.PivotTables("RcvGIDPvt").PivotFields( _
        "Receive Consumer ID Type Photo")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("RcvGIDPvt").PivotFields( _
        "Receive Consumer ID Issue Country")
        .Orientation = xlRowField
        .Position = 3
    End With
    Sheets("Receives Pivots").Visible = False
End If

End Sub

3 个答案:

答案 0 :(得分:1)

这就是我所做的:

Sub Macro2()  

Dim ws As Worksheet
Set ws = Sheets(2)

ws.Activate
If InStr(1, ws.Name, "Sender", vbTextCompare) > 0 Then
    Sheets("Send Pivots").Visible = True
    Sheets("Send Pivots").Select
    Range("A1") = "first option"
Else
    Sheets("Receives Pivots").Visible = True
    Sheets("Receives Pivots").Select
    Range("A1") = "Second option"
End If

End Sub

现在第二个工作表名称是" Receiver Summary"所以代码应该取消隐藏工作表"接收Pivot"。执行代码时,它会取消隐藏工作表"发送支点"

答案 1 :(得分:0)

那是因为您正在检查Sheets(2) like "Sender*",而不是活动表。如果表格(2)实际上像发件人一样,这可能总是如此。

我做了一些调整。

Sub Macro2()

If ActiveSheet.Name Like "Sender*" Then
    Sheets("Send Pivots").Visible = True
    Sheets("Send Pivots").Select
    Range("D4").Select
    ActiveSheet.PivotTables("SndAddPvt").PivotFields("Count of Sender Address"). _
        Orientation = xlHidden
    ActiveSheet.PivotTables("SndAddPvt").AddDataField ActiveSheet.PivotTables( _
        "SndAddPvt").PivotFields("Send Consumer City"), "Count of Send Consumer City", _
        xlCount
    With ActiveSheet.PivotTables("SndAddPvt").PivotFields("Send Consumer City")
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables("SndAddPvt").PivotFields("Sender Address").Orientation _
        = xlHidden
    Range("E4").Select
    ActiveSheet.PivotTables("SndAddPvt").PivotFields("Send Consumer City"). _
        AutoSort xlDescending, "Count of Send Consumer City", ActiveSheet.PivotTables( _
        "SndAddPvt").PivotColumnAxis.PivotLines(1), 1
    With ActiveSheet.PivotTables("SndAddPvt").PivotFields("Send Consumer Country")
        .Orientation = xlRowField
        .Position = 2
    End With
      Range("S4").Select
    With ActiveSheet.PivotTables("SndGIDPvt").PivotFields( _
        "Send Consumer ID Type Photo")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("SndGIDPvt").PivotFields( _
        "Send Consumer ID Issue Country")
        .Orientation = xlRowField
        .Position = 3
    End With
    Sheets("Send Pivots").Visible = False
Else
    Sheets("Receives Pivots").Visible = True
        Sheets("Receives Pivots").Select
    Range("D4").Select
    ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Count of Receiver Address"). _
        Orientation = xlHidden
    ActiveSheet.PivotTables("RcvAddPvt").AddDataField ActiveSheet.PivotTables( _
        "RcvAddPvt").PivotFields("Receive Consumer City"), "Count of Receive Consumer City", _
        xlCount
    With ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Receive Consumer City")
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Receiver Address").Orientation _
        = xlHidden
    Range("E4").Select
    ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Receive Consumer City"). _
        AutoSort xlDescending, "Count of Receive Consumer City", ActiveSheet.PivotTables( _
        "RcvAddPvt").PivotColumnAxis.PivotLines(1), 1
    With ActiveSheet.PivotTables("RcvAddPvt").PivotFields("Receive Consumer Country")
        .Orientation = xlRowField
        .Position = 2
    End With
      Range("S4").Select
    With ActiveSheet.PivotTables("RcvGIDPvt").PivotFields( _
        "Receive Consumer ID Type Photo")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("RcvGIDPvt").PivotFields( _
        "Receive Consumer ID Issue Country")
        .Orientation = xlRowField
        .Position = 3
    End With
    Sheets("Receives Pivots").Visible = False
End If

End Sub

答案 2 :(得分:0)

我试过这个并为我工作

Dim ws As Worksheet
Set ws = ActiveSheet

If InStr(1, ws.Name, "Sender", vbTextCompare) > 0 Then
    'execute this code because activesheet is Like Sender*
Else
    'execute another code
End If

Set ws = Nothing