'Most efficient way to have multiple criteria in an IF statement [closed]
Basically, I hoping to achieve 2 things here.
I want to know if there is a way to make this multiple criteria IF statement shorter/more efficient. ie. how would this be done if using VBA best practices?
Is there a way to name each column as a range or variable on sheet 'upload file'? This works fine, I just want to know if there is a way that is better or if there is a 'best practices' method that would work better.
Thank you
If (bulksheet.range("b" & x).Value = "keyword" Or bulksheet.range("b" & x).Value = "product targeting") And bulksheet.range("d" & x).Value = campaign And bulksheet.range("p" & x).Value = "enabled" And _
bulksheet.range("r" & x).Value = "enabled" And (matchtype = "broad" Or matchtype = "phrase" Or matchtype = "exact" Or matchtype = "targeting expression" Or matchtype = "targeting expression predefined") Then
uploadfile.range("a" & uploadrowcounter).Value = campaign
uploadfile.range("f" & uploadrowcounter).Value = adgroup
uploadfile.range("i" & uploadrowcounter).Value = keyword
uploadfile.range("n" & uploadrowcounter).Value = "enabled"
uploadfile.range("j" & uploadrowcounter).Value = targetingid
uploadfile.range("k" & uploadrowcounter).Value = matchtype
Solution 1:[1]
This is hundreds of times faster...
Dim v, w
v = bulksheet.Range("a" & x & ":r" & x)
If v(1, 2) = "keyword" Or v(1, 2) = "product targeting" Then
If v(1, 4) = campaign Then
If v(1, 16) = "enabled" Then
If v(1, 18) = "enabled" Then
Select Case matchtype
Case "broad", "phrase", "exact", "targeting expression", "targeting expression predefined"
With uploadfile.Range("a" & uploadrowcounter & ":n" & uploadrowcounter)
w = .Value2
w(1, 1) = campaign
w(1, 6) = adgroup
w(1, 9) = keyword
w(1, 14) = "enabled"
w(1, 10) = targetingid
w(1, 11) = matchtype
.Value2 = w
End With
End Select
End If
End If
End If
End If
Research Short-circuiting Conditional Expressions in VBA.
Research Variant Arrays for Speed in VBA.
Solution 2:[2]
Not sure about efficiency but Select Case
is useful for multiple ORs and loops for ANDs.
Dim b(4) As Boolean, i As Integer
Select Case matchtype
Case "broad", "phrase", "exact", "targeting expression", _
"targeting expression predefined"
b(0) = True
End Select
With bulksheet
Select Case .Cells(x, "B")
Case "keyword", "product targeting": b(1) = True
End Select
b(2) = (.Cells(x, "D").Value = campaign)
b(3) = (.Cells(x, "P").Value = "enabled")
b(4) = (.Cells(x, "R").Value = "enabled")
End With
' AND
For i = 1 To UBound(b)
b(0) = b(0) And b(i)
Next
Dim ar
ar = Array("A", campaign, "F", adgroup, "I", keyword, _
"N", "enabled", "J", targetingid, "K", matchtype)
If b(0) Then
For i = 0 To UBound(ar) Step 2
uploadfile.Cells(uploadrowcounter, ar(i)).Value = ar(i + 1)
'Debug.Print ar(i), ar(i + 1)
Next
End If
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | CDP1802 |