如何阻止我的VBA代码出现错误424?

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

How can I stop my vba code from giving me an error 424?

问题

以下是您的代码的翻译部分:

  1. Dim Cl As Range, Rng As Range
  2. Dim Cl2 As Range, Rng2 As Range
  3. Dim Cl3 As Range, Rng3 As Range
  4. Dim c As Range
  5. Dim Cl4 As Range, Rng4 As Range
  6. Dim Lastrow As Long
  7. Dim ws As Worksheet
  8. For Each ws In ActiveWorkbook.Worksheets
  9. If ws.Name Like "MW*" Then
  10. For Each Cl In Range("A1:J1")
  11. Select Case Cl.Value
  12. Case "#", "Coupler Detached", "Coupler Attached", "Host Connected", "End Of File", "ms"
  13. If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
  14. End Select
  15. Next Cl
  16. If Not Rng Is Nothing Then Rng.EntireColumn.Delete
  17. For Each Cl4 In Range("D1")
  18. Select Case Cl4.Value
  19. Case "Abs Pres (kPa) c:1 2"
  20. If Rng4 Is Nothing Then Set Rng4 = Cl4 Else Set Rng4 = Union(Rng4, Cl4)
  21. End Select
  22. Next Cl4
  23. If Not Rng4 Is Nothing Then
  24. Application.ScreenUpdating = False
  25. Lastrow = Cells(Rows.Count, "D").End(xlUp).Row
  26. For Each c In Range("D2:D" & Lastrow)
  27. c.Value = c.Value * 0.101972
  28. Next
  29. Application.ScreenUpdating = True
  30. End If
  31. For Each Cl2 In Range("A1:J1")
  32. Select Case Cl2.Value
  33. Case "Abs Pres (kPa) c:1 2"
  34. If Rng2 Is Nothing Then Set Rng2 = Cl2 Else Set Rng = Union(Rng, Cl2)
  35. End Select
  36. Next Cl2
  37. If Not Rng2 Is Nothing Then Rng2.Value = ("LEVEL")
  38. For Each Cl3 In Range("A1:J1")
  39. Select Case Cl3.Value
  40. Case "Temp (°C) c:2"
  41. If Rng3 Is Nothing Then Set Rng3 = Cl3 Else Set Rng = Union(Rng, Cl3)
  42. End Select
  43. Next Cl3
  44. If Not Rng3 Is Nothing Then Rng3.Value = ("TEMPERATURE")
  45. End If
  46. Next ws
英文:

I've written a code that does many things. Essentially it loops through each worksheets starting with MW, in these sheets, it deletes some columns, does some operations and changes some column names. Right now, The code works, but after adding the loop through worksheets, I get an error 424 on the "If not Rng Is Nothing Then Rng.EntireColumn.Delete"

How can I fix this?

My theory is that my ws loop doesn't work well, so the code can't work since the sheet is already processed

Here's my code

  1. Dim Cl As Range, Rng As Range
  2. Dim Cl2 As Range, Rng2 As Range
  3. Dim Cl3 As Range, Rng3 As Range
  4. Dim c As Range
  5. Dim Cl4 As Range, Rng4 As Range
  6. Dim Lastrow As Long
  7. Dim ws As Worksheet
  8. For Each ws In ActiveWorkbook.Worksheets
  9. If ws.Name Like "MW*" Then
  10. For Each Cl In Range("A1:J1")
  11. Select Case Cl.Value
  12. Case "#", "Coupler Detached", "Coupler Attached", "Host Connected", "End Of File", "ms"
  13. If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
  14. End Select
  15. Next Cl
  16. If Not Rng Is Nothing Then Rng.EntireColumn.Delete
  17. For Each Cl4 In Range("D1")
  18. Select Case Cl4.Value
  19. Case "Abs Pres (kPa) c:1 2"
  20. If Rng4 Is Nothing Then Set Rng4 = Cl4 Else Set Rng4 = Union(Rng4, Cl4)
  21. End Select
  22. Next Cl4
  23. If Not Rng4 Is Nothing Then
  24. Application.ScreenUpdating = False
  25. Lastrow = Cells(Rows.Count, "D").End(xlUp).Row
  26. For Each c In Range("D2:D" & Lastrow)
  27. c.Value = c.Value * 0.101972
  28. Next
  29. Application.ScreenUpdating = True
  30. End If
  31. For Each Cl2 In Range("A1:J1")
  32. Select Case Cl2.Value
  33. Case "Abs Pres (kPa) c:1 2"
  34. If Rng2 Is Nothing Then Set Rng2 = Cl2 Else Set Rng = Union(Rng, Cl2)
  35. End Select
  36. Next Cl2
  37. If Not Rng2 Is Nothing Then Rng2.Value = ("LEVEL")
  38. For Each Cl3 In Range("A1:J1")
  39. Select Case Cl3.Value
  40. Case "Temp (°C) c:2"
  41. If Rng3 Is Nothing Then Set Rng3 = Cl3 Else Set Rng = Union(Rng, Cl3)
  42. End Select
  43. Next Cl3
  44. If Not Rng3 Is Nothing Then Rng3.Value = ("TEMPERATURE")
  45. End If
  46. Next ws

