我在VBA编辑器Sheet2中有以下代码:
Sub Organize_Data()
Dim i As Integer
Dim S2 As Worksheet, S3 As Worksheet
Application.ScreenUpdating = False
Set S2 = ThisWorkbook.Sheets("Sheet2")
Set S3 = ThisWorkbook.Sheets("Sheet3")
S3.Range("A:G").Clear
S2.Range("F:H").Copy Destination:=S3.Range("A:C")
S2.Range("P:P").Copy Destination:=S3.Range("F:F")
S2.Range("K:K").Copy Destination:=S3.Range("G:G")
S3.Columns("A:G").Sort key1:=S3.Range("A2"), _
order1:=xlAscending, Header:=xlYes
S3.Cells(1, 4) = "Name Boy"
S3.Cells(1, 5) = "Name Girl"
Last = S3.Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 2 Step -1
If Len(S3.Cells(i, "A")) = 16 Or Len(S3.Cells(i, "A")) = 18 Then
Boys_Girls1
ElseIf Len(S3.Cells(i, "A")) = 23 Then
Boys_Girls2
Else
S3.Cells(i, "D") = ""
S3.Cells(i, "E") = ""
End If
Next i
End Sub
Sub Boys_Girls1()
BOY = Mid(S3.Cells(i, "A"), 6, 2)
Select Case BOY
Case Is = "AM", "01"
S3.Cells(i, "D") = "Aaron Mitchels"
Case Is = "BP"
S3.Cells(i, "D") = "Brian Parker"
Case Else
S3.Cells(i, "D") = ""
End Select
GIRL = Mid(S3.Cells(i, "A"), 8, 2)
Select Case GIRL
Case Is = "AL"
S3.Cells(i, "E") = "Alexa"
Case Is = "EQ", "02"
S3.Cells(i, "E") = "Elizabeth Queen"
Case Else
S3.Cells(i, "E") = ""
End Select
End Sub
Sub Boys_Girls2()
BOY = Mid(S3.Cells(i, "A"), 10, 2)
Select Case BOY
Case Is = "AM", "01"
S3.Cells(i, "D") = "Aaron Mitchels"
Case Is = "BP"
S3.Cells(i, "D") = "Brian Parker"
Case Else
S3.Cells(i, "D") = ""
End Select
GIRL = Mid(S3.Cells(i, "A"), 12, 2)
Select Case GIRL
Case Is = "AL"
S3.Cells(i, "E") = "Alexa"
Case Is = "EQ", "02"
S3.Cells(i, "E") = "Elizabeth Queen"
Case Else
S3.Cells(i, "E") = ""
End Select
End Sub
该程序的目的是从Sheet2复制数据集并将其粘贴到Sheet3,然后组织格式,如执行升序和标记数据。当我运行程序时,运行时错误'424':所需对象不断弹出。该错误不指向任何行,但代码在不调用过程时完美地工作。我无法找到解决数小时的方法,也找不到在线搜索的想法。有人可以在这里解释我的程序有什么问题以及如何解决它?
答案 0 :(得分:1)
查看您的代码(并假设您已包含所有内容),似乎您从未在子例程的最后一个中定义BOY
和GIRL
变量。将行Dim BOY As String, Girl As String
添加到两个Subs,它应该消除该错误。
正如另一位评论者所指出的,在给定变量范围的情况下,S3变量也不可用于后续子程序。您可以将其作为子例程的参数传递,也可以将变量设为全局。两种解决方案都可以运作
通常,您似乎遇到了变量Scope的问题。这个link可以帮助您了解VBA中的细微差别。就这个特殊问题而言,你确定有必要甚至调用一个单独的例行程序吗?鉴于例程的长度很短,并且因为它们似乎只在一个条件下调用,所以只需在原始例程中移动这些进程就可以同时解决大多数问题
在回应OP的评论时,我已经在下面列出了固定的代码部分:
If Len(S3.Cells(i, "A")) = 16 Or Len(S3.Cells(i, "A")) = 18 Then
Boys_Girls1(S3,i)
ElseIf Len(S3.Cells(i, "A")) = 23 Then
Boys_Girls2(S3,i)
Sub Boys_Girls1(InputSheet As WorkSheet, InputRow As Long)
'Your code here (replace calls to S3 with InputSheet and calls to i with InputRow)
End Sub
Sub Boys_Girls2(InputSheet As WorkSheet, InputRow As Long)
'Your code here (replace calls to S3 with InputSheet and calls to i with InputRow)
End Sub
答案 1 :(得分:1)
我会写下你的例程如下。注意我将S3
和i
变量传递给两个例程,Option Explicit
应该在任何过程之前位于模块的最顶层。
Option Explicit
Sub Organize_Data()
Dim i As Integer
Dim S2 As Worksheet, S3 As Worksheet
Dim Last As Long
Application.ScreenUpdating = False
Set S2 = ThisWorkbook.Sheets("Sheet2")
Set S3 = ThisWorkbook.Sheets("Sheet3")
S3.Range("A:G").Clear
S2.Range("F:H").Copy Destination:=S3.Range("A:C")
S2.Range("P:P").Copy Destination:=S3.Range("F:F")
S2.Range("K:K").Copy Destination:=S3.Range("G:G")
S3.Columns("A:G").Sort key1:=S3.Range("A2"), _
order1:=xlAscending, Header:=xlYes
S3.Cells(1, 4) = "Name Boy"
S3.Cells(1, 5) = "Name Girl"
Last = S3.Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 2 Step -1
If Len(S3.Cells(i, "A")) = 16 Or Len(S3.Cells(i, "A")) = 18 Then
Boys_Girls1 S3, i
ElseIf Len(S3.Cells(i, "A")) = 23 Then
Boys_Girls2 S3, i
Else
S3.Cells(i, "D") = ""
S3.Cells(i, "E") = ""
End If
Next i
End Sub
Sub Boys_Girls1(S3 As Worksheet, i As Integer)
Dim BOY As String, GIRL As String
BOY = Mid(S3.Cells(i, "A"), 6, 2)
Select Case BOY
Case Is = "AM", "01"
S3.Cells(i, "D") = "Aaron Mitchels"
Case Is = "BP"
S3.Cells(i, "D") = "Brian Parker"
Case Else
S3.Cells(i, "D") = ""
End Select
GIRL = Mid(S3.Cells(i, "A"), 8, 2)
Select Case GIRL
Case Is = "AL"
S3.Cells(i, "E") = "Alexa"
Case Is = "EQ", "02"
S3.Cells(i, "E") = "Elizabeth Queen"
Case Else
S3.Cells(i, "E") = ""
End Select
End Sub
Sub Boys_Girls2(Sht As Worksheet, i As Integer)
Dim BOY As String, GIRL As String
BOY = Mid(S3.Cells(i, "A"), 10, 2)
Select Case BOY
Case Is = "AM", "01"
S3.Cells(i, "D") = "Aaron Mitchels"
Case Is = "BP"
S3.Cells(i, "D") = "Brian Parker"
Case Else
S3.Cells(i, "D") = ""
End Select
GIRL = Mid(S3.Cells(i, "A"), 12, 2)
Select Case GIRL
Case Is = "AL"
S3.Cells(i, "E") = "Alexa"
Case Is = "EQ", "02"
S3.Cells(i, "E") = "Elizabeth Queen"
Case Else
S3.Cells(i, "E") = ""
End Select
End Sub