VBA:If-ElseIf具有多个Or条件
我在工作表摘要报告的第2列循环,查找值0 我使用嵌套的VBA:If-ElseIf具有多个Or条件,vba,excel,for-loop,if-statement,Vba,Excel,For Loop,If Statement,我在工作表摘要报告的第2列循环,查找值0 我使用嵌套的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
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
出错时继续下一步
对于i=3到SR.Cells(SR.Rows.Count,2)。结束(xlUp)。行
If(SR.Cells(i,2).值0)和_
((SR.Cells(i,1).值,如“*GER10YBond*”)或_
(SR.Cells(i,1).值如“*Gilt10Y*”)或_
(SR.Cells(i,1).值如“*JPN10yBond*”)或_
(SR.Cells(i,1)。值类似于“*US30YBond*”),然后
'创建名为“Bonds”的新工作表'
ElseIf(SR.Cells(i,2).值0)和((SR.Cells(i,2).值如“*#Corn*”)或_
(SR.Cells(i,2).值,如“*#NaturalGas*”)或_
(SR.Cells(i,2).值如“*#Oil*”)或(SR.Cells(i,2).值如“*#Wheat*”)或_
(SR.Cells(i,2).值如“*#XAGUSD*”)或(SR.Cells(i,2).值如“*#XAUUSD*”)或_
(SR.Cells(i,2).值如“*Aluminal*”)或(SR.Cells(i,2).值如“*BrentOil*”)或_
(SR.Cells(i,2).值如“*Cocoa*”)或(SR.Cells(i,2).值如“*Cocoa!*”)或_
(SR.Cells(i,2).值如“*Cocoa!*”)或(SR.Cells(i,2).值如“*Coffee*”)或_
(SR.Cells(i,2).值如“*Coffee!*”)或(SR.Cells(i,2).值如“*Coffee!*”)或_
(SR.Cells(i,2).值如“*铜*”)或(SR.Cells(i,2).值如“*玉米*”)或_
(SR.Cells(i,2).值如“*Corn!*”)或(SR.Cells(i,2).值如“*Corn!*”)或_
(SR.Cells(i,2).值如“*Cotton*”)或(SR.Cells(i,2).值如“*Cotton!*”)或_
(SR.Cells(i,2).值如“*天然藻类*”)或(SR.Cells(i,2).值如“*石油*”)或_
(SR.Cells(i,2).值如“*钯*”)或(SR.Cells(i,2).值如“*铂*”)或_
(SR.Cells(i,2).值如“*大米*”)或(SR.Cells(i,2).值如“*大豆*”)或_
(SR.Cells(i,2).值如“*大豆!*”)或(SR.Cells(i,2).值如“*大豆!*”)或_
(SR.Cells(i,2).值如“*大豆!*”)或(SR.Cells(i,2).值如“*小麦*”)或_
(SR.Cells(i,2).值如“*麦!*”)或(SR.Cells(i,2).值如“*麦!*”)或_
(SR.Cells(i,2).值如“*XAGUSD*”)或(SR.Cells(i,2).值如“*XAGUSD*”)或_
(SR.Cells(i,2).值类似于“*XAUUSD*”)或(SR.Cells(i,2).值类似于“*XAUUSD*”),然后
'创建名为“商品”的新工作表'
如果结束
接下来我
当循环从ElseIf
语句命中一个资产时,在第20列中有一个值,如果,它只会跳到End,然后转到下一次迭代
为什么? 我建议您使用Collection/Dictionary对象来保存列表。此示例使用字典(=hashmap,关联数组)作为示例。使用VBA编辑器中的Tools/references
菜单启用脚本运行时。这是大多数尺寸合适的VBA应用程序所需要的
您可以在参数表中维护列表,并在VBA函数中填充字典。我试图以一种合理的格式构造循环,以便更容易地跟踪正在发生的事情
此外,大多数编程指南都有很好的理由避免使用GOTO命令。然而VBA缺少一个continue语句,如果你问我的话,这是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
我建议您使用Collection/Dictionary对象来保存列表。此示例使用字典(=hashmap,关联数组)作为示例。使用VBA编辑器中的Tools/references
菜单启用脚本运行时。这是大多数尺寸合适的VBA应用程序所需要的
您可以在参数表中维护列表,并在VBA函数中填充字典。我试图以一种合理的格式构造循环,以便更容易地跟踪正在发生的事情
此外,大多数编程指南都有很好的理由避免使用GOTO命令。然而VBA缺少一个continue语句,如果你问我的话,这是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
您正在为0和文本检查同一单元格。您需要检查文本的第一列
SR.Cells(i, 2).Value <> 0) And ((SR.Cells(i, 1).Value Like "*#Corn*") –
SR.Cells(i,2).值0)和((SR.Cells(i,1).值如“*#Corn*”)
您正在为0和文本检查同一单元格。您需要检查文本的第一列
SR.Cells(i, 2).Value <> 0) And ((SR.Cells(i, 1).Value Like "*#Corn*") –
SR.Cells(i,2).值0)和((SR.Cells(i,1).值如“*#Corn*”)
@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
命令的方法。@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
请注意,我避免使用<代码>的方法是继续< /COD>命令。
您必须在错误的地方输入“代码>”,然后再在下一个< /CODE >某个地方,因为VBA中的条件表达式从不短路,所以整个表达式需要每一次进行评估。假设为真。这些单元格中是否有任何单元格包含错误值(例如#DIV/0!
,#N/A
,#value!
等)用于与运算符进行模式匹配。您需要将其括在括号内。代码中嵌入的此类检查的大列表很难维护-如果您将单词列表放在工作表中,您可以在单个操作中直接对照列表进行检查。Dim foo As Variant
;foo=SR.Cells(i,2).Value
???…你在尝试吗?我还没有投反对票,但如果我不得不猜测的话,我敢说下一次错误简历上的领先优势还没有被清除;我们已经有15多条评论了,你的帖子仍然没有足够的信息来提供有意义的答案。请随时回答你的问题。你一定有