![](/img/trans.png)
[英]Excel VBA if/elseif function with multiple conditions and dropdowns
[英]VBA: If - ElseIf with multiple Or conditions
我在摘要報告工作表的第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.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
。
為什么?
我建議您使用Collection / Dictionary對象來保存列表。 本示例以字典(=哈希映射,關聯數組)為例。 使用VBA編輯器中的“ Tools/Referennces
菜單啟用腳本運行時。 這是大多數體面的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*") –
@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
命令的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.