我在工作表中循环以查找与某些变量匹配的行时遇到问题。
我每行有8列,我想找到一个包含6个变量的行,一个空单元格和一个值为0的单元格。像这样:
- | A | B | C | D | E | F | G | H | i | x1 | | x2 | 0 | x3 | x4 | x5 | x6 |
其中i
是行号而x1
,x2
,.. x6
是从CSV文件获取的变量。我想检查一下我的列表'变量{x1, ,x2,0,x3,x4,x5,x6}
是工作表中的现有行。所以我希望程序在If (Ai = x1 And Bi = "" And Ci = x2 And Di = "0" And Ei = x3 And Fi = x4 And Gi = x5 And Hi = x6)
时执行Else
并执行任何操作。
所以我需要遍历所有行并检查所有变量是否都在一行中。
目前我尝试了这一点,但它似乎无法奏效。
LastCol = 8
LastRow = ThisWorkbook.Sheets("Boekingen AMS-IAD").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
For j = 1 To LastCol
'Tried with only 1 criteria, still didn't work
If (ThisWorkbook.Sheets("Boekingen AMS-IAD").Cells(i, j).Value = x1) Then
Accept = "nvt"
End If
Next j
Next i
答案 0 :(得分:0)
你可以尝试这样的事情。除了几个例外,我几乎把所有东西都留下了。
lastcol = 8
Dim vars
ReDim vars(1 To lastcol)
'Set vars() to the values you're looking for in the columns.
LastRow = ThisWorkbook.Sheets("Boekingen AMS-IAD").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
accept = ""
met = 0
For j = 1 To lastcol
With ThisWorkbook.Sheets("Boekingen AMS-IAD").Cells(i, j)
If .Value = vars(j) Then
met = met + 1
End If
End With
Next j
If met = lastcol Then accept = "nvt"
'At this point you have to do something with the row you just
' found--maybe leave the loop and do something, or do something
' before going to the next row.
Next i
答案 1 :(得分:0)
我在一个新的excel文件夹中创建了这个快速的小测试子,将@Import('statusStream:1.0.0')
define stream statusStream (id string, success bool);
@Export('alertStream:1.0.0')
define stream alertStream (alert string);
from statusStream
select id, success, time:timestampInMilliseconds() as ts
insert into statusStreamWithTS;
from every e1=statusStreamWithTS[success==false], statusStreamWithTS[success==false]*, e2=statusStreamWithTS[success==false AND (e2.ts - e1.ts) >= 500000]
select 'some message' as alert
insert into alertStream;
重命名为您的工作表名称并且它的工作原理非常好:
Sheet1
这应该吐出" nvt"当该行跟随您的变量时:)
PS - 任何想要测试此代码的人,您只需在Sub testing()
Dim ws As Worksheet
Dim rng As Range
Set ws = ThisWorkbook.Sheets("Boekingen AMS-IAD")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
If ws.Cells(i, 1).Value = x1 And _
ws.Cells(i, 2).Value = "" And _
ws.Cells(i, 3).Value = x2 And _
ws.Cells(i, 4).Value = "0" And _
ws.Cells(i, 5).Value = x3 And _
ws.Cells(i, 6).Value = x4 And _
ws.Cells(i, 7).Value = x5 And _
ws.Cells(i, 8).Value = x6 Then
'MsgBox "nvt at row " & i
Accept = "nvt"
End If
Next i
End Sub
下添加以下内容:
Dim rng as Range
答案 2 :(得分:0)
我解决了这个问题,问题不在于循环遍历工作表,而是在变量的值中。当我从Excel表格中读取日期时,日期从D-M-Y变为M-D-Y。但我解决了那些对答案感兴趣的人:VBA changes date format from D-M-Y to M-D-Y