我想要一个 VBA 宏将一些工作表导出到单独的繁體基于工作表名称列表。这些名称的列表例如在范围内Sheet1 中的 A1:A50
答案1
我没有声誉点数来评论,所以我无法确定你具体需要什么。希望你能从我的回答中得到一些启发。
我假设:
- 您要导出的每个工作表都将导出到其自己的(新)工作簿 (*.xlsx)
- 您将在当前工作簿中保留导出的工作表的副本
请注意,这不能处理异常(例如缺少表格,名称不匹配等)
Sub ExportSheets()
Dim wksWorksheets As Excel.Sheets
Dim rngSheetNames As Excel.Range, rngSheet As Excel.Range
Dim strSheetTitle As String, strSavePath As String
strSavePath = "DriveLetter:\path\to\save\goes\here\" ' Change this to your path
Set wksWorksheets = Excel.ThisWorkbook.Worksheets
Set rngSheetNames = wksWorksheets("Sheet1").Range("A1:A50") ' Put the specific sheet and range here
For Each rngSheet in rngSheetNames
strSheetTitle = rngSheet.Value
With wksWorksheets(strSheetTitle)
.Copy
.SaveAs Filename:= strSavePath + strSheetTitle + ".xlsx"
End With
Next rngSheet
End Sub
如果这不是您特别需要的,那么根据您的要求修改代码应该不会太难。
新泽西州
编辑: 导出到相同的现有工作簿。注意,导出前工作簿必须存在。
Sub ExportToWorkbook()
Dim wkbExportBook As Excel.Workbook
Dim wksWorksheets As Excel.Sheets
Dim rngSheetNames As Excel.Range, rngSheet As Excel.Range
Dim strSheetTitle As String, strWkBookPath As String
Excel.Application.ScreenUpdating = False
strWkBookPath = "DriveLetter:\path\to\workbook\goes\here\ExportBook.xlsm" ' Change this to your path
Set wksWorksheets = Excel.ThisWorkbook.Worksheets
Set rngSheetNames = wksWorksheets("Sheet1").Range("A1:A50") ' Put the specific sheet and range here
Set wkbExportBook = Excel.Workbooks.Open(strWkBookPath)
For Each rngSheet In rngSheetNames
strSheetTitle = rngSheet.Value
wksWorksheets(strSheetTitle).Copy Before:=wkbExportBook.Sheets(1) ' Before the first sheet or any sheet you want, just pass the sheet index
Next rngSheet
wkbExportBook.Close SaveChanges:=True
Excel.Application.ScreenUpdating = True
End Sub
如果您需要一个新的工作簿来导出一批工作表,请查看该函数,
Excel.Workbooks.Add strWorkBookPathName
您应该能够将其设置为 Workbook 对象变量。
新泽西州