Excel/VBA行和列if语句
我想做一些函数,允许我检查第149行中的任何字段是否为文本“必填”,如果是,则检查下面的行是否为空,如果是,则执行某事。 所以我试着这样做:Excel/VBA行和列if语句,vba,excel,Vba,Excel,我想做一些函数,允许我检查第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 Chec
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
因为这将基于第一个if来查找ws
工作表的第149行中是否出现“必填”项,请使用应用程序。匹配功能
见下面的代码:
If Not IsError(Application.Match("Mandatory", ws.Rows(149), 0)) Then ' <-- successful match
' rest of your code goes here
End If
如果不是iError(Application.Match(“Mandatory”,ws.Rows(149),0)),则“要查找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
如果不是IsError(Application.Match(“强制”,ws.Rows(149),0)),则为“1”。确定要检查的范围
更好的做法是精确指定要检查值的范围。否则,可能会使VBA效率低下。因此,也许您只需要检查范围(“A149:Z149”)
,从现在起,请参考asSearchRange
2.将范围放入VBA数组(可选,推荐)
在VBA中,您可以将SearchedRange
转换为Variant
数组。
像这样:
Dim SearchedArray as Variant
SearchedArray = sheetXY.range("A149:Z149").value2
Dim Member as Variant
For Each Member In SearchedArray
If Member = "Mandatory" Then
Msgbox "Found it!"
Exit For
End If
Next
3.根据条件检查数组的每个成员
然后可以循环遍历变量数组中的所有值。
像这样:
Dim SearchedArray as Variant
SearchedArray = sheetXY.range("A149:Z149").value2
Dim Member as Variant
For Each Member In SearchedArray
If Member = "Mandatory" Then
Msgbox "Found it!"
Exit For
End If
Next
1.确定要检查的范围
更好的做法是精确指定要检查值的范围。否则,可能会使VBA效率低下。因此,也许您只需要检查范围(“A149:Z149”)
,从现在起,请参考asSearchRange
2.将范围放入VBA数组(可选,推荐)
在VBA中,您可以将SearchedRange
转换为Variant
数组。
像这样:
Dim SearchedArray as Variant
SearchedArray = sheetXY.range("A149:Z149").value2
Dim Member as Variant
For Each Member In SearchedArray
If Member = "Mandatory" Then
Msgbox "Found it!"
Exit For
End If
Next
3.根据条件检查数组的每个成员
然后可以循环遍历变量数组中的所有值。
像这样:
Dim SearchedArray as Variant
SearchedArray = sheetXY.range("A149:Z149").value2
Dim Member as Variant
For Each Member In SearchedArray
If Member = "Mandatory" Then
Msgbox "Found it!"
Exit For
End If
Next
谢谢您知道如何以VBA可以接受的方式编写此文件吗?如果ws.Range(“C”&chk.TopLeftCell.Row)。Value=vbNullString或ws.Range(“D”&chk.TopLeftCell.Row)。Value=vbNullString或ws.Range(“E”&chk.TopLeftCell.Row)。Value=vbNullString或ws.Range(“g”&chk.TopLeftCell.Row).Value=vbNullString或etc…@Ttrx这一行的具体用途是什么?我必须检查列中的单元格是否是强制的(基于139行),然后检查每一列中该列是否为强制的。因此要清楚,如果第139行中的A、B列,E和Y值是必需的,如果此列中的单元格为空,则禁用复选框。最后一个禁用复选框可以工作,但我不知道如何编写ifstatements@Ralph哈哈,最近这里有不少人;)谢谢您知道如何以VBA可以接受的方式编写此文件吗?如果ws.Range(“C”&chk.TopLeftCell.Row)。Value=vbNullString或ws.Range(“D”&chk.TopLeftCell.Row)。Value=vbNullString或ws.Range(“E”&chk.TopLeftCell.Row)。Value=vbNullString或ws.Range(“g”&chk.TopLeftCell.Row).Value=vbNullString或etc…@Ttrx这一行的具体用途是什么?我必须检查列中的单元格是否是强制的(基于139行),然后检查每一列中该列是否为强制的。因此要清楚,如果第139行中的A、B列,E和Y值是必需的,如果此列中的单元格为空,则禁用复选框。最后一个禁用复选框可以工作,但我不知道如何编写ifstatements@Ralph哈哈,最近这里有不少人;)