Excel在msgbox中显示变量数据

时间:2012-10-09 03:28:24

标签: vba excel-vba excel

有两列。一个有一些表的名称,第二个有它的值。

Dim rng As Range
    Set rng = [AH3:AH50]  ' <-- adjust to your requirements    
    If Application.WorksheetFunction.Max(rng) > 95 Then    
    MsgBox "tables has sixe more than 95"    
End If   

所需帮助是:
1. Msgbox还应显示它找到的单元格的实际值超过95 2.它还应显示在第一步中显示值的相应表的名称。

msgbox输出应该是:
“ABCD表目前的大小:96.6”

2 个答案:

答案 0 :(得分:2)

此代码假定表名位于表值之前的列中。如果不是这样,请从Offset更改-1参数。

Sub TestRange()
    Dim rngValues As Range
    Dim strTableName As String
    Dim cell As Range

    ' Adjust ranges to suit
    Set rngValues = [C2:C8]

    For Each cell In rngValues
        If cell.Value > 95 Then
            MsgBox Range(cell.Address).Offset(0, -1).Value _
                & " table has current size: " & cell.Value
        End If
    Next
End Sub

答案 1 :(得分:0)

试试这个

更新以查找所有实例&gt;阈值

Sub TestRange()
    Dim rngValues As Range
    Dim rngTables As Range
    Dim mxVal As Variant
    Dim idx As Long

    ' Adjust ranges to suit
    Set rngValues = [F1:F10]
    Set rngTables = [E1:E10]
    mxVal = Application.WorksheetFunction.Max(rngValues)
    Do While mxVal > 95
        idx = Application.Match(mxVal, rngValues, 0)
        MsgBox Application.Index(rngTables, idx) & " table has current size: " & mxVal

        If idx < rngValues.Rows.count Then
            Set rngValues = rngValues.Offset(idx, 0).Resize(rngValues.Rows.count - idx)
            Set rngTables = rngTables.Offset(idx, 0).Resize(rngTables.Rows.count - idx)
            mxVal = Application.WorksheetFunction.Max(rngValues)
        Else
            mxVal = 0
        End If
    Loop
End Sub