VBA:If-ElseIf具有多个Or条件

时间:2018-10-01 20:15:25

标签: excel vba excel-vba for-loop if-statement

我在摘要报告工作表的第2列中循环,寻找值<> 0。

Summary Report sheet

我正在使用嵌套的If语句来确定第1列中的工具是债券还是商品或股票还是外汇。 如果第1列中第2列的值<> 0的符号对应于资产类别之一,并且该资产类别没有工作表,则应为该资产类别创建一个新的工作表。

On Error Resume Next

For i = 3 To SR.Cells(SR.Rows.Count, 2).End(xlUp).Row

If (SR.Cells(i, 2).Value <> 0) And _
        ((SR.Cells(i, 1).Value Like "*GER10YBond*") Or _
        (SR.Cells(i, 1).Value Like "*Gilt10Y*") Or _
        (SR.Cells(i, 1).Value Like "*JPN10yBond*") Or _
        (SR.Cells(i, 1).Value Like "*US30YBond*")) Then

        'Create new Worksheet named "Bonds"


ElseIf (SR.Cells(i, 2).Value <> 0) And ((SR.Cells(i, 2).Value Like "*#Corn*") Or _
        (SR.Cells(i, 2).Value Like "*#NaturalGas*") Or _
        (SR.Cells(i, 2).Value Like "*#Oil*") Or (SR.Cells(i, 2).Value Like "*#Wheat*") Or _
        (SR.Cells(i, 2).Value Like "*#XAGUSD*") Or (SR.Cells(i, 2).Value Like "*#XAUUSD*") Or _
        (SR.Cells(i, 2).Value Like "*Aluminium*") Or (SR.Cells(i, 2).Value Like "*BrentOil*") Or _
        (SR.Cells(i, 2).Value Like "*Cocoa*") Or (SR.Cells(i, 2).Value Like "*Cocoa!*") Or _
        (SR.Cells(i, 2).Value Like "*Cocoa!*") Or (SR.Cells(i, 2).Value Like "*Coffee*") Or _
        (SR.Cells(i, 2).Value Like "*Coffee!*") Or (SR.Cells(i, 2).Value Like "*Coffee!*") Or _
        (SR.Cells(i, 2).Value Like "*Copper*") Or (SR.Cells(i, 2).Value Like "*Corn*") Or _
        (SR.Cells(i, 2).Value Like "*Corn!*") Or (SR.Cells(i, 2).Value Like "*Corn!*") Or _
        (SR.Cells(i, 2).Value Like "*Cotton*") Or (SR.Cells(i, 2).Value Like "*Cotton!*") Or _
        (SR.Cells(i, 2).Value Like "*NaturalGas*") Or (SR.Cells(i, 2).Value Like "*Oil*") Or _
        (SR.Cells(i, 2).Value Like "*Palladium*") Or (SR.Cells(i, 2).Value Like "*Platinum*") Or _
        (SR.Cells(i, 2).Value Like "*Rice*") Or (SR.Cells(i, 2).Value Like "*soybeans*") Or _
        (SR.Cells(i, 2).Value Like "*Soybeans!*") Or (SR.Cells(i, 2).Value Like "*Soybeans!*") Or _
        (SR.Cells(i, 2).Value Like "*Soybeans!*") Or (SR.Cells(i, 2).Value Like "*Wheat*") Or _
        (SR.Cells(i, 2).Value Like "*Wheat!*") Or (SR.Cells(i, 2).Value Like "*Wheat!*") Or _
        (SR.Cells(i, 2).Value Like "*XAGUSD*") Or (SR.Cells(i, 2).Value Like "*XAGUSD.*") Or _
        (SR.Cells(i, 2).Value Like "*XAUUSD*") Or (SR.Cells(i, 2).Value Like "*XAUUSD.*")) Then

       ' Create new Worksheet named "Commodities"

End If

Next i

当循环从ElseIf语句中击中资产时,其值在第2列<> 0中时,它仅跳至End If并转到Next Iteration

为什么?

3 个答案:

答案 0 :(得分:1)

我建议您使用Collection / Dictionary对象保存列表。本示例以字典(=哈希映射,关联数组)为例。使用VBA编辑器中的Tools/Referennces菜单启用脚本运行时。这是大多数体面的VBA应用程序所需要的。

