简体   繁体   English

修剪数据,其中一些单元格包含空间,某些单元格包含公式

[英]Trim data where some cells contain space, some contain formula

I have a large amount data in an Excel file. 我在Excel文件中有大量数据。 I have over 3000 rows which cross A to CZ columns. 我有超过3000列横越A到CZ列。

Before I start using the data, I need to trim all of it. 在开始使用数据之前,需要修剪所有数据。

When I use VBA it takes a long time and I get error "Type not match". 当我使用VBA时,会花费很长时间,并且出现错误“类型不匹配”。

Some cells contain space, some contain formula, some contain formula link with another Excel file. 一些单元格包含空间,一些单元格包含公式,一些单元格包含与另一个Excel文件的链接。

Worksheets("Sheet1").Activate
For Each cell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
    If cell.HasFormula = False Then
        cell.Value = Trim(cell)
    End If
Next cell

It want to trim the cells which do not have a formula but it gets the error. 它想要修整没有公式的单元格,但是会收到错误。

Text-to-Columns, Fixed width, Finish will quickly remove leading/trailing spaces. 文本到列,固定宽度,完成将快速删除前导/后缀空格。

dim i as long

with Worksheets("Sheet1")
    for i=.cells(1, .columns.count).end(xltoleft).column to 1 step -1
        with .columns(i)
            .cells.TextToColumns Destination:=.cells(1), DataType:=xlFixedWidth, _
                                 FieldInfo:=Array(0, 1)
        end with
    next i
end with

BTW, if you want to reduce multiple interim spaces (eg data data to data data ) to a single space, you need to use Application.Trim(...) not VBA.Trim(...) . 顺便说一句,如果您想将多个临时空间(例如, data datadata data )减少到一个空间,则需要使用Application.Trim(...)而不是VBA.Trim(...)

Worksheets("Sheet1").Activate
For Each cell In ActiveSheet.UsedRange
If Not IsError(cell) Then
cell = WorksheetFunction.Trim(cell)
End If
Next cell

finally i use this code to ignore the error. 最后,我使用此代码来忽略该错误。 i know it is not a good method. 我知道这不是一个好方法。

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

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