首页 > 解决方案 > 如何仅使用循环和 if 语句制作此消息框?

问题描述

在此处输入图像描述

        Sub RollerCoaster()

        Dim strCoaster As String
        Dim strType As String
        Dim blnFound As Boolean
        Dim intWood As Integer

        Range("A2").Select


        strType = "Wood"
        blnFound = False


        Do Until ActiveCell.Value = ""
        strCoaster = ActiveCell.Offset(1, 0).Value
            If ActiveCell.Offset(0, 2).Value = strType Then
                strCoaster = ActiveCell.Value
                MsgBox ("The RollerCoaster made out of woods is " & strCoaster)
                blnFound = True
                Exit Do
            Else
                ActiveCell.Offset(1, 0).Select
            End If
        Loop

        End Sub

我知道如何只为 1 个过山车循环,但我怎样才能在消息框中获取所有 Wood 类型的过山车?

标签: vbaloopsconditional-statements

解决方案


像这样的东西(不需要选择/激活)

Sub RollerCoaster()

    Dim strType As String
    Dim c As Range, msg 

    strType = "Wood"
    msg = ""

    Set c = ActiveSheet.Range("A2") 
    Do While Len(c.Value) > 0 = ""
        If c.Offset(0, 2).Value = strType Then
            msg  = msg & vbLf & c.Value
        End If
        Set c = c.Offset(1, 0)
    Loop

    If Len(msg) > 0 Then Msgbox "Found matches:" & msg

End Sub

推荐阅读