vba - Excel VBA 在单行的两列中查找数据
问题描述
我试图让这段代码找到 V 列不等于“Y”或“L”且 A 列不为空的行。我想我做得有点过头了,我确信有一种更简单的方法可以检查行上的两个单元格。
Sub EndMove()
Dim Col1 As Integer, Col2 As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String, currentRowValue2 As String
Col1 = 22
Col2 = 1
rowCount = Cells(Rows.Count, Col1).End(xlUp).row
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, Col1).Value
If currentRowValue <> "y" Or currentRowValue <> "l" Then
currentRowValue2 = Cells(currentRow, Col2).Value
If Not IsEmpty(currentRowValue2) Then
Cells(currentRow, Col1).Select
MsgBox "Move this?"
End If
End If
Next
结束子
谢谢
解决方案
你很亲密。我改成currentrow
是i
因为它更容易多次使用。您还应该限定您的工作表。每当您引用目标工作表上的对象时,请使用ws
这也是区分大小写的。IE Y
<> y
。如果你想让它忽略大小写,你可以放在Option Compare Text
上面Sub EndMove
Option Explicit
Sub EndMove()
Dim rowCount As Long, i As Long
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
rowCount = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
'i refers to row number
For i = 11 To rowCount
If ws.Range("V" & i) <> "Y" And ws.Range("V" & i) <> "L" Then
If ws.Range("A" & i) <> "" Then
'Do what with row i?
End If
End If
Next i
End Sub
您也可以像这样将所有 3 个标准组合成一行
For i = 11 To rowCount
If ws.Range("V" & i) <> "Y" And ws.Range("V" & i) <> "L" And ws.Range("A" & i) <> "" Then
'Do what with row i?
End If
Next i
推荐阅读
- jquery - 使用 JQUERY 和 AJAX 进行 Laravel 分页
- reactjs - Tippyjs React:可见属性之前的内容更新
- javafx - JavaFX - TableHeaderRow 上的上下文菜单会产生列调整大小的问题
- sql - 查看 BigQuery 表/数据集的历史元数据
- c# - 获取 USB 存储驱动器号和硬件序列号的可靠方法
- geospatial - 使用 Spring Data JDBC 进行地理空间
- r - 尽管安装成功,但“找不到函数 read.excel”
- java - 更新后自动构建卡在 Android Studio 上
- spring - Spring Aggregations - 按许多条件过滤
- spring-boot - 具有不完整 liquibase 更改日志的集成测试