Excel “运行时错误”;“424”节:将数据从一个工作簿复制到活动工作簿时所需的对象

Excel “运行时错误”;“424”节:将数据从一个工作簿复制到活动工作簿时所需的对象,excel,vba,object,runtime-error,Excel,Vba,Object,Runtime Error,晚上好 我一直在从事以下项目,该项目产生了几个424运行时错误 此工作簿中声明了以下公用子项: Public Sub Workbook_Open() Dim rolandPath As String 'Declares global string variable rolandFile for RoLanD's filepath Dim rolandSource As Workbook ' declares global workbook variable rolandSource for R

晚上好

我一直在从事以下项目,该项目产生了几个424运行时错误

此工作簿中声明了以下公用子项:

Public Sub Workbook_Open()

Dim rolandPath As String 'Declares global string variable rolandFile for RoLanD's filepath
Dim rolandSource As Workbook ' declares global workbook variable rolandSource for RoLanD listed in rolandPath
Dim destinationWorkbook As Workbook 'declares global workbook variable destinationWorkbook for Extract Tool
Dim storeID As Integer ' Declares global integer variable storeID for Store's 4 digit ID
Dim tFocus As Long 'Declares global long variable tFocus for current Tab in focus
Dim cFocus As Long 'Declares global long variable cFocus for current Column in focus
Dim rFocus As Long 'Declares global long variable rFocus for current Row in focus
Dim rRecord As Long 'Declares global long variable rRecord for current Row being recorded to

Call DataConnect

End Sub
此代码的其余部分在模块1中:

Sub DataConnect()
MsgBox ("Please select your RoLanD file. If you are asked to enter your password, please do this. If you select Cancel, you may need to exit and restart this application.") 'Explains to the user what to do next.
FilePath = Application.GetOpenFilename 'Opens dialogue for user to select RoLanD file
    If FilePath <> "" Then 'Checks that the filepath is completed
       rolandPath = FilePath 'Stores the filepath for RoLanD into the variable rolandFile
    End If
storeID = InputBox("Please enter your Store's four digit ID (EG: 0123). If you enter this incorrectly, this may result in your colleagues records being incorrectly applied", "Enter Store ID") ' Prompts user to enter store ID, then stores it as integer storeID
MsgBox ("Thank you. Your current RoLanD data will now be copied. This will leave your data intact in RoLanD, so please do not be concerned. Please select OK to continue.") 'Gives a message to reassure user.

cFocus = "Q" 'Sets the Column in focus to Column Q
rFocus = 5 'Sets the Row in focus to the fifth one
rRecord = 1 'Sets the row the record is being recorded to as the first one
tFocus = 1 'Sets the Tab in focus to the first one

Set rolandSource = Workbooks.Open(rolandPath)

On Error Resume Next ' tells DB to move on when error is reached
Do
    Call tInFocus
    tFocus = tFocus + 1 'Adds tab (worksheet) in focus up one
Loop Until Err.Number <> 0 'breaks when no more worksheets available

MsgBox ("Data is fully copied.")
End Sub

Sub tInFocus()
cFocus = "Q" 'Sets the Column in focus to Column Q
rFocus = 5 'Sets the Row in focus to the fifth one
If rolandSource.Worksheets(tFocus).Range("Q4").Value = "" Then Exit Sub 'Ends subroutine if Q4 is empty, which means no data on this RoLanD tab
Do
    Do
        ThisWorkbook.Worksheets("1").Range("A" & rRecord).Value = rolandSource.Sheets(tFocus).Range("B" & rFocus) 'Copies employee number in focus to destination
        ThisWorkbook.Worksheets("1").Range("B" & rRecord).Value = rolandSource.Worksheets(tFocus).Range(cFocus & "4") ' Copies learning title in focus to destination
        ThisWorkbook.Wksheets("1").Range("C" & rRecord).Value = rolandSource.Worksheets(tFocus).Range(cFocus & rFocus) 'Copies learning completion date in focus to destination
        ThisWorkbook.Worksheets("1").Range("D" & rRecord).Value = storeID 'Copies store ID to row in focus
        rRecord = rRecord + 1 'Moves line being recorded to on one
        cFocus = Chr(Asc(cFocus) + 1) 'Moves column in focus up one
    Loop Until rolandSource.Worksheets(tFocus).Range(cFocus & 4) = "" 'Breaks loop when end column reached
    rFocus = rFocus + 1 'Moves row in focus up one
    cFocus = "Q" 'Resets cFocus to Column Q
Loop Until rolandSource.Worksheets(tFocus).Range(B & rFocus) = "" 'Breaks loop when end row reached

End Sub
这产生了同样的结果

如有任何想法或支持,将不胜感激。我在一段长时间的休息后重新开始编码,我知道我可能在做一些非常愚蠢的事情,但不知道是什么


感谢您的期待,Dan感谢@TimWilliams,他指出我没有正确声明变量。此外,使用chr(asc更改列的错误在“z”之后不起作用(当您考虑它时,很明显),因此我将更改为使用cell()引用。

all您在
Workbook\u Open
中声明的变量仅对该过程可见:它们将不可访问(例如)
Dataconnect
。在每个模块的顶部添加
Option Explicit
,如果您试图为“未声明的”变量赋值,它会提醒您hanks@TimWilliams这对我帮助很大!我现在得到了预期的输出,但是当我点击Loop直到rolandSource.Worksheets(tFocus).Range(cFocus&4)=“”,代码正在中断,显示“运行时错误1004:应用程序定义的或对象定义的错误”-我尝试了“”和NULL,但两者似乎都不起作用(事实上NULL会导致一个无限循环,我必须按住CTRL键并中断).有什么想法吗?
tFocus
cFocus
出错时的值是什么?谢谢@TimWilliams,这就是问题所在!我使用了chr(asc以增量方式向上移动列-这在“z”之后不起作用,错误发生在cFocus为“[”时。我用cell()而不是range()重写了所以我可以通过数字位置来参考。
rolandSource.Activate
rolandSource.Worksheet(tFocus).Cell("B" & rFocus).Copy
ThisWorkbook.Activate
ThisWorkbook.Worksheet(1).Call("A" & rRecord).Paste