英文:
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
变量。
另外,请注意ws
和cell
是“安全”的,它们不需要重置,因为For Each...
可以被理解为类似于Set ws = WhatEverWorksheet
或Set 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 intorng
. Now you deleterng
and the variable's state is stillNot Nothing
although you have deleted the range making it invalid. In the next iteration, you try to combine this invalid range (which isNot Nothing
) with a matching cell viaUnion
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 singlerng
variable is used. - Also, note how
ws
andcell
are 'safe', they don't need to be reset, sinceFor Each...
could be translated as something likeSet ws = WhatEverWorksheet
orSet cell = WhatEverCell
.
<!-- language: lang-vb -->
Sub ALot()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' If it's not, change back to 'ActiveWorkbook'.
Application.ScreenUpdating = False
Dim ws As Worksheet, rng As Range, cell As Range
For Each ws In wb.Worksheets
If ws.Name Like "MW*" Then
' Delete columns.
For Each cell In ws.Range("A1:J1").Cells
Select Case CStr(cell.Value)
Case "#", "Coupler Detached", "Coupler Attached", _
"Host Connected", "End Of File", "ms"
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 ' reset
End If
' Check 4th column.
Set cell = ws.Range("D1")
If CStr(cell.Value) = "Abs Pres (kPa) c:1 2" Then
Set rng = ws.Range("D2", ws.Cells(ws.Rows.Count, "D").End(xlUp))
rng.Value = ws.Evaluate(rng.Address & "*0.101972")
Set rng = Nothing ' reset
End If
' Check "Abs Pres (kPa) c:1 2".
For Each cell In ws.Range("A1:J1").Cells
Select Case CStr(cell.Value)
Case "Abs Pres (kPa) c:1 2"
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 = "LEVEL"
Set rng = Nothing ' reset
End If
' Check "Temp (°C) c:2".
For Each cell In ws.Range("A1:J1").Cells
Select Case cell.Value
Case "Temp (°C) c:2"
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 = "TEMPERATURE"
Set rng = Nothing ' reset
End If
'Else ' is not like "MW*"; do nothing
End If
Next ws
Application.ScreenUpdating = True
MsgBox "Worksheets processed.", vbInformation
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论