简体   繁体   中英

How can i put many values in 1 variable or in many variables?

Extracting RollerCoasters with Wood Only

            Sub RollerCoaster()

            Dim strType As String
            Dim blnLoop As Boolean
            Dim strCoaster As String

            strType = "Wood"

            Range("A2").Select
            blnLoop = True

            Do Until ActiveCell.Value = ""
                ActiveCell.Offset(1, 0).Select
                strType = ActiveCell.Offset(0, 2).Value

                    If strType = "Wood" Then
                        strCoaster = ActiveCell.Value
                        blnLoop = False
                    Else
                    End If

            Loop

            MsgBox ("The RollerCoaster that are made of Wood are " & strCoaster)

            End Sub

So I created a Sub that finds the rollercoaster made out of wood, and it displays which rollercoaster is made out of wood in a message box. However, how can I make my code display more than one rollercoaster, if I have more on the list. (Look at image)

You could create an array to get each value. To do that you can dim an string array with the code Dim stringArray() As String and then you would include each value of strCoaster in that array. You could also make a fairly small tweak of doing the concatenation in the variable assignment using the code below:

        Sub RollerCoaster()

        Dim strType As String
        Dim blnLoop As Boolean
        Dim strCoaster As String

        strType = "Wood"

        Range("A2").Select
        blnLoop = True

        Do Until ActiveCell.Value = ""
            ActiveCell.Offset(1, 0).Select
            strType = ActiveCell.Offset(0, 2).Value

                If strType = "Wood" Then
                    strCoaster = ActiveCell.Value & " ," & strCoaster
                    blnLoop = False
                Else
                End If

        Loop

        MsgBox ("The RollerCoaster that are made of Wood are " & strCoaster)

        End Sub

If you iteratively concatenate the values you can list all of the values. You can also use & vbCrLf & for line breaks if you want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM