【问题标题】:Excel/VBA Rows and column if statementExcel/VBA 行列 if 语句
【发布时间】:2017-04-14 21:06:47
【问题描述】:

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

因为这将基于第一个 if

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    要查找“强制”是否出现在 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
    

    【讨论】:

    • 谢谢!您知道如何以 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("f" & chk.TopLeftCell.Row).Value = vbNullString 或 ws.Range("g" & chk.TopLeftCell.Row).Value = vbNullString 或等... .
    • @Ttrx 你想用这条线实现什么?
    • 我必须检查列中的单元格是否是强制性的(基于 139 行),然后如果该列对于每个单元格都是强制性的,请检查其是否为空。
    • 所以要明确一点,如果在第 139 行中 A、B、E 和 Y 列的值是强制的,那么如果该列中的单元格为空,则禁用复选框。最后一个禁用复选框有效,但我不知道如何编写 if 语句
    • @Ralph LOL,最近有不少人在这里;)
    【解决方案2】:

    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
    

    【讨论】:

      猜你喜欢
      • 2017-11-30
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 2013-07-09
      • 2018-12-30
      • 2017-06-26
      • 1970-01-01
      相关资源
      最近更新 更多