MsgBox中的打印数组删除尾随逗号

时间:2017-04-07 22:07:23

标签: arrays excel vba excel-vba

我有一个数组通过msgBox函数显示在Join()中。我想知道如何删除在Join数组上使用resultsFinal时出现的尾随逗号。

Function test2(Var As Range)
    Dim result As Integer
    Dim resultsFinal() As String
    ReDim resultsFinal(0)
    Dim i As Integer

    i = 0

    result = 0

    Dim cell As Range
    For Each cell In Var.Cells
            If cell.Value = 25 Or cell.Value = 45 Then
                   result = result + 1
                   ReDim Preserve resultsFinal(result)
                   Dim temp As String

                   resultsFinal(i) = cell.Row
                   i = i + 1
                   test2 = cell.Value
            End If
    Next cell

    Dim resultsFinal1() As String   'here is my try
    resultsFinal1 = resultsFinal  
    ReDim Preserve resultsFinal1(result - 1)  'the length is smaller!
    MsgBox result & " and " & vbNewLine & "array: " & Join(resultsFinal1, ", ")   'still displayes the full array, including the last character (but fortunately somehow doesn't display comma)

End Function

此外,我尝试创建一个resultsFinal1数组,它是一个初始数组减去最后一个元素。虽然resultsFinal1的长度小于resultsFinal的长度,但它仍然存储了最后一个元素,逗号消失了。为什么呢?

4 个答案:

答案 0 :(得分:1)

我认为问题是您过早地增加了result变量。尝试此代码,看看它是否符合您的要求。我基本上删除了resultsFinal1的所有代码,并将result = result + 1移到ReDim数组的resultFinal之后。

Function test2(Var As Range)
    Dim result As Integer
    Dim resultsFinal() As String
    ReDim resultsFinal(0)
    Dim i As Integer

    i = 0

    result = 0

    Dim cell As Range
    For Each cell In Var.Cells
            If cell.Value = 25 Or cell.Value = 45 Then
                   ReDim Preserve resultsFinal(result)
                   result = result + 1
                   Dim temp As String

                   resultsFinal(i) = cell.Row
                   i = i + 1
                   test2 = cell.Value
            End If
    Next cell

    MsgBox result & " and " & vbNewLine & "array: " & Join(resultsFinal, ", ")   'still displayes the full array, including the last character (but fortunately somehow doesn't display comma)

End Function

答案 1 :(得分:1)

首先,你的数组总是大1,因为数组索引从0开始。所以实际上Redim resultsFinal(0)创建一个包含1个元素的数组。

通过交换来自

的行
result = result + 1
ReDim Preserve resultsFinal(result)

ReDim Preserve resultsFinal(result)
result = result + 1

你的数组大小变得正确。

对于第二部分,关键字Join在数组项之间添加字符串,因此当您的数组1更大时,它会在您的最后一个正确元素之间添加,数组和一个空字符串,实际上是数组中的最后一项。

答案 2 :(得分:1)

我不是在我的电脑上,所以这是一个盲目的猜测,但我怀疑这与你没有使用Option Explicit语句的事实有关,所以所有未声明的变量(如" resultsFinal")得到Variant类型的隐式变暗,因此当你为它赋一个数组时,它的第一个索引为零,最后一个索引为"结果" -1,这样当你把它调整为&时#34;结果" -1它没有实际调整大小的效果!

您可能想尝试这个(未经测试的)代码

Option Explicit

Function test2(Var As Range)
    Dim result As Integer
    ReDim resultsFinal(1 to Var.Count) As String

    Dim cell As Range
    For Each cell In Var.Cells
        If cell.Value = 25 Or cell.Value = 45 Then
               result = result + 1
               resultsFinal(result) = cell.Row
        End If
    Next cell
    If result > 1 Then
        ReDim Preserve resultsFinal(1 to result - 1)  'resize the array one element less then those stored
        MsgBox result & " and " & vbNewLine & "array: " & Join(resultsFinal, ", ") 
    Else
        MsgBox "No items found!"
    End If

End Function

答案 3 :(得分:0)

一个没有环的衬里:

Function test2(Var As Range)
test2 = "Found 25 in " & Join(Filter(Application.Transpose(Application.Evaluate("=IF(" & Var.Address & "=25,ROW(" & Var.Address & "),""x"")")), "x", False), ",") & _
    vbNewLine & "Found 45 in " & Join(Filter(Application.Transpose(Application.Evaluate("=IF(" & Var.Address & "=45,ROW(" & Var.Address & "),""x"")")), "x", False), ",")
End Function

测试

Sub TEST()
MsgBox test2(Range("A1:a100"))
End Sub