在值之前修整空白
我有一个问题修剪空白字符。 当我input一个值并在Excel中的值之前和之后给出空格时,我会看到周围有空格,但是我不应该看到这些额外的空格。
我想删除这些空间,但我不知道如何实现这一点,这里是目前我得到的输出:
ServerName = " testing "
但生成的输出应该是这样的…
ServerName = "testing"
这是我在我的项目中使用的代码:
If Sheets(Itm).Cells(i, 2) = "Required1" Then MsgBox "Enter the value for required field : " & Sheets(Itm).Cells(i, 1) Return End If Value = Replace(Sheets(Itm).Cells(i, 4).Value, vbLf, " ") 'Value = Sheets(Itm).Cells(i, 4).Value
试试这个(未经testing)
Value = Trim(Sheets(Itm).Cells(i, 4).Value) Value = Application.WorksheetFunction.Clean(Value) Print #1, Sheets(Itm).Cells(i, 1) & "=" & Value
这里是一个示例代码,以了解它是如何工作的…
Sub sample() Dim sample As String sample = "Sid" Debug.Print Len(sample) '<~~ This will give you 3 sample = " " & vbNewLine & sample & vbNewLine & vbCrLf & vbLf Debug.Print Len(sample) '<~~ This will give you 11 sample = Trim(sample) Debug.Print Len(sample) '<~~ This will give you 10 sample = Application.WorksheetFunction.Clean(sample) Debug.Print Len(sample) '<~~ This will give you 3 End Sub
更换:
Print #1, Sheets(Itm).Cells(i, 1) & "=" & Value
附:
Print #1, Replace(Sheets(Itm).Cells(i, 1) & "=" & Value," ","")