VBA宏将单元格函数转换为文本表达式非常慢。

huangapple go评论96阅读模式
英文:

VBA macro to convert cell Function into text expression is very slow

问题

我正在尝试在每个公式前面设置',以便不计算函数并以纯文本显示。

  1. Sub ConvertFormulasToTextForSheet()
  2. Dim ws As Worksheet
  3. Dim rng As Range
  4. Dim cell As Range
  5. Dim sheetName As String
  6. sheetName = "Sheet1"
  7. Set ws = ThisWorkbook.Worksheets(sheetName)
  8. Set rng = ws.UsedRange
  9. For Each cell In rng
  10. cell.Value = "'" & cell.Formula
  11. Next cell
  12. End Sub

执行该函数非常慢,基本上每秒只能转换3-4个。有没有办法加快速度?

英文:

I am trying to set ' in front of every formula so that the function is not evaluated and is displayed as plain text.

  1. Sub ConvertFormulasToTextForSheet()
  2. Dim ws As Worksheet
  3. Dim rng As Range
  4. Dim cell As Range
  5. Dim sheetName As String
  6. sheetName = "Sheet1"
  7. Set ws = ThisWorkbook.Worksheets(sheetName)
  8. Set rng = ws.UsedRange
  9. For Each cell In rng
  10. cell.Value = "'" & cell.Formula
  11. Next cell
  12. End Sub

executing that function is extremly slow, it basically convert 3-4 per seconds.
Is there any way how to speed that up?

答案1

得分: 3

使用数组:

  1. Dim vals As Variant
  2. vals = ws.UsedRange.Formula
  3. If IsArray(vals) Then
  4. Dim i As Long, j As Long
  5. For i = LBound(vals, 1) To UBound(vals, 1)
  6. For j = LBound(vals, 2) To UBound(vals, 2)
  7. vals(i, j) = "'" & vals(i, j)
  8. Next
  9. Next
  10. Else
  11. vals = "'" & vals
  12. End If
  13. ws.UsedRange.Value = vals
英文:

Use an array:

  1. Dim vals As Variant
  2. vals = ws.UsedRange.Formula
  3. If IsArray(vals) Then
  4. Dim i As Long, j As Long
  5. For i = LBound(vals, 1) to UBound(vals, 1)
  6. For j = LBound(vals, 2) to UBound(vals, 2)
  7. vals(i, j) = "'" & vals(i, j)
  8. Next
  9. Next
  10. Else
  11. vals = "'" & vals
  12. End If
  13. ws.UsedRange.Value = vals

答案2

得分: 1

数组比迭代每个公式要高效得多。设置 Application.Calculation = xlCalculationManual 也会提高代码性能。我添加了 Suspend 作为参数。这将使启用和禁用公式变得容易。

用法

SuspendFormula ActiveSheet, True
SuspendFormula ActiveSheet, False

代码

  1. Sub SuspendFormula(Worksheet As Worksheet, Suspend As Boolean)
  2. Dim InitialCalculationState As XlCalculation
  3. Application.ScreenUpdating = False
  4. InitialCalculationState = Application.Calculation
  5. Application.Calculation = xlCalculationManual
  6. Dim Target As Range
  7. Set Target = Worksheet.UsedRange
  8. Dim Data As Variant
  9. If Target.Count = 1 Then
  10. ReDim Data(1 To 1, 1 To 1)
  11. Data(1, 1) = Target.Formula
  12. Else
  13. Data = Target.Formula
  14. End If
  15. Dim r As Long, c As Long
  16. For r = 1 To UBound(Data)
  17. For c = 1 To UBound(Data, 2)
  18. If Suspend Then
  19. If Left(Data(r, c), 1) = "=" Then Data(r, c) = "'" & Data(r, c)
  20. Else
  21. If Left(Data(r, c), 2) = "='" Then Data(r, c) = Mid(Data(r, c), 2)
  22. End If
  23. Next
  24. Next
  25. Target.Formula = Data
  26. Application.Calculation = InitialCalculationState
  27. End Sub
英文:

Arrays are much more efficient that iterating over each formula. Setting Application.Calculation = xlCalculationManual will also give the code a performance boost.
I added Suspend as a parameter. This will make it easy to enable and disable the formulas.

Usage:
> SuspendFormula ActiveSheet, True
> SuspendFormula ActiveSheet, False

Code:

  1. Sub SuspendFormula(Worksheet As Worksheet, Suspend As Boolean)
  2. Dim InitialCalculationState As XlCalculation
  3. Application.ScreenUpdating = False
  4. InitialCalculationState = Application.Calculation
  5. Application.Calculation = xlCalculationManual
  6. Dim Target As Range
  7. Set Target = Worksheet.UsedRange
  8. Dim Data As Variant
  9. If Target.Count = 1 Then
  10. ReDim Data(1 To 1, 1 To 1)
  11. Data(1, 1) = Target.Formula
  12. Else
  13. Data = Target.Formula
  14. End If
  15. Dim r As Long, c As Long
  16. For r = 1 To UBound(Data)
  17. For c = 1 To UBound(Data, 2)
  18. If Suspend Then
  19. If Left(Data(r, c), 1) = "=" Then Data(r, c) = "'" & Data(r, c)
  20. Else
  21. If Left(Data(r, c), 2) = "'=" Then Data(r, c) = Mid(Data(r, c), 2)
  22. End If
  23. Next
  24. Next
  25. Target.Formula = Data
  26. Application.Calculation = InitialCalculationState
  27. End Sub

huangapple
  • 本文由 发表于 2023年7月12日 22:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定