【发布时间】:2020-11-06 00:13:19
【问题描述】:
我正在努力解决 VBA 中的拆分功能。尽管我尝试使用Dim as Variant 和Dim as String 切换,但也许我的声明有问题。
我的代码如下:
'Split the txtString variable at every "|" and add every split string item to an array split_sText
Dim txtString as String
.
.
.
Dim split_sText() As String
split_sText() = Split(txtString, "|")
Pick the first part of respective item out of that array and place it at the right cell
Sheets(Table1).Cells(1, 1) = Split(split_sText(15), "_")'
split_sText(15) 看起来像这样:“ABC_1234”。在那个字符串中,我只想得到“ABC”。
由于“运行时错误 13 不匹配类型”,错误发生在最后一行,这很奇怪,因为我已将变量声明为字符串。
感谢您的帮助!
【问题讨论】:
-
Split(split_sText(15), "_")的结果将是一个包含 2 个元素的数组。如果您只想要ABC部分,请尝试Sheets(Table1).Cells(1, 1) = Split(split_sText(15), "_")(0) -
这确实是一个非常好的观点。我试过你的想法,但不幸的是它不起作用。用字符串
"abc_1234"替换split_sText(15)仍然会触发相同的错误。