使用数据创建,命名和填充新工作簿

时间:2011-03-25 00:29:18

标签: excel vba excel-vba

我有一张包含两列数据的Excel表格。 A列具有所需的新工作簿名称,B列的相邻单元格具有新工作簿中所需的数据。

因此,如果{A}位于A列,而032411位于B列,那么该宏会创建一个名为50的新工作簿,而032411将位于单元格{{该工作簿的1}}。

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