答案1

得分: 1

循环中的对象变量

主要问题是您不能将无效范围与单元格组合,因此您需要将rng*变量“重置”,即明确将它们设置为Nothing。例如,在第一次迭代中,有单元格组合到rng中。现在您删除了rng,但变量的状态仍然为“Not Nothing”,尽管您已经删除了使其无效的范围。在下一次迭代中,您尝试将此无效范围(仍为“Not Nothing”)与匹配的单元格通过Union组合,因此会发生错误。顺便说一下,即使您没有删除范围,仍然会发生错误,因为不能组合来自不同工作表的范围。
在以下代码中,注意每个工作表的每个阶段之后如何执行此操作(Set rng = Nothing)时,只使用单个rng变量。
另外,请注意wscell是“安全”的,它们不需要重置,因为For Each...可以被理解为类似于Set ws = WhatEverWorksheetSet cell = WhatEverCell

英文:

Object Variables in Loops

  • The main issue was that you cannot combine cells with an invalid range so you need to 'reset' the rng* variables i.e. explicitly set them to nothing. For example, in the first iteration, there were cells combined into rng. Now you delete rng and the variable's state is still Not Nothing although you have deleted the range making it invalid. In the next iteration, you try to combine this invalid range (which is Not Nothing) with a matching cell via Union so the error occurs. BTW, even if you didn't delete the range, again, an error would occur because you cannot combine ranges from different worksheets.
  • In the following code, pay attention to how this (Set rng = Nothing) is done after each stage per worksheet when only a single rng variable is used.
  • Also, note how ws and cell are 'safe', they don't need to be reset, since For Each... could be translated as something like Set ws = WhatEverWorksheet or Set cell = WhatEverCell.

<!-- language: lang-vb -->

  1. Sub ALot()
  2. Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
  3. &#39; If it&#39;s not, change back to &#39;ActiveWorkbook&#39;.
  4. Application.ScreenUpdating = False
  5. Dim ws As Worksheet, rng As Range, cell As Range
  6. For Each ws In wb.Worksheets
  7. If ws.Name Like &quot;MW*&quot; Then
  8. &#39; Delete columns.
  9. For Each cell In ws.Range(&quot;A1:J1&quot;).Cells
  10. Select Case CStr(cell.Value)
  11. Case &quot;#&quot;, &quot;Coupler Detached&quot;, &quot;Coupler Attached&quot;, _
  12. &quot;Host Connected&quot;, &quot;End Of File&quot;, &quot;ms&quot;
  13. If rng Is Nothing Then Set rng = cell Else Set rng = Union(rng, cell)
  14. End Select
  15. Next cell
  16. If Not rng Is Nothing Then
  17. rng.EntireColumn.Delete
  18. Set rng = Nothing &#39; reset
  19. End If
  20. &#39; Check 4th column.
  21. Set cell = ws.Range(&quot;D1&quot;)
  22. If CStr(cell.Value) = &quot;Abs Pres (kPa) c:1 2&quot; Then
  23. Set rng = ws.Range(&quot;D2&quot;, ws.Cells(ws.Rows.Count, &quot;D&quot;).End(xlUp))
  24. rng.Value = ws.Evaluate(rng.Address &amp; &quot;*0.101972&quot;)
  25. Set rng = Nothing &#39; reset
  26. End If
  27. &#39; Check &quot;Abs Pres (kPa) c:1 2&quot;.
  28. For Each cell In ws.Range(&quot;A1:J1&quot;).Cells
  29. Select Case CStr(cell.Value)
  30. Case &quot;Abs Pres (kPa) c:1 2&quot;
  31. If rng Is Nothing Then Set rng = cell Else Set rng = Union(rng, cell)
  32. End Select
  33. Next cell
  34. If Not rng Is Nothing Then
  35. rng.Value = &quot;LEVEL&quot;
  36. Set rng = Nothing &#39; reset
  37. End If
  38. &#39; Check &quot;Temp (&#176;C) c:2&quot;.
  39. For Each cell In ws.Range(&quot;A1:J1&quot;).Cells
  40. Select Case cell.Value
  41. Case &quot;Temp (&#176;C) c:2&quot;
  42. If rng Is Nothing Then Set rng = cell Else Set rng = Union(rng, cell)
  43. End Select
  44. Next cell
  45. If Not rng Is Nothing Then
  46. rng.Value = &quot;TEMPERATURE&quot;
  47. Set rng = Nothing &#39; reset
  48. End If
  49. &#39;Else &#39; is not like &quot;MW*&quot;; do nothing
  50. End If
  51. Next ws
  52. Application.ScreenUpdating = True
  53. MsgBox &quot;Worksheets processed.&quot;, vbInformation
  54. End Sub

huangapple
  • 本文由 发表于 2023年2月9日 00:06:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388549.html
匿名

发表评论

匿名网友

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

确定