我有一张包含两列数据的Excel表格。 A列具有所需的新工作簿名称,B列的相邻单元格具有新工作簿中所需的数据。
因此,如果{A}位于A列,而032411
位于B列,那么该宏会创建一个名为50
的新工作簿,而032411
将位于单元格{{该工作簿的1}}。
答案 0 :(得分:2)
您可以使用:
Sub CreateBooks()
Dim oCell As Excel.Range
Dim oWorkbook As Excel.Workbook
'Added to avoid messages asking to confirm overwriting
' previous existent files with same name
Application.DisplayAlerts = False
For Each oCell In Range("A:A")
If oCell.Value = "" Then Exit For
Set oWorkbook = Workbooks.Add
oWorkbook.Sheets(1).Cells(1, 1).Value = oCell.Offset(0, 1).Value
'If the cell value contains only the file name (instead of the whole path
' the file needs to be saved) it will save into MyDocuments folder
oWorkbook.Close True, oCell.Value
Next oCell
Application.DisplayAlerts = True
End Sub
如果您要生成多个文件,您也可以使用application.ScreenUpdating = False
。