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

我有一个数组,通过Join()函数显示在msgBox 。 我想知道如何删除一个结尾逗号,如果我使用了一个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 

此外,我试图创build一个resultsFinal1数组是一个初始数组减去最后一个元素。 它可以工作,但并不像预期的那样 – 虽然resultsFinal1的长度小于resultsFinal1的长度,但它仍然存储最后一个元素,并且逗号消失。 为什么?

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

所以通过交换线路

 result = result + 1 ReDim Preserve resultsFinal(result) 

 ReDim Preserve resultsFinal(result) result = result + 1 

您的数组大小变得正确。

对于第二部分,关键字Join 数组项之间添加一个string,所以当您将数组1放大时,会在数组中的最后一个正确元素和空string之间添加一个空string,这实际上是数组中的最后一项。

我认为问题在于你太早增加了resultvariables。 试试这个代码,看看它是否做你想要的。 我基本上删除了resultsFinal1所有代码, resultsFinal1 result = result + 1移到resultsFinal1数组的ReDim之后。

 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 

我不是通过我的个人电脑,所以这是一个盲目的猜测,但我怀疑这与你没有使用Option Explicit语句的事实有关,因此所有没有声明的variables(如“resultsFinal”)隐式地变暗为Varianttypes因此,当你给它赋值一个数组时,它的第一个索引是零,最后一个索引是“result”-1,所以当你将其大小调整为“result”时,它没有真正的resize的效果!

你可能想试试这个(未经testing的)代码

 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 

一个没有循环的class轮:

 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 

testing

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