我正在学习让我的代码更短一些,我对这种情况有疑问
wsPipe.Cells(i, 2).value = Split(key, "; ")(2) & Split(key, "; ")(3) & Split(key, "; ")(6) & Split(key, "; ")(7)
我如何尝试让它变得更短:
wsPipe.Cells(i, 2).value = Split(key, "; ")(2,3,6,7)
不工作
wsPipe.Cells(i, 4).value = Join(Array(Split(key, "; ")(2), Split(key, "; ")(3), Split(key, "; ")(6) _
, Split(key, "; ")(7), "; ")
工作但不短
我必须同意这样一个事实,即您可能最好按照 @Chill60 他的评论将
Split()
的结果分配给变量。
为了回答你的问题,有一种(W/H)acky方法可以一次性完成此操作:
Sub Test()
Dim key As String
Dim res As Variant
key = "a; b; t; e; c; d; s; t"
res = Join(Application.Index(Split(key, "; "), Array(3, 4, 7, 8)), "; ")
Debug.Print res
End Sub
请注意,
Application.Index()
是不是,使用0索引,而是从1开始计数!因此,数字数组略有不同。