Excel / VBA行和列if语句
我想做一些函数,让我检查是否有任何第149行的字段是文本“强制性”,那么如果是,检查下面的行是空的,如果是然后做某事。 所以我试过这样的东西:
If ws.Rows("149") = "Mandatory" Then If ws.Range("C" & chk.TopLeftCell.Row).Value
但我不知道如何编写第二个检查每列的值
帮助家伙! 谢谢!
我现在的vba脚本:
Sub CheckBoxDate() Dim ws As Worksheet Dim chk As CheckBox Dim lColD As Long Dim lColChk As Long Dim lRow As Long Dim rngD As Range lColD = 0 'number of columns to the right for date Set ws = Sheets("MA Template_VBack-End") Set chk = ws.CheckBoxes(Application.Caller) lRow = chk.TopLeftCell.Row lColChk = chk.TopLeftCell.Column Set rngD = ws.Cells(lRow, lColChk + lColD) Select Case chk.Value Case 1 'box is checked For Each chk In ws.CheckBoxes If ws.Range("C" & chk.TopLeftCell.Row).Value = vbNullString Then chk.Enabled = False rngD.EntireRow.Interior.Color = vbGreen End If Next chk Case Else 'box is not checked rngD.ClearContents rngD.EntireRow.Interior.ColorIndex = xlColorIndexNone End Select End Sub
现在,主要的问题之一是如何写这个声明:
If ws.Range("C" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("D" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("E" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("f" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("g" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("i" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("t" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("u" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("z" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ab" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ac" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ap" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("at" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("bs" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("bt" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("bu" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("bv" & chk.TopLeftCell.Row).Value = vbNullSt ring Or ws.Range("bx" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("bz" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ca" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("cc" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("cd" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ce" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ci" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ck" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("cl" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("cm" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("cn" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("co" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("cp" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("cq" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("cs" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ea" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ed" & chk.TopLeftCell.Row).Va lue = vbNullString Or ws.Range("ee" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("eg" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("eh" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ei" & chk.TopLeftCell.Row).Value = vbNullString Or ws.Range("ej" & chk.TopLeftCell.Row).Value = vbNullString Then
因为这将基于第一如果
要查找ws
工作表中行149中是否出现“Mandatory”,请使用Application.Match
函数。
看到下面的代码:
If Not IsError(Application.Match("Mandatory", ws.Rows(149), 0)) Then ' <-- successful match ' rest of your code goes here End If
1.确定要检查的范围
最好指定一个范围,然后检查值。 否则,你可以使你的VBA效率低下。 所以,也许你只需要检查范围("A149:Z149")
,从现在起被调整为SearchRange
2.获取范围到VBA数组(可选,推荐)
在VBA中,可以将SearchedRange
传递给Variant
数组。 像这样:
Dim SearchedArray as Variant SearchedArray = sheetXY.range("A149:Z149").value2
3.根据条件检查每个数组成员
然后,您可以遍历Variant数组中的所有值。 像这样:
Dim Member as Variant For Each Member In SearchedArray If Member = "Mandatory" Then Msgbox "Found it!" Exit For End If Next