[英]Trim Cells using VBA in Excel
我對Excel中的某些數據有一個簡單的問題。 我有很多數據與從Web表粘貼的前導空格,我想擺脫最初的空間。 我抄了下面的代碼(我對VBA來說是全新的),但似乎沒有用。 當我在調試器中單步執行它時,它看起來像一個無限循環。 任何幫助,將不勝感激!
Sub DoTrim()
For Each cell In Selection.Cells
If cell.HasFormula = False Then
cell = Trim(cell)
End If
Next
End Sub
編輯:看起來TRIM函數遇到了“空格”字符的問題。 這段代碼:
Sub DoTrim()
Dim cell As Range, areaToTrim As Range
Set areaToTrim = Selection.Cells
For Each cell In areaToTrim
cell.Value = "MUPPET"
Next cell
End Sub
更改了單元格的值,所以我猜這是一個非空格的空格! 關於如何擺脫那些的任何想法?
這對我很有用。 它使用一個數組,因此您不會循環遍歷每個單元格。 在大型工作表部分上運行得更快。
Sub Trim_Cells_Array_Method()
Dim arrData() As Variant
Dim arrReturnData() As Variant
Dim rng As Excel.Range
Dim lRows As Long
Dim lCols As Long
Dim i As Long, j As Long
lRows = Selection.Rows.count
lCols = Selection.Columns.count
ReDim arrData(1 To lRows, 1 To lCols)
ReDim arrReturnData(1 To lRows, 1 To lCols)
Set rng = Selection
arrData = rng.value
For j = 1 To lCols
For i = 1 To lRows
arrReturnData(i, j) = Trim(arrData(i, j))
Next i
Next j
rng.value = arrReturnData
Set rng = Nothing
End Sub
如果字符串前面有非打印字符,請嘗試此操作
Option Explicit
Sub DoTrim()
Dim cell As Range
Dim str As String
Dim nAscii As Integer
For Each cell In Selection.Cells
If cell.HasFormula = False Then
str = Trim(CStr(cell))
If Len(str) > 0 Then
nAscii = Asc(Left(str, 1))
If nAscii < 33 Or nAscii = 160 Then
If Len(str) > 1 Then
str = Right(str, Len(str) - 1)
Else
strl = ""
End If
End If
End If
cell=str
End If
Next
End Sub
一次改變對我來說很好 - 第四行應該是:
cell.Value = Trim(cell.Value)
編輯:如果它似乎陷入循環,我會添加
Debug.Print cell.Address
在你的For ... Next
循環中,可以獲得有關正在發生的事情的更多信息。
我還懷疑Trim
只剝離空格 - 你確定你沒有其他類型的非顯示字符嗎?
為此完美地為我工作:
修剪所有選定的單元格。 注意選擇完整的列/行:P。
Sub TrimSelected()
Dim rng As Range, cell As Range
Set rng = Selection
For Each cell In rng
cell = Trim(cell)
Next cell
End Sub
只有對我有用的是這個功能:
Sub DoTrim()
Dim cell As Range
Dim str As String
For Each cell In Selection.Cells
If cell.HasFormula = False Then
str = Left(cell.Value, 1) 'space
While str = " " Or str = Chr(160)
cell.Value = Right(cell.Value, Len(cell.Value) - 1)
str = Left(cell.Value, 1) 'space
Wend
str = Right(cell.Value, 1) 'space
While str = " " Or str = Chr(160)
cell.Value = Left(cell.Value, Len(cell.Value) - 1)
str = Right(cell.Value, 1) 'space
Wend
End If
Next cell
End Sub
無限循環是代碼中更高位置的On Error Resume Next
語句的結果。 現在擺脫它。 如果您的代碼只運行On Error Resume Next
,則需要立即完成大修。
當你在它時,在一個模塊的頂部放置一個Option Explicit
。 它會強制您聲明所有變量,以防止由於錯誤輸入的變量名稱導致的煩人的錯誤。 (您可以修改VBA編輯器首選項以在下次自動設置該選項,強烈建議這樣做。)
代碼的直接問題是單元格是一個對象,並且無法修剪對象。 您想要修剪單元格的文本,因此您必須這樣做:
cell.Text = Trim(cell.Text)
這應該完成你想要做的事情。 我只是在一張地板上測試過它; 如果這不起作用,請告訴我。 LTrim就是你只有領先的空間; 可以使用Trim函數,它可以處理前導和尾隨空格。 將我所擁有的區域中的單元格范圍替換為“A1:C50”,並確保將“Sheet1”更改為您正在處理的工作表的名稱。
Dim cell As Range, areaToTrim As Range
Set areaToTrim = Sheet1.Range("A1:C50")
For Each cell In areaToTrim
cell.Value = LTrim(cell.Value)
Next cell
我會嘗試在沒有VBA的情況下解決這個問題。 只需選擇此空間並在您嘗試擺脫這些空間的工作表上使用替換(更改為空 )。
如果你真的想使用VBA,我相信你可以選擇第一個角色
strSpace = left(range("A1").Value,1)
並以相同的方式在VBA中使用替換功能
Range("A1").Value = Replace(Range("A1").Value, strSpace, "")
要么
for each cell in selection.cells
cell.value = replace(cell.value, strSpace, "")
next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.