首页 > 解决方案 > VBA 检查特定列中的每一行

问题描述

我想检查列中的每一行F是否有任何值。其中大部分是空的,但是,如果里面有任何字符,我需要检查字符串的第一个字符是否为AB或者C根据第一个字符采取哪些操作。我该如何执行?目前我只发现了一些这样的资源代码,这对我正在做的事情并没有真正的帮助。也用于循环遍历每一行以检查,我使用此代码

While Cells(8 + i, 1).Value <> ""

        i = i + 1
Wend

并获取我找到的字符串的第一个字符

Sub FirstChar()
    Dim xlString As String
    Dim xlFirstChar As String

    xlString = "01999102477490"

    xlFirstChar = Left$(xlString, 1)

    MsgBox xlFirstChar
End Sub

但这仅检查 1 个单元格....

标签: excelvba

解决方案


试试这个代码:

Sub CheckFirstChar()
    Dim lastRow As Long, i As Long
    ' Find last cell in column F
    lastRow = Cells(Rows.Count, 6).End(xlUp).Row

    For i = 1 To lastRow
        firstChar = Left(Cells(i, 6).Value, 1)
        If firstChar = "A" Or firstChar = "B" Or firstChar = "C" Then
            ' Take some actions
        End If
    Next
End Sub

推荐阅读