使用另一个工作簿中的文件名多次保存excel文件。

时间:2018-08-04 09:23:21

标签: excel excel-vba

我有一个小查询。我有2个档案。

我想在一个特定的文件夹中多次保存“ File#1”。 预期的文件名在“文件#2”的Sheet1的C列中。我该怎么办?任何帮助将不胜感激。

:)

1 个答案:

答案 0 :(得分:0)

以下是我用于类似操作的代码摘录的片段-应该会帮助您

Option Explicit
Sub Exportmacro()

Dim rCell As Range, rRng As Range 'define loop names
Dim NewCaseFile As Workbook 'give a name to new work book for duplicate sheet
Dim wks As Worksheet 'name of the copy of feedback
Dim sPath As String
sPath = MacScript("(path to desktop folder as string)")
'turn off screen
With Application
    '.ScreenUpdating = False  ‘only removed while testing
    '.EnableEvents = False
    '.Calculation = xlCalculationManual  ‘disabled for the moment
End With

'Student numbers in cells A7:A160 WARNING SET TO 3 STUDENTS ONLY FOR TEST
Set rRng = Worksheets("studentlist").Range("A7:A9")

    For Each rCell In rRng '<--| loop through "students" range

        'now open new workbook 
         Set NewCaseFile = Workbooks.Add

        'now save as xls with student number as filename Filename:=sPath & rCell.Value & ".xlsx"
         ActiveWorkbook.SaveAs Filename:=rCell.Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

        'now close duplicate file
         ActiveWorkbook.Close False

    Next rCell   '<-- next student number
End With         '<-- once all done
'turn screen back on
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

提供入门知识-您需要根据自己的需要进行编辑/更改。