vba 如何删除用数组元素添加的值的空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14085350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove spaces added with a values with the array elements?
提问by Arup Rakshit
I am having a major problem with an Array
values, which are added with spaces when they were added to the array. So is there any way to remove such spaces means using Trim
in a single line of statement, before they are used in further processing, otherwise I would need to use Trim
in every calculations. So any quicker process to remove such spaces one time within the code
?
我有一个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")
回答by Kyle
Trim the values as they are inserted into the array. Or, alternatively, loop over the array, trim each element and reinsert the trimmed element into the array.
在值插入数组时对其进行修剪。或者,或者,循环遍历数组,修剪每个元素并将修剪后的元素重新插入数组。
回答by rene
Can't you clean up the array after initialization, like so:
你不能在初始化后清理数组,像这样:
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
回答by Jüri Ruut
My VBE didn't accept 350 entries in an array.
我的 VBE 不接受数组中的 350 个条目。
However, for VBA works the code below with a reduced set:
但是,对于 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