[英]Macro stops after Workbook.Open
我試圖復制打開一個工作簿,復制一個范圍並將其粘貼到另一個工作簿中。 打開工作簿並選擇范圍后,宏將停止。
如果稍后在代碼中使用Workbook.close,則宏會在同一點停止,但工作簿甚至不會打開。
我在其他線程中看到了使用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
嘗試像這樣打開它:
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
'',如果您要從中選擇范圍,請使用xl.Worksheets()。Range()。Select
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.