如何在下面的代码中使用数组来查找多个字符串?
Sub Replace18()
Dim rng As Range
Dim rws As Long
rws = Range("A" & Rows.Count).End(xlUp).Row - 3
Set rng = Rows("3:3").Find(What:="quantity", LookAt:=xlWhole, MatchCase:=False)
If Not rng Is Nothing Then
rng.Offset(1, 0).FormulaR1C1 = "20"
rng.Offset(1, 0).Resize(rws).FillDown
End If
End Sub
答案 0 :(得分:3)
设置变量数组并循环显示它们。
Sub Replace18()
Dim rng As Range, rws As Long, w As Long, vWHATs As Variant
vWHATs = Array("Lorem", "ipsum", "dolor", "amet", "consectetur", "adipiscing", _
"elit", "Mauris", "facilisis", "rutrum", "faucibus", "Sed", _
"euismod", "orci", "rhoncus", "tincidunt", "elit", "eros")
With Worksheets("Sheet2") '<~~set this worksheet reference properly!
rws = .Cells.SpecialCells(xlCellTypeLastCell).Row - 3
For w = LBound(vWHATs) To UBound(vWHATs)
Set rng = .Rows(3).Find(What:=vWHATs(w), LookAt:=xlWhole, MatchCase:=False)
If Not rng Is Nothing Then
'just fill then all at once
rng.Offset(1, 0).Resize(rws, 1) = "20"
End If
Next w
End With
End Sub
我已修改了对“最后一行”的搜索,以使用Range.SpecialCells method选项包含xlCellTypeLastCell的所有列。这适用于我已包含在With ... End With块中的正确引用的父工作表。此块中的所有单元格和范围引用都应带有句点(也称为.
或句号)作为前缀,以指出它们属于With ... End With中引用的工作表。这包括.Rows(3)
,就像.Find
使用前缀时段来指出它引用Rows(3)
一样。
答案 1 :(得分:1)
另一种变体(基于 public function get_country($country_id = '')
{
//I am using method chaining here... it's more efficient,
$query = $this->db->select('id, name')
->where('id', $country_id)
->get('country');
$row = $query->row_array(); //returns a single row
if($row['id'] == 1 && $row['name'] == 'EEUU')
{
}
else
{
}
}
回答)
@Jeeped
或
Sub test()
Dim Dic As Object, k As Variant, S$, rws&, x&, Rng As Range
Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = vbTextCompare
S = "Lorem,ipsum,dolor,amet,consectetur,adipiscing,elit,Mauris," & _
"facilisis,rutrum,faucibus,Sed,euismod,orci,rhoncus,tincidunt,elit,eros"
For Each k In Split(S, ",")
If Not Dic.exists(k) Then Dic.Add k, Nothing
Next k
rws = Range("A" & Rows.Count).End(xlUp).Row - 3
x = [3:3].Find("*", , , xlByColumns, , xlPrevious).Column
For Each Rng In Range([A3], Cells(3, x))
If Dic.exists(Rng.Value) Then
Rng.Offset(1, 0).FormulaR1C1 = "20"
Rng.Offset(1, 0).Resize(rws).FillDown
End If
Next Rng
End Sub