简体   繁体   English

使用VBA将另一个工作簿中的工作表加载到Excel中

[英]Loading a worksheet from another workbook into Excel with VBA

I am attempting to create a subroutine that prompts the user to select a workbook and then adds the first worksheet of the selected workbook as a tab in the existing (active) workbook. 我正在尝试创建一个子例程,提示用户选择一个工作簿,然后将所选工作簿的第一个工作表添加为现有(活动)工作簿中的选项卡。 Then names the new tab "Data". 然后将新选项卡命名为“数据”。 Here is the code I am using so far: 这是我到目前为止使用的代码:

Sub getworkbook()
' Get workbook...
Dim ws As Worksheet
Dim filter As String
Dim targetWorkbook As Workbook

Set targetWorkbook = Application.ActiveWorkbook

' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
ws = Application.GetOpenFilename(filter, , caption)

ws.Add After:=Sheets(Sheets.Count)

ws.Name = "DATA"

End Sub

This code doesn't seem to be working and is returning the following error: 此代码似乎不起作用,并返回以下错误:

"ws.Add" method or With Block not set. “ws.Add”方法或With Block未设置。

Any help is appreciated. 任何帮助表示赞赏。

Thanks, 谢谢,

You have declared ws as a worksheet and GetOpenFilename is returning a File name. 您已将ws声明为工作表, GetOpenFilename将返回文件名。 I would recommend reading my post in this link : 我建议在这个链接中阅读我的帖子:

Is this what you are trying? 这是你在尝试什么?

Note : I have not done any error handling. 注意 :我没有做任何错误处理。 I am sure you can take care of that. 我相信你可以照顾到这一点。

Sub getworkbook()
    ' Get workbook...
    Dim ws As Worksheet
    Dim filter As String
    Dim targetWorkbook As Workbook, wb As Workbook
    Dim Ret As Variant

    Set targetWorkbook = Application.ActiveWorkbook

    ' get the customer workbook
    filter = "Text files (*.xlsx),*.xlsx"
    Caption = "Please Select an input file "
    Ret = Application.GetOpenFilename(filter, , Caption)

    If Ret = False Then Exit Sub

    Set wb = Workbooks.Open(Ret)

    wb.Sheets(1).Move After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)

    ActiveSheet.Name = "DATA"
End Sub

暂无
暂无

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

相关问题 VBA:将Excel工作表复制到另一个工作簿 - VBA: Copy an Excel worksheet into another workbook 从工作簿创建新工作表 VBA (Excel) - creating new worksheet from workbook in VBA (Excel) VBA将Excel工作表复制到另一个工作簿中的新工作表 - VBA copying an Excel worksheet to a new worksheet in another workbook 防止在另一个工作簿上从VBA取消隐藏工作表 - Prevent unhiding worksheet from VBA on another workbook Excel VBA宏,用于打开信息并将信息从一个工作簿加载到另一个工作簿 - Excel VBA Macro for opening and loading information from one workbook to another VBA 将工作表从活动工作簿添加到新的 Excel 应用程序工作簿 - VBA Add Worksheet from active Workbook to new Excel Application Workbook VBA将数据从一个工作簿复制到另一工作簿/工作表不起作用 - VBA to Copy data from one workbook to another workbook/worksheet not working VBA 脚本或 excel 公式根据一组规则从另一个工作簿向工作表添加数据 - VBA script or excel formula to add data in a worksheet from another workbook, based on a set of rules Excel VBA 可根据文本将整行从 1 个工作表复制并粘贴到同一工作簿中的另一个工作表 - Excel VBA to copy and paste an entire row from 1 worksheet to another in the same workbook based on a text 如何通过另一个工作簿中的VBA对象名称引用Excel工作表? - How to refer to a Excel Worksheet by its VBA Object Name in another Workbook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM