晚安。
我一直在研究以下项目,该项目产生了多个424运行时错误。
在ThisWorkbook中声明了以下public sub:
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
此代码的其余部分位于Module1:
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
我有两个问题;第一个是循环直到Err.Number&lt;&gt; 0 在第一个实例后结束。当tFocus处于不存在工作表的值时,这应该结束循环。
第二个也是更大的问题是,以下行在调试窗口中生成运行时错误&#34; 424&#34 ;: Object Required 错误消息:
如果是rolandSource.Worksheets(tFocus).Range(&#34; Q4&#34;)。值=&#34;&#34;然后退出Sub
ThisWorkbook.Worksheets(&#34; 1&#34;)。范围(&#34; A&#34;&amp; rRecord).Value = rolandSource.Sheets(tFocus).Range(&#34; B&#34;&amp; rFocus)&#39;将员工人数重点转移到目的地
ThisWorkbook.Worksheets(&#34; 1&#34;)。范围(&#34; B&#34;&amp; rRecord).Value = rolandSource.Worksheets(tFocus).Range(cFocus&amp;& #34; 4&#34;)&#39;将学习标题复制到目的地
ThisWorkbook.Wksheets(&#34; 1&#34;)。范围(&#34; C&#34;&amp; rRecord).Value = rolandSource.Worksheets(tFocus).Range(cFocus&amp; rFocus )&#39;将学习完成日期复制到目的地
ThisWorkbook.Worksheets(&#34; 1&#34;)。范围(&#34; D&#34;&amp; rRecord).Value = storeID&#39;将商店ID复制到焦点行< /强>
循环直到rolandSource.Worksheets(tFocus)。范围(cFocus&amp; 4)=&#34;&#34; &#39;到达结束列时中断循环
循环直到rolandSource.Worksheets(tFocus)。范围(B&amp; rFocus)=&#34;&#34; &#39;在到达结束行时中断循环
我已经阅读了几篇文章并尝试了不同的方法,包括更改将数据复制到以下内容的行:
rolandSource.Activate
rolandSource.Worksheet(tFocus).Cell("B" & rFocus).Copy
ThisWorkbook.Activate
ThisWorkbook.Worksheet(1).Call("A" & rRecord).Paste
这产生了相同的结果。
非常感谢任何想法或支持。我在休息后回到编码,知道我可能做了一些非常愚蠢的事情,但无法弄清楚是什么!
感谢期待,Dan
答案 0 :(得分:0)
感谢@TimWilliams,他指出我没有正确地声明我的变量。另外,使用chr(asc更改列的错误并不会超过&#34; z&#34;(显然当你考虑它时),所以我将更改为使用cell()进行引用。