【发布时间】:2019-11-21 06:33:38
【问题描述】:
我想执行以下操作: '计算活动工作表上表格第 1 列中的所有非空白单元格。 '将此单元格数量保存为变量“TransQty”。 '如果计数为 0,则消息“此选项卡上没有项目被标记为过渡”。 '如果计数大于 0,是/否消息“'TransQty' 项目将从该选项卡转换。您要继续吗?” '如果“否”,结束。 '如果“是”,继续执行其余代码。
我想使用表格列的标题,而不是列位置。这样添加和删除列不会影响代码的功能。
Sub Transition_from_Queue()
Dim TransRange As Range
Dim TransQty As Integer
Set TransRange = Worksheets("Project Queue").DateBodyRange("TableQueue[Transition]")
For Each TransRange In Selection
If Application.WorksheetFunction.CountA(TransRange) Then
TransQty = TransQty + 1
End If
Next TransRange
If TransQty = 0 Then
MsgBox "No projects on this tab are marked for transition."
Else
If TransQty > 0 Then
MsgBox Range("TransQty") & "projects will be transitioned from this tab." & vbNewLine & "Would you like to continue?"
End If
End If
在尝试了多种不同方式的代码后,我发现了几个问题。 1)我没有正确识别表格列,无法弄清楚我做错了什么。我想使用列标题,而不是列位置。 2)无论我在目标列的0个单元格还是100个单元格中都有文本,我一直收到TransQty = 0的消息。
【问题讨论】:
-
什么是
DateBodyRange?看起来像是一个错字,但更重要的是DataBodyRange是ListObject的属性,而不是Worksheet。
标签: vba counting listobject