作者ptguitar (佩蒂吉他)
看板Office
標題[問題] 關閉檔案出錯的巨集
時間Tue Feb 7 10:12:01 2017
軟體:excel
版本:任何版本
我弄了個巨集excel檔案
可以開啟一個檔案把資料複製過來
但是我想要開啟那個檔案後 複製好後就關閉這個檔案
但是關閉都會出錯誤
不知道哪邊寫錯了?
問題檔案
https://drive.google.com/open?id=0BwCgrQhGgaw7V2Mxdy0wSXVfRWs
原始巨集:
Sub 匯入()
If (MsgBox("是否執行檔案匯入?", vbYesNo) = vbNo) Then Exit Sub
Dim abc, xyz As String
Dim 本年月, 匯率 As Variant
Dim thiswrkname$
本年月 = InputBox("輸入本年月,格式:10512")
Sheets("設定").Range("H1") = 本年月
匯率 = InputBox("輸入匯價")
Sheets("設定").Range("H3") = 匯率
thiswrkname = ThisWorkbook.Name
abc = Sheets("原始資料").Range("C1").End(xlDown).Row + 1
filetoopen = Application.GetOpenFilename("allfiles, *.*")
If filetoopen <> False Then
Workbooks.Open (filetoopen)
End If
'貼上開啟的檔案
Sheets("彙總").Range("A6:ZT73").Select
Selection.Copy
Workbooks(thiswrkname).Activate
Worksheets("原始資料").Activate
Range("C" & abc).Select
Selection.PasteSpecial Paste:=xlPasteValues
xyz = Range("C1").End(xlDown).Row
Range("B" & abc) = Sheets("設定").Range("H1")
Range("B" & abc).AutoFill Range("B" & abc & ":B" & xyz)
Range("A" & abc - 1).AutoFill Range("A" & abc - 1 & ":A" & xyz)
Workbooks(filetoopen).Activate
Workbooks(filetoopen).Close SaveChanges:=False
End Sub
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.34.252.11
※ 文章網址: https://www.ptt.cc/bbs/Office/M.1486433524.A.D83.html
→ soyoso: 如有可能filetoopen是未選取檔案的話,if..end if的部分請 02/07 10:39
→ soyoso: 含括註解'貼上開啟的檔案以下的部分 02/07 10:41
→ soyoso: 或如msgbox寫法,filetoopen判斷為false時exit sub 02/07 10:43
→ ptguitar: 可以了 感謝大大 真的幫忙很大 !! 02/07 11:02