拆分字符串但在所有拆分-VBA中考虑1个分隔符

时间:2021-03-14 21:43:25

I have an output_file which looks like 0001_1234_abcd_defg_U_2018.08.24-14.50.23.TIF

我有一个output_file,看起来像0001_1234_abcd_defg_U_2018.08.24-14.50.23.TIF

I am trying to split this file value and paste the splitted values into a table in column C:H starting the immediate available row.

我正在尝试拆分此文件值并将拆分的值粘贴到C列中的表中:H启动立即可用的行。

I am using the below code to achieve this.

我使用以下代码来实现此目的。

 tbl.Range(LastRow, "C").Offset(1).Value = Split(output_file, "_")(0)
 tbl.Range(LastRow, "D").Offset(1).Value = Split(output_file, "_")(1)
 tbl.Range(LastRow, "E").Offset(1).Value = Split(output_file, "_")(2)
 tbl.Range(LastRow, "F").Offset(1).Value = Split(output_file, "_")(3)
 tbl.Range(LastRow, "G").Offset(1).Value = Split(output_file, "_")(4)
 tbl.Range(LastRow, "H").Offset(1).Value = Split(output_file, "_")(5)

But sometimes my output_file has 2 underscores in a feild eg 0001_1234__abcd_defg_U_2018.08.24-14.50.23.TIF. The above code fails in such cases. How to I handle this scenario.

但有时我的output_file在一个字段中有2个下划线,例如0001_1234__abcd_defg_U_2018.08.24-14.50.23.TIF。在这种情况下,上述代码失败。如何处理这种情况。

Thanks in advance. :)

提前致谢。 :)

2 个解决方案

#1


3  

Try,

tbl.Range(LastRow, "C").resize(1, 6).Offset(1, 0) = _
  split(replace(output_file, "__", "_"), "_")

#2


3  

I hope this helps

我希望这有帮助

Sub test()

Dim a() As String

a = Split(replace("123_456_789","__","_"), "_")

Range("h1").Resize(1, UBound(a) + 1).Value = a

End Sub

#1


3  

Try,

tbl.Range(LastRow, "C").resize(1, 6).Offset(1, 0) = _
  split(replace(output_file, "__", "_"), "_")

#2


3  

I hope this helps

我希望这有帮助

Sub test()

Dim a() As String

a = Split(replace("123_456_789","__","_"), "_")

Range("h1").Resize(1, UBound(a) + 1).Value = a

End Sub