在Select Case中需要对象(错误424)

我在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”:Object不断popup。 该错误不指向任何行,但代码完美的作品,当它不调用的程序。 我无法find一个方法来找出几个小时,也没有从网上searchfind一个想法。 有人可以在这里解释什么是我的程序错误,以及如何解决它?

看你的代码(假设你已经包含了所有东西),看起来你从来没有在子例程中定义BOYGIRLvariables。 添加线Dim BOY As String, Girl As String到两个子,它应该消除这个错误。

正如另一位评论者所指出的那样,S3variables对于后续的子程序来说也是不可用的,因为给定了variables作用域。 您可以将其作为子例程的parameter passing,也可以将variables设为全局。 两种解决scheme都可以工作

一般来说,你似乎在variablesScope中遇到问题。 这个链接可以帮助你理解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 

我会写下你的例程。 注意我将S3ivariables传递给两个例程,并且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