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

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

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

问题

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

    Dim Cl As Range, Rng As Range
    Dim Cl2 As Range, Rng2 As Range
    Dim Cl3 As Range, Rng3 As Range
    Dim c As Range
    Dim Cl4 As Range, Rng4 As Range
    Dim Lastrow As Long
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "MW*" Then

            For Each Cl In Range("A1:J1")
                Select Case Cl.Value
                    Case "#", "Coupler Detached", "Coupler Attached", "Host Connected", "End Of File", "ms"
                        If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
                End Select
            Next Cl
            If Not Rng Is Nothing Then Rng.EntireColumn.Delete

            For Each Cl4 In Range("D1")
                Select Case Cl4.Value
                    Case "Abs Pres (kPa) c:1 2"
                        If Rng4 Is Nothing Then Set Rng4 = Cl4 Else Set Rng4 = Union(Rng4, Cl4)
                End Select
            Next Cl4
            If Not Rng4 Is Nothing Then
                Application.ScreenUpdating = False

                Lastrow = Cells(Rows.Count, "D").End(xlUp).Row
                For Each c In Range("D2:D" & Lastrow)
                    c.Value = c.Value * 0.101972
                Next
                Application.ScreenUpdating = True

            End If

            For Each Cl2 In Range("A1:J1")
                Select Case Cl2.Value
                    Case "Abs Pres (kPa) c:1 2"
                        If Rng2 Is Nothing Then Set Rng2 = Cl2 Else Set Rng = Union(Rng, Cl2)
                End Select
            Next Cl2
            If Not Rng2 Is Nothing Then Rng2.Value = ("LEVEL")

            For Each Cl3 In Range("A1:J1")
                Select Case Cl3.Value
                    Case "Temp (°C) c:2"
                        If Rng3 Is Nothing Then Set Rng3 = Cl3 Else Set Rng = Union(Rng, Cl3)
                End Select
            Next Cl3
            If Not Rng3 Is Nothing Then Rng3.Value = ("TEMPERATURE")

        End If
    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

    Dim Cl As Range, Rng As Range
Dim Cl2 As Range, Rng2 As Range
Dim Cl3 As Range, Rng3 As Range
Dim c As Range
Dim Cl4 As Range, Rng4 As Range
Dim Lastrow As Long
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "MW*" Then
For Each Cl In Range("A1:J1")
Select Case Cl.Value
Case "#", "Coupler Detached", "Coupler Attached", "Host Connected", "End Of File", "ms"
If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
End Select
Next Cl
If Not Rng Is Nothing Then Rng.EntireColumn.Delete
For Each Cl4 In Range("D1")
Select Case Cl4.Value
Case "Abs Pres (kPa) c:1 2"
If Rng4 Is Nothing Then Set Rng4 = Cl4 Else Set Rng4 = Union(Rng4, Cl4)
End Select
Next Cl4
If Not Rng4 Is Nothing Then
Application.ScreenUpdating = False
Lastrow = Cells(Rows.Count, "D").End(xlUp).Row
For Each c In Range("D2:D" & Lastrow)
c.Value = c.Value * 0.101972
Next
Application.ScreenUpdating = True
End If
For Each Cl2 In Range("A1:J1")
Select Case Cl2.Value
Case "Abs Pres (kPa) c:1 2"
If Rng2 Is Nothing Then Set Rng2 = Cl2 Else Set Rng = Union(Rng, Cl2)
End Select
Next Cl2
If Not Rng2 Is Nothing Then Rng2.Value = ("LEVEL")
For Each Cl3 In Range("A1:J1")
Select Case Cl3.Value
Case "Temp (°C) c:2"
If Rng3 Is Nothing Then Set Rng3 = Cl3 Else Set Rng = Union(Rng, Cl3)
End Select
Next Cl3
If Not Rng3 Is Nothing Then Rng3.Value = ("TEMPERATURE")
End If
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 -->

Sub ALot()
Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
&#39; If it&#39;s not, change back to &#39;ActiveWorkbook&#39;.
Application.ScreenUpdating = False
Dim ws As Worksheet, rng As Range, cell As Range
For Each ws In wb.Worksheets
If ws.Name Like &quot;MW*&quot; Then
&#39; Delete columns.
For Each cell In ws.Range(&quot;A1:J1&quot;).Cells
Select Case CStr(cell.Value)
Case &quot;#&quot;, &quot;Coupler Detached&quot;, &quot;Coupler Attached&quot;, _
&quot;Host Connected&quot;, &quot;End Of File&quot;, &quot;ms&quot;
If rng Is Nothing Then Set rng = cell Else Set rng = Union(rng, cell)
End Select
Next cell
If Not rng Is Nothing Then
rng.EntireColumn.Delete
Set rng = Nothing &#39; reset
End If
&#39; Check 4th column.
Set cell = ws.Range(&quot;D1&quot;)
If CStr(cell.Value) = &quot;Abs Pres (kPa) c:1 2&quot; Then
Set rng = ws.Range(&quot;D2&quot;, ws.Cells(ws.Rows.Count, &quot;D&quot;).End(xlUp))
rng.Value = ws.Evaluate(rng.Address &amp; &quot;*0.101972&quot;)
Set rng = Nothing &#39; reset
End If
&#39; Check &quot;Abs Pres (kPa) c:1 2&quot;.
For Each cell In ws.Range(&quot;A1:J1&quot;).Cells
Select Case CStr(cell.Value)
Case &quot;Abs Pres (kPa) c:1 2&quot;
If rng Is Nothing Then Set rng = cell Else Set rng = Union(rng, cell)
End Select
Next cell
If Not rng Is Nothing Then
rng.Value = &quot;LEVEL&quot;
Set rng = Nothing &#39; reset
End If
&#39; Check &quot;Temp (&#176;C) c:2&quot;.
For Each cell In ws.Range(&quot;A1:J1&quot;).Cells
Select Case cell.Value
Case &quot;Temp (&#176;C) c:2&quot;
If rng Is Nothing Then Set rng = cell Else Set rng = Union(rng, cell)
End Select
Next cell
If Not rng Is Nothing Then
rng.Value = &quot;TEMPERATURE&quot;
Set rng = Nothing &#39; reset
End If
&#39;Else &#39; is not like &quot;MW*&quot;; do nothing
End If
Next ws
Application.ScreenUpdating = True
MsgBox &quot;Worksheets processed.&quot;, vbInformation
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:

确定