Excel格式化单元格

我有一张最后有额外空间的date。 我想删除它们,以便我可以格式化它们作为date和sortingExcel表。 我使用了在线的macros,例如:

Sub TrimColumnA() Dim rng As Range On Error Resume Next ''#if entire column is blank, exit sub Set rng = Intersect(Range("B1").EntireColumn, ActiveSheet.UsedRange) rng.Value = Evaluate("IF(ROW(" & rng.Address & "),IF(" & rng.Address & _ "<>"""",TRIM(" & rng.Address & "),""""))") End Sub 

我也试过其他macros,它的工作原理和删除空格,但我必须双击每个单元格,然后他们格式化为date。 我怎样才能避免点击,直接删除空间,并格式化为date。

版本 – Excel 2007

我不认为你在乎细胞是否空了。 也许出于速度考虑,但只有当你使用TRS80

 Sub MakeDates() Dim rCell As Range For Each rCell In Intersect(ActiveSheet.Columns(2), ActiveSheet.UsedRange).Cells If Not rCell.HasFormula Then rCell.Value = Trim(rCell.Value) End If Next rCell End Sub 

这将跳过任何公式,但否则它只是修剪已经在范围内的东西。

所以,你想要做的是修剪它们后select单元格,使格式化生效? 如果是这样的话,我已经在一列数据中使用了这个过程,当我在列的顶部时,我开始了这个macros,并且只要单元格中有东西,我就想下去。

 Sub Double_Click_Cells() Dim iOS As Integer Application.Calculation = xlCalculationManual iOS = 0 While Not IsEmpty(ActiveCell.Offset(iOS)) ActiveCell.Offset(iOS).Formula = ActiveCell.Offset(iOS).Formula iOS = iOS + 1 Wend Application.Calculation = xlCalculationAutomatic End Sub 

这是你需要的吗?

也许我误解了,但不能只是select列,按CTRL-H,并用空格replace空格?

我用下面的代码,并改变它,它是为我的scheme工作。 谢谢大家的回答和时间

 Sub RemoveTrailing() 'Code removes leading and trailing spaces (both ASCII 32 and 160) from a selected range of cells Dim cel As Range, rg As Range Application.ScreenUpdating = False Set rg = Intersect(Selection, ActiveSheet.UsedRange) For Each cel In rg If Not IsError(cel) Then If cel <> "" Then cel.Value = Trim(Application.Substitute(cel.Value, Chr(160), " ")) End If Next cel Application.ScreenUpdating = True End Sub