使用VBA和Excel

时间:2019-02-21 21:32:54

标签: excel vba

Before Code Execution

After Code Execution

我在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

1 个答案:

答案 0 :(得分:0)

这将动态选择整个使用范围,然后在使用范围右侧的第一列中为每一行填充“ 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