代码之家  ›  专栏  ›  技术社区  ›  Aman Singh Dhir

VBA文本到以括号作为拆分器的列

  •  1
  • Aman Singh Dhir  · 技术社区  · 7 年前

    输入:1)VITOL-GUNVORSA,$286.5/mt,2kt,FOB DARM,FE So the first row starting in column D is how I would like it

    这是我目前拥有的代码,它允许我从名字中分割除数字以外的所有内容,即。 Vitol 现在我的问题是,如何添加另一个 OtherChar 所以我也可以按 )

    谢谢

    Private Sub CommandButton1_Click()
    
    
        Dim myRng As Range
        Dim LastRow As Long
    
        LastRow = Sheets("Sheet1").UsedRange.Rows.Count
    
        With Sheets("Sheet1")
            Set myRng = Sheets("Sheet1").Range("A2:A" & LastRow)
        End With
    
    
        myRng.TextToColumns _
          Destination:=Range("D2:E2:F2:G2:H2"), _
          DataType:=xlDelimited, _
          Tab:=False, _
          Semicolon:=False, _
          Comma:=False, _
          Space:=True, _
          Other:=True, _
          OtherChar:="-"
    
    
    
    End Sub
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   BruceWayne    7 年前

    循环遍历范围内的单元格,运行 SUBSTITUTE ,然后对列执行文本转换。

    Private Sub CommandButton1_Click()
    
        Dim myRng As Range
        Dim LastRow As Long
    
        LastRow = Sheets("Sheet1").UsedRange.Rows.Count
    
        With Sheets("Sheet1")
            Set myRng = Sheets("Sheet1").Range("A2:A" & LastRow)
        End With
    
        Dim cel As Range
        For Each cel In myRng
            cel.Value = WorksheetFunction.Substitute(cel.Value, "-", ", ")
        Next cel
    
        myRng.TextToColumns _
                Destination:=Range("D2:E2:F2:G2:H2"), _
                DataType:=xlDelimited, _
                Tab:=False, _
                Semicolon:=False, _
                Comma:=False, _
                Space:=True, _
                Other:=True, _
                OtherChar:=")"
    End Sub
    

    enter image description here