代码之家  ›  专栏  ›  技术社区  ›  anky

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

  •  2
  • anky  · 技术社区  · 6 年前

    我有一个 output_file 0001_1234_abcd_defg_U_2018.08.24-14.50.23.TIF

    我使用下面的代码来实现这一点。

     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)
    

    输出\u文件 在一个字段中有两个下划线 0001_1234__abcd_defg_U_2018.08.24-14.50.23.TIF . 上述代码在这种情况下失败。如何处理这种情况。

    2 回复  |  直到 4 年前
        1
  •  3
  •   Nathan_Sav    6 年前

    我希望这有帮助

    Sub test()
    
    Dim a() As String
    
    a = Split(replace("123_456_789","__","_"), "_")
    
    Range("h1").Resize(1, UBound(a) + 1).Value = a
    
    End Sub
    
        2
  •  3
  •   user4039065 user4039065    6 年前

    尝试,

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