繁体   English   中英

Excel格式化单元格

[英]Excel formatting cells

我的工作表末尾有多余的空格。 而且我想删除它们,以便我可以将它们格式化为日期并对Excel工作表进行排序。 我使用了在线提供的宏,例如:

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

我还尝试了其他宏,它可以工作并删除空格,但是我必须双击每个单元格,然后将它们格式化为日期。 我如何避免单击并直接删除空间并将其格式化为日期。

版本-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

这将跳过任何公式,但否则只会修剪范围内的现有值。

因此,您要做的就是在修剪单元格之后选择它们,以便格式化生效? 如果是这样的话,这就是我在数据列中用于此目的的过程,当我位于相关列的顶部时,只要该单元格中有内容,我就应该向下移动宏。

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

这是您需要的吗?

也许是我误会了,但是您不能只选择该列,按CTRL-H并用任何内容替换空格吗?

我使用了以下代码并对其进行了更改,它适用于我的方案。 谢谢大家的回答和时间

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

暂无
暂无

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

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