[英]Find matching values across multiple columns using VBA with Excel
我在 excel 中有如下所示的 VBA 代碼,它允許我標記一列或多列中包含“FALSE”一詞的行,如圖所示。 我如何讓代碼只循環行和列的乘積次數,在這種情況下為 25(5 行 * 5 列),並在不使用 For 循環的情況下退出。
Dim myRange As Range
Dim Rc As Long
Dim Cc As Long
Dim i As Integer
Set myRange = Range("C12:G16")
Rc = myRange.Rows.Count
Cc = myRange.Columns.Count
iter = Rc * Cc
With Worksheets(1).Range("C12:G16")
Set c = .Find("False", LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
i = Cc - c.Column + 2
c.Select
Selection.Offset(0, i) = "FALSE"
Set c = .FindNext(c)
Loop While Not c Is Nothing
End If
End With
這將動態選擇您的整個使用范圍,然后如果 False 存在於該行,則為您使用范圍右側第一列中的每一行填充“False”。
Sub LabelRowsFalse()
Dim myRng As Range
Dim rngWidth As Integer
Dim nextCol As Integer
Dim rngCol As Integer
Set myRng = Sheet1.UsedRange
rngWidth = myRng.Columns.Count
rngCol = myRng.Column
With myRng
Set c = .Find("False", LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Cells(c.Row, (rngWidth + rngCol)).Value = "False"
Set c = .FindNext(c)
While c.Address <> firstAddress
Cells(c.Row, (rngWidth + rngCol)).Value = "False"
Set c = .FindNext(c)
Wend
End If
End With
MsgBox ("done")
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.