简体   繁体   English

循环使用VBA修剪单元

[英]Trim a cell with VBA in a loop

I'm trying to use the trim function without success. 我试图使用修剪功能没有成功。 After searching for the solution on this forum and other sources online I have seen many different approaches. 在此论坛和其他在线资源上搜索解决方案后,我发现了许多不同的方法。

Is there no simple way of trimming a cell in VBA? 在VBA中没有修整单元格的简单方法吗?

What I want is something like this: 我想要的是这样的:

Sub trimloop()

Dim row As Integer

row = 1

Do While Cells(row, 1) <> ""

   Cells(row, 2) = trim(Cells(row, 2))

   row = row + 1

Loop

So that when there is a value in column A (1) the value in column B (2) should be trimmed of any extra spaces. 因此,当A(1)列中有值时,B(2)列中的值应修剪掉多余的空格。 I just cant get this to work for me. 我只是无法让它为我工作。

Appreciate any help/tips! 感谢任何帮助/提示!

Regards Jim 问候吉姆

So i made the code a bit accurate and mistakeproof and it worked. 所以我使代码有点准确和防错,并且有效。

So i can recommend you to double check, if you have correct row and column values, because you probably targeting wrong cells. 因此,我建议您仔细检查一下,如果您具有正确的行和列值,因为您可能定位到错误的单元格。 (cause your code is working) (因为您的代码有效)

Sub trimloop()

Dim row As Integer
Dim currentSheet As Worksheet
Set currentSheet = sheets("Sheet1")

row = 2

Do While currentSheet.Cells(row, 1) <> ""

   currentSheet.Cells(row, 2).Value = Trim(currentSheet.Cells(row, 2).Value)

   row = row + 1

Loop

End Sub

Use Application.WorksheetFunction.Trim(string) 使用Application.WorksheetFunction.Trim(string)

Sub trimloop()

Dim row As Integer

row = 1

With ThisWorkbook.ActiveSheet
Do While .Cells(row, 1) <> ""

   .Cells(row, 2) = Application.WorksheetFunction.Trim(.Cells(row, 2))

   row = row + 1

Loop
End With
End Sub

this is the optimized version of your code, in case of big data sheets: 如果是大数据表,这是代码的优化版本:

Option Explicit

Sub trimloop()

Dim row As Long, max As Long
Dim Data() As Variant

With ThisWorkbook.Sheets(1)

    max = .Cells(1, 1).End(xlDown).row 'this does the same as your code, on first empty cell it stops
    'the following finds the last un-empty cell of column(1):
    'max= .cells(.rows.count,1).end(xlup).row

    'copies values from sheet to memory (is faster for working with later)
    Data = .Range(.Cells(1, 1), .Cells(max, 2)).Value2

    'loop :
    For row = 2 To max + 1

        'work with memory instead of sheet
        Data(row, 2) = Trim(Data(row, 2))
        'for complete delete of all spaces use : = replace( StringName," ", "")

    Next row

    'write back to sheet
    .Range(.Cells(1, 1), .Cells(max, 2)).Value2 = Data

End With

erase Data 'free memory

End Sub

Don't know if this overly simplified... but thought I would simply throw it out there this worked for me. 不知道这是否过于简化...但是我想我只是把它扔出去,对我有用。 The only predecessor step is you assign a "named range" to your workbook/worksheet/dataset ... name a data set and then iterate over the data set with this code 唯一的先前步骤是为工作簿/工作表/数据集分配“命名范围” ...命名数据集,然后使用此代码遍历数据集

Sub forEachLoop()
    For Each cell In Range("yourNamedRange")
        cell.Value = Trim(cell.Value)
    Next cell
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM