将存储为文本的数字转换为每个语句的数字

我试图将存储在文本中的数字转换为多个工作表上的数字。 我的问题是,我拼凑在一起的代码似乎正在花费过多的时间。 我正在使用For Each语句来循环遍历必要的工作表和范围。 它不会使Excel崩溃,它只是看起来永远运行。

Sub ConvertTextToNumber() Application.ScreenUpdating = False Dim WshtNames As Variant Dim WshtNameCrnt As Variant Dim r As Range WshtNames = Array("Financial Data", "Site Data ", "Org Data", "Program Data") For Each WshtNameCrnt In WshtNames On Error Resume Next For Each r In Worksheets(WshtNameCrnt).UsedRange.SpecialCells(xlCellTypeConstants) If IsNumeric(r) Then r.Value = Val(r.Value) Next Next Application.ScreenUpdating = False End Sub 

当我停止运行脚本并单击debugging时,它似乎陷入了第一个Next语句。 我认为我用来转换值的方法只是比必要的更多的时间密集,因此在多张纸上运行它更糟糕。

我愿意接受任何和所有的build议,使这个过程运行得更快。 提前致谢!

尝试下面的代码。 我使用了一个索引号,而不是用一个变体来循环访问数组。 我可能是错的,但我认为For Each只适用于集合。 有人请纠正我,如果我错了。 (编辑:我错了,因为在这里每个作品都很好。)

无论如何,arrays上的索引号码是最佳实践。

我也删除了您的简历下一步,并妥善处理它。 我强烈build议不要使用继续下一步。 我不能想到Resume Next不能被好逻辑取代的事件。

 Sub ConvertTextToNumber() Application.ScreenUpdating = False ' These two statements should further improve processing time. ' The first prevents formulas from calculating. The second prevents ' any background events from firing (mostly for Event triggered macros). Application.Calculation = xlCalculationManual Application.EnableEvents = False Dim WshtNames As Variant Dim i as Long Dim r As Range WshtNames = Array("Financial Data", "Site Data ", "Org Data", "Program Data") ' When looping over an array use an index number. ' I this case, 'i' will go from the lowest range of the array ' all the way through to the highest range of the array. For i = LBound(WshtNames) to Ubound(WshtNames) 'On Error Resume Next ' It is best to catch the errors, dont just skip them. If Not Worksheets(WshtNames(i)) Is Nothing Then For Each r In Worksheets(WshtNames(i)).UsedRange.SpecialCells(xlCellTypeConstants) ' No need to check for an empty string here since ' IsNumeric() will return false for non-numbers. If IsNumeric(r) Then r.Value = Val(r.Value) Next Else ' Put your error handling in here, or you can just skip it ' I tend to use debug.print just to keep track. Debug.Print WshtNames(i) & " doesn't exist." End If Next Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic Application.EnableEvents = True End Sub 

我最初使用布兰登的答案,但根据他的build议,我研究了使用数组来存储值,并在内存中进行更改。 以下是我现在使用的更新的代码:

 Sub ConvertTextToNumber() Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Application.EnableEvents = False Dim WshtNames As Variant Dim DataRange As Variant Dim r As Range Dim i As Long Dim lrow As Long Dim lcol As Integer Dim MyVar WshtNames = Array("Financial Data", "Site Data ", "Org Data", "Program Data") DataRange = Range("A1:FZ6000").Formula For lrow = 1 To 6000 For lcol = 1 To 156 MyVar = DataRange(lrow, lcol) If IsNumeric(MyVar) Then MyVar = Val(MyVar) DataRange(lrow, lcol) = MyVar End If Next lcol Next lrow Range("A1:FZ6000").Formula = DataRange Application.ScreenUpdating = False Application.Calculation = xlCalculationAutomatic Application.EnableEvents = True End Sub 

这对我来说是有效的,因为我知道我的表单将永远不会超出我根据数据的性质select的范围。 这有效地减less了我的计算时间〜2秒。 感谢大家的意见和快乐编码!

那么这是因为你的循环正在经历每个细胞。 最好对空白值进行一些检查

  IF r.Value <> "" or r.value <> vbnullstring then