从列中的单元格中删除多余的空格
Remove extra spaces from cells in column
我编写了以下代码来执行库存扫描条码,但由于某种原因,当我扫描条码时,它在单元格中添加了额外的空格,结果没有按预期显示。
如何从列中的单元格中删除多余的空格?
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count > 1 Or IsEmpty(Target) Or Target.Column <> 1 Then Exit Sub
If Not SheetExists("WarehouseInventory") Then Exit Sub
Dim result As Variant
Set result = Sheets("WarehouseInventory").Cells.Range("E:E").Find(Target)
If result Is Nothing Then
Target.Worksheet.Cells(Target.Row, 2) = "Data Maybe Bin #?"
Else
Target.Worksheet.Cells(Target.Row, 2) = result.Worksheet.Cells(result.Row, 4)
Target.Worksheet.Cells(Target.Row, 3) = result.Worksheet.Cells(result.Row, 5)
Target.Worksheet.Cells(Target.Row, 4) = result.Worksheet.Cells(result.Row, 6)
Target.Worksheet.Cells(Target.Row, 5) = result.Worksheet.Cells(result.Row, 7)
End If
End Sub
Public Function SheetExists(SheetName As String) As Boolean
Dim ws As Worksheet
SheetExists = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name = SheetName Then SheetExists = True
Next ws
End Function
将在 A 列扫描条码
这是 trim 额外 space 单元格的代码。
Dim cell As Range
For Each cell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
cell = WorksheetFunction.Trim(cell)
Next cell
以上代码将 Trim ActiveSheet 中的所有单元格。
Select 适合您想要 trim 的单元格,然后在其上应用 Trim(单元格)。
when I scan the barcode it is add extra spaces in the cells and the result are not showing up as expected.
这个想法不是trim所有单元格,而是trim扫描时的条形码条目。这是你想要的吗?把这个放在相关sheet的代码区。我假设条形码将在 Col B 到 E 中扫描。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
'~~> Assuming that the bar code is scanned in B to E
'~~> If it is Just one column like B then change
'~~> The code below to
'~~> If Not Intersect(Target, Columns("B:B")) Is Nothing Then
If Not Intersect(Target, Columns("B:E")) Is Nothing Then
Target.Value = Trim(Target.Value)
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
我编写了以下代码来执行库存扫描条码,但由于某种原因,当我扫描条码时,它在单元格中添加了额外的空格,结果没有按预期显示。
如何从列中的单元格中删除多余的空格?
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count > 1 Or IsEmpty(Target) Or Target.Column <> 1 Then Exit Sub
If Not SheetExists("WarehouseInventory") Then Exit Sub
Dim result As Variant
Set result = Sheets("WarehouseInventory").Cells.Range("E:E").Find(Target)
If result Is Nothing Then
Target.Worksheet.Cells(Target.Row, 2) = "Data Maybe Bin #?"
Else
Target.Worksheet.Cells(Target.Row, 2) = result.Worksheet.Cells(result.Row, 4)
Target.Worksheet.Cells(Target.Row, 3) = result.Worksheet.Cells(result.Row, 5)
Target.Worksheet.Cells(Target.Row, 4) = result.Worksheet.Cells(result.Row, 6)
Target.Worksheet.Cells(Target.Row, 5) = result.Worksheet.Cells(result.Row, 7)
End If
End Sub
Public Function SheetExists(SheetName As String) As Boolean
Dim ws As Worksheet
SheetExists = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name = SheetName Then SheetExists = True
Next ws
End Function
将在 A 列扫描条码
这是 trim 额外 space 单元格的代码。
Dim cell As Range
For Each cell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
cell = WorksheetFunction.Trim(cell)
Next cell
以上代码将 Trim ActiveSheet 中的所有单元格。 Select 适合您想要 trim 的单元格,然后在其上应用 Trim(单元格)。
when I scan the barcode it is add extra spaces in the cells and the result are not showing up as expected.
这个想法不是trim所有单元格,而是trim扫描时的条形码条目。这是你想要的吗?把这个放在相关sheet的代码区。我假设条形码将在 Col B 到 E 中扫描。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
'~~> Assuming that the bar code is scanned in B to E
'~~> If it is Just one column like B then change
'~~> The code below to
'~~> If Not Intersect(Target, Columns("B:B")) Is Nothing Then
If Not Intersect(Target, Columns("B:E")) Is Nothing Then
Target.Value = Trim(Target.Value)
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub