Create, name, and populate new workbook with data

2019-08-17 07:59发布

I have an Excel sheet with two columns of data. Column A has the desired new workbook names and Column B's adjacent cell has the data I want in the new work book.

So if 032411 is in column A and 50 is in column B then the macro would create a new workbook named 032411 and 50 would be in cell A1 of that workbook.

1条回答
迷人小祖宗
2楼-- · 2019-08-17 08:27

You can use this:

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

In case you have several files to generate, you can alternatively use the application.ScreenUpdating = False.

查看更多
登录 后发表回答