您可以在“参数”表中维护列表,并在VBA函数中填充字典。我试图以理智的格式构造循环,以便更轻松地了解正在发生的事情。

多数编程指南也有充分的理由避免使用GOTO命令。但是,VBA缺少继续声明,如果您问我,这对于goto声明来说是很少见的好用法。

Option Explicit
' Tools/References: [x]Microsoft Scripting Runtime

Public Sub doIt()
    Dim ws As Worksheet
    Dim iRow As Long
    Dim idx As Long
    Dim val As String
    Dim key As String
    Dim bonds As New Scripting.Dictionary
    Dim commodities As New Scripting.Dictionary

    Call bonds.Add("*GER10YBond*", "")
    Call bonds.Add("*Gilt10Y*", "")
    Call bonds.Add("*JPN10yBond*", "")
    Call bonds.Add("*US30YBond*", "")

    Call commodities.Add("*[#]Corn*", "")
    Call commodities.Add("*[#]NaturalGas*", "")
    Call commodities.Add("*[#]Oil*", "")
    Call commodities.Add("*[#]Wheat*", "")

    Set ws = Application.Sheets("Sheet1")
    For iRow = 3 To ws.UsedRange.Rows.Count
        val = ws.Cells(iRow, 2).Value
        If val = "0" Or val = "" Then GoTo ContinueLoop
        val = LCase(ws.Cells(iRow, 1).Value)

        For idx = 0 To bonds.Count - 1
            key = bonds.Keys(idx)
            If val Like LCase(key) Then
                ws.Cells(iRow, 3) = "bonds " & key
                GoTo ContinueLoop
            End If
        Next

        For idx = 0 To commodities.Count - 1
            key = commodities.Keys(idx)
            If val Like LCase(key) Then
                ws.Cells(iRow, 3) = "commodities " & key
                GoTo ContinueLoop
            End If
        Next

        ws.Cells(iRow, 3) = "Unknown"

ContinueLoop:
        ' next step
    Next iRow
End Sub

答案 1 :(得分:1)

您正在检查同一单元格中的0和文本。您将需要检查文本的第一列

