2013-08-01 69 views

回答

1

如果「 - 」是一致的分隔符,那麼它應該非常簡單。

下面是一些命令,你可以使用:

Strings and Manipulations

編輯:添加簡單的代碼

Sub textuptodash() 
    i = 1 'start on row 1 
    Do While Not IsEmpty(Cells(i, 1)) 'do until cell is empty 
     If Not InStr(1, Cells(i, 1), "-") = 0 Then 'if dash in cell 
      Cells(i, 1) = Left(Cells(i, 1), InStr(1, Cells(i, 1), "-") - 1) 'change cell contents 
     End If 
     i = i + 1 'increment row 
    Loop 
End Sub 
0

嘗試使用Split如下:

Sub MySplit() 

    Dim rngMyRange As Range, rngCell As Range 
    Dim strTemp() As String, strDel As String, strTest As String 
    Dim lngCnt As Long 
    Set rngMyRange = Range("A1:A4") 

    For Each rngCell In rngMyRange 
     ' Split the test based on the delimiter 
     ' Store entries in a vector of strings 
     strTemp = Split(rngCell.Text, "-") 
     ' Reset cell value and intermmediate delimiter 
     rngCell.Value = vbNullString 
     strDel = vbNullString 
     ' Scan all entries. store all of them but not the last -* part 
     For lngCnt = LBound(strTemp) To UBound(strTemp) - 1 
      rngCell = rngCell & strDel & strTemp(lngCnt) 
      ' If we enter the loop again we will need to apend a "-" 
      strDel = "-" 
     Next lngCnt 
    Next rngCell 
End Sub 

歐tput的:

Red 
Blue 
Purple 
White-US 

這是不完全清楚你想怎麼最後一個條目被分割:假設你要刪除的最後一個「 - *」只位。要僅保留第一部分,請註釋掉For lngCnt循環。

我希望這有助於!

相關問題