根据单元格值将Excel工作表保存到文件夹和文件名

时间:2018-09-25 16:36:16

标签: excel

我正在尝试将工作表的副本保存到基于单元格H3的特定文件夹,并也根据单元格范围F2命名.xlsx文件。我正在使用以下代码将工作表的副本保存到文件夹。只是无法使其正常工作。我收到错误代码。

这是我正在使用的代码:

  Const MYPATH As String = "R:\Sales\Quotes (Commercial)\"

Sub IfNewFolder()

'Dim part1 As String 'this variable is not used -----
Dim part3 As String  'Company Name
Dim part4 As String 'Folder Name
'----- Dim FolderCreate As String  'this variable is not used -----

'----- part1 = Range("E4").Value 'not used here -----
part3 = Range("D1").Value
part4 = Range("J2").Value

If Len(Dir(MYPATH & part3 & part4, vbDirectory)) = 0 Then
   MkDir MYPATH & part3 & part4
End If

End Sub
Sub SaveFileFolder()

Dim part1 As String
Dim part3 As String
Dim part4 As String

part1 = Range("F2").Value 'Quote Number
part3 = Range("D1").Value 'Company Name
part4 = Range("J2").Value 'Folder Name


IfNewFolder 'create company subfolder

'ChDir MYPATH ' From what I've read on the internet, this is telling excel to save files to this directory...
'-----you don't have to do that because this path is included in the filename in the SaveAs below -----

' Creates file to directory Customers. But I can't get it to recognize the new folder created in the sub above...

'ActiveWorkbook.SaveAs Filename:= _
 MYPATH & part1 & "_" & part3 & ".xlsm", FileFormat:= _
 xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

'----- instead put company in path (point to company subfolder) -----
ActiveWorkbook.SaveAs FileName:= _
MYPATH & part3 & part4 & "\" & part1 & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False


End Sub

0 个答案:

没有答案