SR.Cells(i, 2).Value <> 0) And ((SR.Cells(i, 1).Value Like "*#Corn*")  –

答案 2 :(得分:0)

@Whome,

根据您的建议,我设法合并了此循环。

Option Explicit
Public Sub PopulateHistoricalData(ByVal BondsDict As Dictionary, ByVal CryptoDict As Dictionary, ByVal CommoditiesDict As Dictionary, ByVal IndexesDict As Dictionary, ByVal FXDict As Dictionary, ByVal StocksDict As Dictionary)

          Dim CTF As Workbook
          Dim SR As Worksheet

          Dim SRRow As Long
          Dim ItemIndex As Long

          Dim Deal As String
          Dim SheetName As String
          Dim DealVal As String
          Dim Key1 As String
          Dim Key2 As String
          Dim Key3 As String
          Dim Key4 As String
          Dim Key5 As String
          Dim Key6 As String

          Dim FSO As Object
          Dim Folder As Object

1         Set CTF = Workbooks("CodeTestFile_V2")
2         Set SR = Worksheets("Summary Report")

3         Set FSO = CreateObject("Scripting.FileSystemObject")
4         Set Folder = FSO.GetFolder("C:\Users\Betty\AppData\Roaming\MetaQuotes\Terminal\B4D9BCD10BE9B5248AFCB2BE2411BA10\MQL4\Files")

5         On Error Resume Next

6         For SRRow = 3 To SR.Cells(SR.Rows.Count, 2).End(xlUp).Row: Do

7             Deal = SR.Cells(SRRow, 2)

8             If Deal = 0 Or Deal = "" Then Exit Do

9             DealVal = SR.Cells(SRRow, 2).Offset(, -1).Value

10            For ItemIndex = 0 To BondsDict.Count - 1

11                Key1 = BondsDict.Keys(ItemIndex)

12                If DealVal = Key1 Then

13                    SheetName = "Bonds"

14                        If FilepathExists(ByVal DealVal, ByVal SheetName, ByVal Folder) Then

15                            If SheetExists(ByVal SheetName, ByVal DealVal, ByVal Folder, ByVal Key1, ByVal Key2, ByVal Key3, ByVal Key4, ByVal Key5, ByVal Key6) Then

16                                With Worksheets(SheetName)
17                                    Call PopulateHeadersAndClosePrices(ByVal DealVal, ByVal SheetName, ByVal Folder)
18                                End With

19                            End If

20                        End If

21                        Exit Do

22                End If

23            Next ItemIndex

24            For ItemIndex = 0 To CryptoDict.Count - 1

25                Key2 = CryptoDict.Keys(ItemIndex)

26                If DealVal = Key2 Then

27                    SheetName = "Crypto"

28                    If FilepathExists(ByVal DealVal, ByVal SheetName, ByVal Folder) Then

29                        If SheetExists(ByVal SheetName, ByVal DealVal, ByVal Folder, ByVal Key1, ByVal Key2, ByVal Key3, ByVal Key4, ByVal Key5, ByVal Key6) Then

30                            With Worksheets(SheetName)
31                                Call PopulateHeadersAndClosePrices(ByVal DealVal, ByVal SheetName, ByVal Folder)
32                            End With

33                        End If

34                    End If

35                    Exit Do

36                End If

37            Next ItemIndex

38            For ItemIndex = 0 To CommoditiesDict.Count - 1

39                Key3 = CommoditiesDict.Keys(ItemIndex)

40                If DealVal = Key3 Then

41                    SheetName = "Commodities"

42                    If FilepathExists(ByVal DealVal, ByVal SheetName, ByVal Folder) Then

43                        If SheetExists(ByVal SheetName, ByVal DealVal, ByVal Folder, ByVal Key1, ByVal Key2, ByVal Key3, ByVal Key4, ByVal Key5, ByVal Key6) Then

44                            With Worksheets(SheetName)
45                                Call PopulateHeadersAndClosePrices(ByVal DealVal, ByVal SheetName, ByVal Folder)
46                            End With

47                        End If

48                    End If

49                    Exit Do

50                End If

51            Next ItemIndex

52            For ItemIndex = 0 To IndexesDict.Count - 1

53                Key4 = IndexesDict.Keys(ItemIndex)

54                If DealVal = Key4 Then

55                    SheetName = "Indexes"

56                    If FilepathExists(ByVal DealVal, ByVal SheetName, ByVal Folder) Then

57                        If SheetExists(ByVal SheetName, ByVal DealVal, ByVal Folder, ByVal Key1, ByVal Key2, ByVal Key3, ByVal Key4, ByVal Key5, ByVal Key6) Then

58                            With Worksheets(SheetName)
59                                Call PopulateHeadersAndClosePrices(ByVal DealVal, ByVal SheetName, ByVal Folder)
60                            End With

61                        End If

62                    End If

63                    Exit Do

64                End If

65            Next ItemIndex

66            For ItemIndex = 0 To FXDict.Count - 1

67                Key5 = FXDict.Keys(ItemIndex)

68                If DealVal = Key5 Then

69                    SheetName = "FX"

70                    If FilepathExists(ByVal DealVal, ByVal SheetName, ByVal Folder) Then

71                        If SheetExists(ByVal SheetName, ByVal DealVal, ByVal Folder, ByVal Key1, ByVal Key2, ByVal Key3, ByVal Key4, ByVal Key5, ByVal Key6) Then

72                            With Worksheets(SheetName)
73                                Call PopulateHeadersAndClosePrices(ByVal DealVal, ByVal SheetName, ByVal Folder)
74                            End With

75                        End If

76                    End If

77                    Exit Do

78                End If

79            Next ItemIndex

80            SheetName = vbNullString

81            For ItemIndex = 0 To StocksDict.Count - 1

82                Key6 = StocksDict.Keys(ItemIndex)

83                If DealVal = Key6 Then

84                    SheetName = "Stocks"

85                    If FilepathExists(ByVal DealVal, ByVal SheetName, ByVal Folder) Then

86                        If SheetExists(ByVal SheetName, ByVal DealVal, ByVal Folder, ByVal Key1, ByVal Key2, ByVal Key3, ByVal Key4, ByVal Key5, ByVal Key6) Then

87                            With Worksheets(SheetName)
88                                Call PopulateHeadersAndClosePrices(ByVal DealVal, ByVal SheetName, ByVal Folder)
89                            End With

90                        End If

91                    End If

92                    Exit Do

93                End If

94            Next ItemIndex

95        Loop While False: Next SRRow

End Sub

请注意我用来避免使用Continue命令的方法。