Excel / VBA行和列if语句

时间:2017-04-14 12:44:42

标签: excel vba excel-vba

我想制作一些功能,让我检查第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

因为这将基于第一个

2 个答案:

答案 0 :(得分:2)

要查看ws工作表中的第149行是否显示“必填”,请使用Application.Match功能。

请参阅以下代码:

If Not IsError(Application.Match("Mandatory", ws.Rows(149), 0)) Then ' <-- successful match
    ' rest of your code goes here

End If

答案 1 :(得分:0)

1。确定要检查的范围

最好准确指定范围,并检查值。否则,您可以使您的VBA效率低下。所以,也许您只需要检查范围from __future__ import absolute_import ,从现在开始参考("A149:Z149")

2。获取VBA阵列的范围(可选,推荐)

在VBA中,您可以将SearchRange转移到SearchedRange阵列。 像这样:

Variant

3。根据条件

检查数组的每个成员

然后,您可以遍历Variant数组中的所有值。 像这样:

Dim SearchedArray as Variant
SearchedArray  = sheetXY.range("A149:Z149").value2