[英]How to convert string type array to variant type array - Excel VBA
I am attempting to load formulae stored in a tab-delimited text file into a range in a worksheet.我正在尝试将存储在以制表符分隔的文本文件中的公式加载到工作表中的范围中。 I have used the function
Split(Expression As String, Delimiter)
to correctly load each line in turn into a 1D array, but have run into problems concerning the array type returned.我使用函数
Split(Expression As String, Delimiter)
依次将每一行正确加载到一维数组中,但遇到了有关返回数组类型的问题。
The Split function only returns string type arrays, and I need a variant type array to set the range to. Split 函数只返回字符串类型数组,我需要一个变体类型数组来设置范围。 This is because setting the formulae of cells using a string type array causes the cell values to be set to the raw text, even if the strings begin with an equals sign.
这是因为使用字符串类型数组设置单元格的公式会导致单元格值设置为原始文本,即使字符串以等号开头。
'Example code to demonstrate the problem:
Sub Tester()
Dim StringArr(1 To 3) As String
StringArr(1) = "= 1"
StringArr(2) = "= 2"
StringArr(3) = "= 3"
Range("Sheet1!$A$1:$C$1").Formula = StringArr
'Cells display raw string until edited manually
Dim VariantArr(1 To 3) As Variant
VariantArr(1) = "= 1"
VariantArr(2) = "= 2"
VariantArr(3) = "= 3"
Range("Sheet1!$A$2:$C$2").Formula = VariantArr
'Cells display formula result correctly
End Sub
Resulting output:结果输出:
I would like to know if there is a way to convert the array returned from the Split
function to a variant type array, preferably without loops.我想知道是否有办法将从
Split
函数返回的数组转换为变体类型数组,最好没有循环。 I am aware that I could set each cell formula individually within a loop, but I am trying to keep it as efficient and neat as possible.我知道我可以在一个循环中单独设置每个单元格公式,但我试图让它尽可能高效和整洁。
I am using Microsoft Excel for Mac 2011, Version 14.5.5.我使用的是 Microsoft Excel for Mac 2011,版本 14.5.5。 VBA is driving me up the wall.
VBA 把我逼上了绝路。
You can use WorksheetFunction.Index
(or Application.Index
) to convert the array of String to array of Variant/String :您可以使用
WorksheetFunction.Index
(或Application.Index
)将String数组转换为Variant/String数组:
Sub Test()
StringArr = Split("=1 =2 =3")
VariantArr = WorksheetFunction.Index(StringArr, 1, 0)
Range("Sheet1!$A$1:$C$1").Formula = StringArr
Range("Sheet1!$A$2:$C$2").Formula = VariantArr
End Sub
Here are array types:以下是数组类型:
And expected output:和预期的输出:
I created a text file with a few formulas in there.我创建了一个文本文件,里面有几个公式。 I used the "|"
我用了“|” character instead of ",", or tab delimited.
字符而不是“,”或制表符分隔。 You can use the find & replace function in a text editor to replace the four spaces to "|"
您可以使用文本编辑器中的查找和替换功能将四个空格替换为“|” if you can.
如果可以的话。
Then I created this code in VBA然后我在 VBA 中创建了这段代码
Sub loadFormula()
Dim fso As Object, textFile As Object: Set fso = CreateObject("Scripting.FileSystemObject")
Dim textFileStr As String
Dim textFileArr As Variant
Dim outputArr() As Variant
Dim oneRow As Variant
Dim numRows, numColumns As Long
If Dir("C:\Users\dawsong4\Documents\Reports\WIP\formula.txt") = "" Then
Exit Sub
Else
potentialFileToLoad = "C:\Users\dawsong4\Documents\Reports\WIP\formula.txt"
End If
Set textFile = fso.OpenTextFile(potentialFileToLoad, 1)
textFileStr = textFile.ReadAll
textFile.Close
Set textFile = Nothing
Set fso = Nothing
textFileArr = Split(textFileStr, Chr(10))
numRows = UBound(textFileArr)
numColumns = UBound(Split(textFileArr(0), "|"))
ReDim outputArr(numRows, numColumns)
For ii = 0 To (numRows - 1)
oneRow = Split(textFileArr(ii), "|")
For jj = 0 To numColumns
outputArr(ii, jj) = oneRow(jj)
Next jj
Next ii
Worksheets("Data").Range("A2:P1048576").ClearContents
Worksheets("Data").Range("A2").Resize(numRows + 1, numColumns).Value = outputArr
End Sub
And the result in excel was this: excel中的结果是这样的:
Hope that helps!希望有帮助!
PS I imagine if you use " " four spaces (however many a tab delimited text file has), instead of "|" PS我想如果你使用“”四个空格(无论有多少制表符分隔的文本文件),而不是“|” it will work the same
它会一样工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.