0

我遇到了一个Array值的主要问题,这些值在添加到数组时添加了空格。那么有没有办法删除这些空格意味着Trim在单行语句中使用它们,然后再将它们用于进一步处理,否则我需要Trim在每次计算中使用。那么有什么更快的过程可以在code?

Dim VMHArray
VMHArray = Array("VMH All Fields Review - Add Supplier  Emergency", _
                 "VMH All Fields Review  (Req. Provides Supplier Details) - Update/Enable Supplier", _
                 "VMH Triggered Final Sourcing Review - Add Address_Direct", _
                 "Activate / Create supplier in business system - Add Address", _
                 "Activate / Create supplier in Business System - Add Supplier")
4

3 回答 3

3

将值插入到数组中时对其进行修剪。或者,循环遍历数组,修剪每个元素并将修剪后的元素重新插入数组。

于 2012-12-29T20:17:48.720 回答
2

你不能在初始化后清理数组吗,像这样:

Sub test()
   Dim a(2) As String
   a(0) = "t1"
   a(1) = "t2  "
    a(2) = "test3 "
    Dim i As Integer

    Dim trimmed As Variant

    trimmed = TrimArray(a)

     For i = 0 To UBound(trimmed)
         Debug.Print trimmed(i) + "|"
     Next

End Sub

Function TrimArray(ByRef a() As String) As Variant
    For i = 0 To UBound(a)
     a(i) = Trim(a(i))
    Next

    TrimArray = a
End Function
于 2012-12-29T20:18:31.180 回答
1

我的 VBE 不接受数组中的 350 个条目。

但是,对于 VBA,下面的代码使用简化的集合:

Sub main()

Dim VMHArray As Variant

VMHArray = Array("VMH All Fields Review - Add Supplier  Emergency", _
"VMH All Fields Review (Req. Provides Supplier Details) - Update/Enable Supplier", _
"VMH Triggered Final Sourcing Review - Add Address_Direct", _
"Activate - / Create supplier in business system - Add Address", _
"Activate /Create supplier in Business System - Add Supplier", _
"Activate / Create supplier in Business System -Add Supplier Emergency")

VMHArray = TrimArrayElements(VMHArray)

End Sub

Function TrimArrayElements(InputArray As Variant) As Variant

  Dim arr_element As Variant

  For Each arr_element In InputArray
    arr_element = Trim(arr_element)
  Next

End Function
于 2012-12-29T23:00:03.557 回答