VBA根据位于单元格中的保存path创build一个文件夹

我有位于J2中的文件保存path,所以我想有一个macros,在J2中的位置创build一个文件夹,如果该文件已经创build结束进程,并循环到我的其他代码创buildPDF和保存每个一个到那个位置。 我已经有这个代码工作。 我将粘贴下面两个:

这是我正在基于单元格中的位置创build文件夹的第一个代码

Sub MakeMyFolder() Dim FldrName As String On Error Resume Next Set fsoFSO = CreateObject("Scripting.FileSystemObject") If fsoFSO.FolderExists = Range("J2") Then MsgBox "found it" Else fsoFSO.CreateFolder = Range("J2") MsgBox "Done" End If End Sub 

这是我已经工作的第二个代码,在J2中创build并保存PDF的位置

 Sub PDF_Generator() Dim cell As Range Dim wsSummary As Worksheet Dim counter As Long Set wsSummary = Sheets("SUMMARY BY PROVIDER") For Each cell In Worksheets("NAME KEY").Range("$H2:$H60") If cell.Value <> "Exclude" Then 'progress in status bar counter = counter + 1 Application.StatusBar = "Processing file: " & counter & "/1042" With wsSummary .Range("$B$8").Value = cell.Value .ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=ThisWorkbook.Sheets("SUMMARY BY PROVIDER").Range("J2").Value & _ "\" & cell.Value & ".pdf", _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End With End If Next cell Set wsSummary = Nothing End Sub 

我想要得到的第一个代码工作比结合下一个过程,任何有关这个问题的见解将是伟大的!

这些都是函数,所以它们有一个返回值。 试试像这样:

 fsoFSO.FolderExists(Range("J2")) 

代替

 fsoFSO.FolderExists = Range("J2") 

对于CreateFolder一样的:

 Sub MakeMyFolder(strFolder as string) Set fsoFSO = CreateObject("Scripting.FileSystemObject") If fsoFSO.FolderExists(strFolder) Then MsgBox "found it" Else fsoFSO.CreateFolder(strFolder) MsgBox "Done" End If End Sub 

要合并它们,请向你的sub MakeMyFolder(strFolder as string)添加一个参数,
我假设你的单元格可以排除将有文件夹path,所以调用你的子作为参数; MakeMyFolder cell.Value from your PDF_generate sub。

 Sub MakeMyFolder(strFolder as string) Set fsoFSO = CreateObject("Scripting.FileSystemObject") If fsoFSO.FolderExists(strFolder) Then MsgBox "found it" Else fsoFSO.CreateFolder(strFolder) MsgBox "Done" End If End Sub Sub PDF_Generator() Dim cell As Range Dim wsSummary As Worksheet Dim counter As Long Set wsSummary = Sheets("SUMMARY BY PROVIDER") For Each cell In Worksheets("NAME KEY").Range("$H2:$H60") If cell.Value <> "Exclude" Then '******* Call your sub here with the folder to be creted **************************** MakeMyFolder cell.Value 'progress in status bar counter = counter + 1 Application.StatusBar = "Processing file: " & counter & "/1042" With wsSummary .Range("$B$8").Value = cell.Value .ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=ThisWorkbook.Sheets("SUMMARY BY PROVIDER").Range("J2").Value & _ "\" & cell.Value & ".pdf", _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End With End If Next cell Set wsSummary = Nothing End Sub