我有一点看,我发现了类似的问题,但它们似乎都是如何将一个列表添加到另一个列表的末尾。
我最近回答了一个问题,我在Excel工作中使用的技术依赖于创建第3列,其中包含公式floodfill来连接col1和amp;每行col2。我认为必须有一个更好的方法来做到这一点,所以在VBA中有一点玩,并从我能找到的问题中得出以下结论。但现在我有几个问题:
有没有办法摆脱设置第三个数组的x量的任意值,无论如何都会被替换? ReDim something maybe
总体上有更好/更简洁的方法来根据他们的位置组合arr1和arr2中的元素,而没有循环通过每个元素吗? (使用内置数组命令或其他)
(道歉,如果这在任何地方重复任何线索,我确实看起来,诚实!)
Private Sub CommandButton1_Click()
Dim arr1() As Variant
Dim arr2() As Variant
Dim arr3() As Variant
Dim element As Variant
Dim pos As Integer
arr1 = Array("ONE", "TWO", "THREE")
arr2 = Array("1111", "2222", "3333")
arr3 = Array("x", "x", "x")
For Each element In arr1
pos = Application.WorksheetFunction.Match(element, arr1, False) - 1
arr3(pos) = arr1(pos) & arr2(pos)
'MsgBox (arr1(pos) & arr2(pos) & arr3(pos))
Next
'Where arr3 will equal ("ONE1111", "TWO2222", "THREE3333")
End Sub
编辑 - 感谢大家的答案,在接下来的几天里给了我大约20件事情可以考虑和使用。
答案 0 :(得分:3)
要扩展我的评论,您可能会为此使用的任何代码将循环遍历数组元素。与内置代码的唯一区别是您无法查看循环代码。下面是一些代码,允许您进行简单的函数调用,并支持任意数量的输入数组。 JoinArrayElements
函数可以满足您的要求,您可以使用它而无需将代码写入"循环遍历元素"每一次。
Public Sub Main()
Dim arr1, arr2
arr1 = Array("ONE", "TWO", "THREE")
arr2 = Array("1111", "2222", "3333")
arr3 = Array("!@!@", "@#@#", "#$#$")
Debug.Print arrayToString(joinArrayElements(arr1, arr2))
Debug.Print arrayToString(joinArrayElements(arr1, arr2, arr3))
End Sub
Public Function arrayToString(arr As Variant) As String
Dim output As String
output = "["
If UBound(arr) - LBound(arr) > 0 Then
output = output & """" & arr(LBound(arr)) & """"
For index = LBound(arr) + 1 To UBound(arr)
output = output & ", " & """" & arr(index) & """"
Next
End If
output = output & "]"
arrayToString = output
End Function
Public Function joinArrayElements(ParamArray args() As Variant) As Variant
'Validation to add:
' Are all the passed parameters actual valid arrays?
' Are they all the same length?
Dim arrayNumber As Long
Dim index As Long
Dim arrOutput() As Variant
ReDim arrOutput(LBound(args(0)) To UBound(args(0)))
For arrayNumber = LBound(args) To UBound(args)
For index = LBound(args(0)) To UBound(args(0))
arrOutput(index) = arrOutput(index) & args(arrayNumber)(index)
Next
Next
joinArrayElements = arrOutput
End Function
此代码的输出为:
[" ONE1111"," TWO2222"," THREE3333"]
[" ONE1111!@!@"," TWO2222 @#@#"," THREE3333#$#$"]
答案 1 :(得分:2)
如果您不需要将arr1保持其原始形式,那么:
arr1 = Array("ONE", "TWO", "THREE")
arr2 = Array("1111", "2222", "3333")
For x =lbound(arr1) to ubound(arr1)
arr1(x) = arr1(x) & arr2(x)
Next
VBA中几乎没有有用的内置数组函数/方法,但循环并不一定是坏事。
你总是可以这样做,但它可能不会很好地扩展,我认为这是一种更糟糕的方法:
Dim r, arr1, arr2
arr1 = Array("ONE", "TWO", "THREE")
arr2 = Array("1111", "2222", "3333")
r = ActiveSheet.Evaluate("={""" & Join(arr1, """,""") & """} & {""" & _
Join(arr2, """,""") & """}")
Debug.Print Join(r, ",") '>> ONE1111,TWO2222,THREE3333
答案 2 :(得分:1)
以下是ReDim
数组的方法。第一次使用ReDim array(size)
对数组进行调整后,您将使用ReDim Preserve array(size)
来保留数组中已有的信息。
Private Sub CommandButton1_Click()
Dim arr1() As Variant, arr2() As Variant, arr3() As Variant
Dim pos As Integer
arr1 = Array("ONE", "TWO", "THREE")
arr2 = Array("1111", "2222", "3333")
ReDim arr3(pos)
For pos = 0 To UBound(arr1)
ReDim Preserve arr3(pos)
arr3(pos) = arr1(pos) & arr2(pos)
Next
End Sub
答案 3 :(得分:1)
您可以选择仅使用UDT。
System.out.println(String.format("%3.3E",223.45654543434));
// "2.235E+02" <---- 9 total characters
System.out.println(String.format("%9.3E",223.45654543434));
// "2.235E+02" <---- 9 total characters
System.out.println(String.format("%12.3E",223.45654543434));
// " 2.235E+02" <---- 12 total characters, 3 spaces
System.out.println(String.format("%12.8E",223.45654543434));
// "2.23456545E+02" <---- 14 total characters
System.out.println(String.format("%16.8E",223.45654543434));
// " 2.23456545E+02" <---- 16 total characters, 2 spaces