【问题标题】:How to select table cell in the if function in VBA excel?如何在VBA excel的if函数中选择表格单元格?
【发布时间】:2021-11-18 06:32:30
【问题描述】:

我在 Excel 表中有一个库存管理表。演示在这里: Demo photo of this table

我想在 In stock 列中创建一个 VBA 代码,并测试整个单元格。如果任何 cell.Value 0 则向后转到 4 个单元格,获取该单元格的值并显示一个消息框 “落后的 cell.value 和 库存不可用”。


我累了,请帮帮我。

【问题讨论】:

  • 你需要展示你迄今为止尝试过的内容,然后人们可以帮助你修改。

标签: excel vba if-statement excel-2019


【解决方案1】:

选择表格单元格的方法有几种,这里有一种'

Option Explicit

Sub CheckStock()
    Dim r As Long, n As Long, i As Integer, p As Integer
    Dim s As String

    With Sheet1.ListObjects("Table1")
        i = .ListColumns("In stock").Index
        p = .ListColumns("Product Name").Index

        For r = 1 To .DataBodyRange.Rows.Count
            n = .DataBodyRange(r, i)
            If n > 0 And n < 2 Then
                s = s & vbCrLf & .DataBodyRange(r, p)
            End If
        Next
    End With
    If Len(s) > 0 then 
        MsgBox "Products not available :" & s, vbExclamation
    End If
End Sub

【讨论】:

  • 非常感谢。但是这里有个问题 Msgbox 在函数之外。因此,Msgbox 始终可以正常工作。但是我想在库存低于 2 时启用 Msgbox。否则,Msgbox 被禁用。
  • @saiful 例如,如果有 3 件商品缺货,您是否希望 msgbox 提醒 3 次?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
相关资源
最近更新 更多