excel - VBA 检查特定列中的每一行
问题描述
我想检查列中的每一行F
是否有任何值。其中大部分是空的,但是,如果里面有任何字符,我需要检查字符串的第一个字符是否为A
,B
或者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 个单元格....
解决方案
试试这个代码:
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
推荐阅读
- c++ - 如何在 C++ 中将 2D char 数组转换为 1D char 数组
- c# - 改装说它找不到身份验证方法但它大摇大摆地工作
- html - 我必须在 datalist 上单击 3 次才能选择项目
- reactjs - 将文件从 Axios 发布到 Django Rest Framework 时如何解决授权问题?
- java - 有没有办法可以限制 SpringBoot 方法标头中的请求参数数量?
- mysql - 为什么在用 `type=ref` 的扫描替换全表扫描后这个 MySQL 查询变慢了?
- javascript - 根据具有特定类的每个 div 的不同数据属性值更改数据属性值
- reactjs - React & Redux MapStateToProps
- flutter - 一旦用户在颤动的下拉列表中选择了一个项目,如何禁用下拉列表
- javascript - 从 JSON 获取数据并将其格式化为表格