修剪工作簿中的所有单元格(VBA)

我已经试图添加function到一个Excel的插件,一直在发展,在使用单元格的末尾修剪的领先空间,甚至可能parsing文本,我需要这样做的原因是简单地把它变成一个超链接我已经工作,但部分罚款。

这是我迄今为止所尝试的,我已经修剪active.worksheet是在哪个是好的,但我不知道如何:

  1. 修剪在整个工作簿中使用的每个单元格。
  2. 如果可能的话也parsing文本

这是我在修整整个工作簿的尝试,其简单的我只知道它,我只是不知道:

 Sub DoTrim(Wb As Workbook) Dim cell As Range Dim str As String Dim nAscii As Integer Dim wsh As Worksheet For Each wsh In Worksheets With wsh.UsedRange For Each cell In ActiveSheet.UsedRange str = Trim(cell) If Len(str) > 0 Then nAscii = Asc(Left(str, 1)) If nAscii < 33 Or nAscii = 160 Then If Len(str) > 1 Then str = Right(str, Len(str) - 1) Else str = "" End If End If End If cell = str Next cell End With Next wsh End Sub 

任何意见将是欢迎这个语言相当新,所以对不起,如果我听起来像一个完整的纽伯!

TL; DR修剪单元格只有工作表上,需要运行整个工作簿我无法弄清楚如何遍历整个事情。

编辑:这也是一个更快的方法来修剪这些单元格,为他们devise的这个电子表格是巨大的,需要一段时间来修剪细胞

未经testing

这是你正在尝试?

 Sub DoTrim(Wb As Workbook) Dim aCell As Range Dim wsh As Worksheet '~~> If you are using it in an Add-In, it is advisable '~~> to keep the user posted :) Application.StatusBar = "Processing Worksheets... Please do not disturb..." DoEvents Application.ScreenUpdating = False For Each wsh In Wb.Worksheets With wsh Application.StatusBar = "Processing Worksheet " & _ .Name & ". Please do not disturb..." DoEvents For Each aCell In .UsedRange If Not aCell.Value = "" And aCell.HasFormula = False Then With aCell .Value = Replace(.Value, Chr(160), "") .Value = Application.WorksheetFunction.Clean(.Value) .Value = Trim(.Value) End With End If Next aCell End With Next wsh Application.ScreenUpdating = True Application.StatusBar = "Done" End Sub 

我同意Siddarth:

 For Each cell In ActiveSheet.UsedRange 

应该:

 For Each cell In wsh.UsedRange 

我本来以为你应该可以用循环中的“With wsh.UsedRange”语句去除。

当你传递一个WorkBook引用时,也许你应该考虑改变你的外部For循环:

 For Each wsh In Worksheets 

至:

 For Each wsh In Wb.Worksheets