我在vba中編寫代碼以取出一個字符串並刪除所有出現的單個空格,同時保留連續的空格。如何從字符串VBA中刪除單個空格(不刪除連續空格)
這就是我現在所擁有的,但它只是刪除所有空間並用短劃線替換它們。
感謝您的任何幫助或指導!
Sub Main
'Nothing happens when the code executes the following string
CombineText("Job Hours Pay Labor %")
'When the following executes it should look like this
'Major-Group-Total 382 2,085.25
CombineText("Major Group Total 382 2,085.25")
End Sub
Sub CombineText(searchString As String)
a = Len(searchString)
For n = 1 To a
If Mid(searchString, n, 1) = Chr(32) Then
searchString = Application.Substitute(searchString, Mid(searchString, n, 1), "-")
End If
Next n
End Sub
檢查當前的字符是一個空格。如果是,請檢查右側和左側的角色。如果這兩者都不是空格,則用 - 替換它,如果是空格,則不執行任何操作。 – nhgrif