我试图弄清楚如何遍历我的工作表的第一列并取出空格,以便我可以使用VLOOKUP。不确定如何在VBA中执行此操作。这就是我所拥有的:
我无法弄清楚为什么现在不进入下一张表?我不能只是循环遍历所有的表格,因为它们是不同的。
Sub trima()
Dim x As Integer
Dim numrows As Long
numrows = Range("A1",Range("A1").End(xlDown)).Rows.Count
Range("A1").Select
For x = 1 To numrows
Application.WorksheetFunction.trim (ActiveCell)
ActiveCell.Offset(1, 0).Select
Next
End Sub
答案 0 :(得分:2)
你走了:
Sub TrimA()
Dim v
v = [transpose(transpose(trim(a1:index(a:a,match("",a:a,-1)))))]
[a1].Resize(UBound(v)) = v
End Sub
<强>更新强>
如果您想要更新多张图纸,可以像上面这样使用以上内容:
Sub DoTrims()
Sheet1.Activate: TrimA
Sheet2.Activate: TrimA
'etc.
End If
答案 1 :(得分:1)
使用Range.TextToColumns method应该快速清除包含前导/尾随空格的所有单元格。
此程序可以快速将 text-like-like-numbers 转换为真实数字。
Dim c As Long
With Range("A1").CurrentRegion `<~~ set to the desired range of one or more columns
For c = 1 To .Columns.Count
.Columns(c).TextToColumns Destination:=.Columns(c), _
DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)
Next c
End With
如果单元格实际上包含非标准间距(如非破坏空间(在从网页复制的数据中常见),则应添加其他Range.Replace method。
答案 2 :(得分:1)
Trim
功能不起作用。
相反,尝试类似:
Sub trima()
Dim numrows As Long
Dim vItem as Variant
Dim i As Long
numrows = Range("A1",Range("A1").End(xlDown)).Rows.Count
Application.ScreenUpdating = False
With ActiveSheet
For i = 1 To numrows
vItem = .Range("A" & i)
If vItem <> vbNullString then .Range("A" & i) = Application.WorksheetFunction.Trim(vItem)
Next
End With
Application.ScreenUpdating = True
End Sub
以下代码将遍历工作簿中的所有工作表,并对A列中的值执行相同的修剪:
Sub trimA()
Dim ws As Excel.Worksheet
Dim i As Long, numrows As Long
Dim vItem As Variant
Application.ScreenUpdating = False
For Each ws In Worksheets
With ws
numrows = .Range("A1", .Range("A1").End(xlDown)).Rows.Count
For i = 1 To numrows
vItem = .Range("A" & i)
If vItem <> vbNullString Then .Range("A" & i) = Application.WorksheetFunction.Trim(vItem)
Next i
End With
Next
Application.ScreenUpdating = True
End Sub