使用数据创建,命名和填充新工作簿 [英] Create, name, and populate new workbook with data

查看:77
本文介绍了使用数据创建,命名和填充新工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

因此,如果 032411 位于A列中,而 50 位于B列中,则宏将创建一个名为 032411 50 将位于该工作簿的单元格 A1 中.

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.

推荐答案

您可以使用此

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 .

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

这篇关于使用数据创建,命名和填充新工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