简体   繁体   English

如何将多个值放入 1 个变量或多个变量中?

[英]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.为此,您可以使用代码 Dim stringArray() As String 使字符串数组变暗,然后将 strCoaster 的每个值包含在该数组中。 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.如果你愿意,你也可以使用 & vbCrLf & 换行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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