我目前正在撰寫一個子程式,允許我使用 Excel 作業表中的資料將 PDF 保存在檔案夾中,但是當我運行代碼時,在預期的檔案夾中找不到 PDF。
這是子程式:
(我對此很陌生,對任何錯誤表示歉意)
Sub createfile()
'Declare worksheet
Dim ws As Worksheet
Set ws = ActiveSheet
' declare location to save file as string
Dim sStoreFile As String
sStoreFile = "L:\My Documents\ExcelTestDocument"
' declare filename variable as string
Dim sFilename As String
' declare number of files to create
Dim iFileCount As Integer
iFileCount = Range("A1").End(xlDown).Row - 1
Dim i As Integer
For i = 1 To iFileCount
' set the value of the file from spreadsheet
sFilename = ws.Range("g" & i 1).Value
' If extension has "pdf" then save as PDF
If Right(sFilename, 3) = "pdf" Then
ws.Range("A" & iFileCount 5).Clear
ws.Range("A" & iFileCount 5).Value = sFilename
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFilename
ActiveSheet.ExportAsFixedFormat Type:=xlType
End If
Next i
MsgBox "All files have been completed"
End Sub
我原本希望在子例程中提到的檔案夾中找到 PDF 檔案,但即使在子例程完成后,系統提示我使用 MSG 框提示所有檔案都已創建,我仍然找不到任何檔案
uj5u.com熱心網友回復:
您必須將 sStoreFile 添加到檔案名:
' set the value of the file from spreadsheet
sFilename = sStoreFile & "\" & ws.Range("g" & i 1).Value
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/533423.html
標籤:擅长vba
