打开工作簿,转录数据并关闭工作簿...... Workbooks.Open似乎无法正常工作

时间:2017-09-15 21:56:21

标签: excel-vba vba excel

我正在尝试使用Excel 2010中的VBA执行一项简单的任务,这给我带来了许多问题,我希望专家可以帮助我。我是VBA的新手并且没有接受过正式的训练,所以很可能我只是在某个地方犯了一个愚蠢的错误。我试图打开一个工作簿(Source.xlsx),然后从该工作簿中获取数据(Sheet1!A1:E1)并将其转录到当前工作簿(Target.xlsm)Sheet1!A5:A9。我创建了Source和Target工作簿,所以我可以尝试解决这个问题,但是实际的" Target"工作簿将从多个工作簿中提取数据。我打算将文件名传递给函数并使用该文件名而不是像#34; Source.xlsx"这样的名称进行硬编码。这是我到目前为止的代码:

    Public Function TranscribeFromClosedWb(SourcePath As String, SourceWb As String, SourceWs As String)
0       'Exits Function if an Error is thrown
1       On Error GoTo ErrHandler
2
3       'Stops Excel from updating the screen so that the Function runs faster and is more aesthetic to the User
4       'Application.ScreenUpdating = False
5
6       'Dimensionalizes the Arrays and counting variables to be used in the function
7       Dim SourceCell(1 To 5) As String
8       Dim cellData(1 To 5) As Variant
9       Dim TargetCell(1 To 5) As String
10      Dim i As Integer
11
12      'Establishes the cell references for each item in the Arrays.  5 shown here for example.  Arbitrary values in SourceCells and no values in TargetCells
13      SourceCell(1) = "A1"
14      SourceCell(2) = "B1"
15      SourceCell(3) = "C1"
16      SourceCell(4) = "D1"
17      SourceCell(5) = "E1"
18      cellData(1) = 42
19      cellData(2) = 42
20      cellData(3) = 42
21      cellData(4) = 42
22      cellData(5) = 42
23      TargetCell(1) = "A5"
24      TargetCell(1) = "A6"
25      TargetCell(1) = "A7"
26      TargetCell(1) = "A8"
27      TargetCell(1) = "A9"
28
29      'Dimensionalizes an Excel Object as a Workbook to hold the Source Workbook open while data gathering and function codes are proceeding
30      Dim SourceWbObject As Workbooks
31
32      'Simply checks to ensure Source file actually exists, and exits if it does not
33      If Dir(SourcePath & SourceWb) = "" Then
34          MsgBox ("File " & Chr(34) & SourcePath & SourceWb & Chr(34) & " was not found.")
35          Exit Function
36      End If
37
38      'Dimensionalizes a string variable to hold the complete filepath, and sets the Excel Workbook Object to the Source Workbook
39      Dim SourceWbPath As String
40      SourceWbPath = SourcePath & SourceWb
41      MsgBox ShiftPressed()
42      
43      Set SourceWbObject = Workbooks.Open(SourceWbPath, True, True)
44      'MsgBox ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
45      'SourceWbObject
46      MsgBox ThisWorkbook.Worksheets("Sheet1").Range("A1").Value & Chr(13) & Workbooks.Count
47
48      'Sets up a For loop that is supposed to cycle through the cells in the SourceCell Array and set the values gathered to items in the cellData Array
49      i = 1
50      For i = 1 To 5
51          cellData(i) = ThisWorkbook.Worksheets("Sheet1").Range(SourceCell(i)).Value
52          'cellData(i) = ThisWorkbook.Worksheets("Sheet1").Range(SourceCell(i)).Value
53          i = i + 1
54      Next
55
56
57      'Sets up a For loop that transcribes the values gathered in the above For loop to the items in the TargetCell Array
58      i = 1
59      Workbooks("Target.xlsm").Activate
60      With ThisWorkbook.Worksheets("Sheet1").Activate
61          For i = 1 To 5
62              ActiveCell.Offset(0, i).Activate
63              ActiveCell.Value = cellData(i)
64              i = i + 1
65          Next
66      End With
67
68      'Basic Housekeeping: Closes the Source Workbook, and clears the Excel Object that held it.
69      'SourceWbObject.Close False
70      'Set SourceWbObject = Nothing
71
72
73      'Error Handling stub: Simply restores EnableEvents properties (which may have been disabled by the Error), and restores screen updating prior to exiting function
74 ErrHandler:
75      Dim Msg As String
76      Msg = "Error # " & Str(Err.Number) & " was generated by " & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
77      MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
78      Application.EnableEvents = True
79      Application.ScreenUpdating = True
End Function

我尝试了很多不同的Workbooks.Open迭代,但似乎没有任何效果。当我开始这个时,我不知道如何在MsgBox中获取错误信息,所以我使用MsxBox向我提供了我可以用来尝试找出发生了什么的信息。第43行之后的MsgBox应该告诉我Excel具有正确的工作簿作为活动工作簿(它没有)并且它成功打开了源工作簿,如.Count指示从1到2(它没有&也可以这样做。我也尝试了不同的东西,因为我正在尝试最大限度地减少我捕获的错误数量,现在勾选的是我在寻求帮助之前尝试的最后一次迭代。我还勾选了ScreenUpdating以及所有这些,所以我可以在视觉上看到它在做什么,显然我会恢复一旦我的代码工作...我收到多个不同的错误取决于我尝试,最常见的是错误91:对象变量或With Block变量未设置,但我不理解这一点,因为代码在With块之前停止,我在第43行上有一个Set语句。我也得到了429:ActiveX组件无法在同一行创建对象。这里的一个帖子声明这可能是由于Excel认为Shift键被按下而导致的错误,因此我复制了一些代码,如果按下shift键并且在Open行之前显示一个MsgBox并且它总是返回False,则返回true所以我不认为这是一个问题。多个来源,Excel提示,Excel先生,MSDN,以及所有状态,Workbooks.Open应该完全按照我的意愿行事,但我无法弄清楚为什么它不适合我...对不起,请帮忙!感谢您的时间和考虑!

1 个答案:

答案 0 :(得分:0)

您的SourceWbObject应声明为WorkBook而不是WorkBooks。 然后将ThisWorkBook替换为SourceWbObject应该这样做。