VBA Excel将string数组元素转换为长“types不匹配”
所以我有一个简单的问题,我试图解决。 我有一串数字,我正在阅读作为一个string。 我然后想要将string转换为一个长,并存储在一个Longtypes的数组中。
我最初尝试了这个:
ElseIf stArray(0, i) = "SrcWth" Then Debug.Print "Found the Width Column" For j = 0 To UBound(stArray) whArray(0, j) = CLng(stArray(j, i)) Next j
stArray是保存CSV文件信息的二维数组。 它是stringtypes。 whArray是将要保存我想要的两列数字的二维数组。 但是,它抱怨types不匹配。
然后我想也许是看到一些空白,所以我试着修剪它。
ElseIf stArray(0, i) = "SrcWth" Then Debug.Print "Found the Width Column" For j = 0 To UBound(stArray) whArray(0, j) = CLng(Trim(stArray(j, i))) Next j
这没有什么区别。 我究竟做错了什么? 我想象一下CLng有问题,所以我不确定。
谢谢
你传递给CLng
第一件事是"SrcWth"
。
我想这是列标题,所以你想在1开始你的下一个循环
For j = 1 To UBound(stArray, 1) whArray(0, j) = CLng(Trim(stArray(j, i))) Next j
你也可以包括一个检查,如果它是一个数字string
For j = 1 To UBound(stArray, 1) If Not isNumeric(Trim(stArray(j, i))) Then MsgBox "invalid value: """ & Trim(stArray(j, i)) & """ at position " & j & ", " & i Else whArray(0, j) = CLng(Trim(stArray(j, i))) End If Next j
编辑:事实certificate,该数组包含空string,因为它已被初始化为数据太大。 尝试使用CLng
转换空string也会返回types不匹配。