[英]Macro stops after Workbook.Open
I'm trying to copy open a workbook, copy a range and paste it into another workbook. 我试图复制打开一个工作簿,复制一个范围并将其粘贴到另一个工作簿中。 After the workbook is opened and the range selected the macro stops. 打开工作簿并选择范围后,宏将停止。
If I use Workbook.close later in the code, the macro stops at the same point but the the workbook is not even opened. 如果稍后在代码中使用Workbook.close,则宏会在同一点停止,但工作簿甚至不会打开。
I saw in other threads the "shift key" problem when using Workbook.Open and this is not the case. 我在其他线程中看到了使用Workbook.Open时出现的“ Shift键”问题,事实并非如此。
Sub importSpecialist()
Dim ws As Worksheet
Set ws = Worksheets("myworksheet")
Dim lws As Integer
lws = ws.Range("B3").End(xlDown).Row
Dim savePath As String
Dim saveName As String
Dim saveMonth As String
Dim fileExtension As String
Dim fullPath As String
Dim version As Integer
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
savePath = "C:\mypath\"
saveName = "myname_"
saveMonth = Format(Date, "mm")
fileExtension = ".xlsx"
fullPath = savePath & saveName & saveMonth & fileExtension
Debug.Print fullPath
If FSO.fileexists(fullPath) Then
Dim i As Integer
Dim wb1 As Workbook
Set wb1 = Workbooks.Open(fullPath)
wb1.Sheets(1).Range("C4:C18").Copy
For i = 1 To lws
On Error Resume Next
If ws.Range("B" & (i + 2)) = Format(Date, "mm/yyyy") Then
ws.Cells(Range("N" & (i + 2))).PasteSpecial
End If
Next
End If
End Sub
Try to open it like this : 尝试像这样打开它:
Dim xl As New Excel.Application
Application.DisplayStatusBar = False
Application.DisplayAlerts = False
xl.Workbooks.Open (Worksheets("MAIN").Range("B7").Value)//your path
xl.Visible = False //or true
//your code here
xl.Quit
Set xl = Nothing
''and if you what to select a range from it use xl.Worksheets().Range().Select '',如果您要从中选择范围,请使用xl.Worksheets()。Range()。Select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.