首页 > 解决方案 > 使用另一个工作簿中的文件名多次保存 excel 文件。

问题描述

我有一个小问题。我有 2 个文件。

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

:)

标签: excelvba

解决方案


这是从我用于类似的代码中提取的代码片段 - 它应该有所帮助:

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

提供帮助您入门 - 您需要根据自己的需要对其进行编辑/更改。


推荐阅读