如果使用Len的全列循环

时间:2013-06-21 19:59:19

标签: vba loops

我想做一个if语句来测量autofilter列中的字符数量,如果字符数(Len)= 9或12,我希望它转到第一列并更改单元格的名称在那一行“CLO”。我想让它运行完整列(列M& N)。我该怎么办呢?

    Dim count As Integer, myRange As Range, rng As Range
Dim RealRng As Range

Set myRange = Columns("M:M")
count = Application.WorksheetFunction.CountA(myRange)
Set RealRng = ActiveSheet.Range("M2:M+" & count)
For Each rng In RealRng
    If Len(rng) = 9 Or Len(rng) = 12 Then
        rng.Offset(0, -12) = "Bond"
    End If
Next rng

1 个答案:

答案 0 :(得分:0)

这是一个简单的演示,它将遍历过滤范围,测试长度,如果长度为9或12,则更改下一列。

您需要根据自己的目的进行调整:

Sub Demo()
    Dim filteredRng As Range, rng As Range

    Set filteredRng = Range("A1:A10").SpecialCells(xlCellTypeVisible)

    For Each rng In filteredRng
        If Len(rng) = 9 Or Len(rng) = 12 Then
            rng.Offset(0, 1) = "CLO"
        End If
    Next rng

End Sub