简体   繁体   English

我如何制作一个宏,使我能够将数据从一个工作簿导入到另一个工作簿?

[英]How do I make a macro that enables me to import data from one workbook to another ?B

Basically I would like to import data by clicking on a button assigned with the macro which would open the file browser, prompting the user to open the excel file they would like to import. 基本上,我想通过单击分配给宏的按钮来导入数据,这将打开文件浏览器,提示用户打开他们要导入的excel文件。 I have tried to debug my codes but my For Each loop keeps getting an error, any help is appreciated! 我尝试调试代码,但是我的For Each循环不断出现错误,我们将为您提供任何帮助!

Sub BrowseForFile()  
    Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

    fileName = Application.GetOpenFilename(, , "Browse for Workbook")
    Workbooks.Open (fileName)

    For Each sheet In Workbooks(fileName).Worksheets
        total = Workbooks("FIEP.xlsm").Worksheets.count
        Workbooks(fileName).Worksheets(sheet.Name).Copy _
          after:=Workbooks("FIEP.xlsm").Worksheets(total)
    Next sheet

    Workbooks(fileName).Close
End Sub

Use a variable for the workbook object: 为工作簿对象使用一个变量:

Sub BrowseForFile()

 Dim directory As String, fileName As String, sheet As Worksheet, total As Long
 Dim wb As Workbook

 fileName = Application.GetOpenFilename(, , "Browse for Workbook")


 Set wb = Workbooks.Open(fileName)

 For Each sheet In wb.Worksheets
    total = Workbooks("FIEP.xlsm").Worksheets.count
    sheet.Copy after:=Workbooks("FIEP.xlsm").Worksheets(total)
 Next sheet

 wb.Close

End Sub

Remove the file path part. 删除文件路径部分。 Variety of methods available. 有多种可用的方法。 I used the one from here . 我从这里用的那个。

The Workbooks object is the collection of all the Workbook objects that are currently open in the Microsoft Excel application. Workbooks对象是Microsoft Excel应用程序中当前打开的所有Workbook对象的集合。

It does not need the file path just the name. 它不需要文件路径,仅需要名称。 You could also have said ActiveWorkbook , though this would have been perhaps less robust. 您可能还说过ActiveWorkbook ,尽管它可能不够健壮。

Edit: Or as in @TimWilliam's answer, you can store the now open workbook in a variable and use that as the reference. 编辑:或者像在@TimWilliam的答案中一样,您可以将现在打开的工作簿存储在变量中,并将其用作引用。

Option Explicit

Sub BrowseForFile()

    Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

    fileName = Application.GetOpenFilename(, , "Browse for Workbook")

    Workbooks.Open (fileName)

    Dim fso As New FileSystemObject 'Requires references to MS Scripting Runtime

    fileName = fso.GetFileName(fileName)

    For Each sheet In Workbooks(fileName).Worksheets '
        Workbooks(fileName).Worksheets(sheet.Name).Copy _
        after:=Workbooks("FIEP.xlsm").Worksheets(total)
    Next sheet

    Workbooks(fileName).Close

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复将数据从一个工作簿复制到另一个工作簿的宏 - How to Fix a macro that copies data from one workbook to another 宏可将数据从一个工作簿复制到另一个工作簿 - Macro to copy data from one workbook to another 如何从工作簿中的不同工作表中获取宏拉数据。 Excel VBA - How do I make macro pull data from different sheets within a workbook. Excel VBA 将数据从一个工作簿导入到另一个工作簿 - Import Data From one workbook to another 宏将一行数据从一个工作簿复制到另一个工作簿 - macro to copy one row of data from one workbook to another 创建宏以将数据从一个工作簿自动填充到另一个 - Creating Macro to Auto-Populate Data from one Workbook to another 如何使用工作簿和工作表的变量将数据从一个 Excel 工作簿复制到另一个 Excel 工作簿? - How can I copy data from one Excel workbook to another Excel workbook using variables for Workbook and Worksheet? Excel宏数据从一个工作簿传输到另一个 - Excel Macro Data Transfer from One Workbook to Another 如何从一个工作簿复制数据并将值仅粘贴到另一个工作簿中并允许宏只运行一次? - How to copy the data from one Workbook and paste the value only in another Workbook and allow macro to run only one time? 如何使用一个工作簿中的宏来触发另一个工作簿的宏 - how to use a macro in one workbook to trigger another workbook's macro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM