首页 文章

将Excel电子表格数据导入另一个包含VBA的Excel电子表格

提问于
浏览
13

我们需要编写一个带有VBA代码的Excel电子表格;代码读取并对第一个工作表中的数据执行操作 .

用户将接收包含数据但不包含VBA代码的电子表格 . 我们需要能够将包含数据的电子表格中的数据自动导入包含VBA代码的电子表格中 . 包含数据的工作表与包含数据的电子表格的工作表具有相同的列格式和数据类型 .

理想情况下,您可以打开包含VBA代码的电子表格,显示一个允许用户导航到包含数据的电子表格的UI,单击“确定”,然后导入数据 .

你会怎么做呢?必须在Excel电子表格中使用VBA完成 .

非常感谢 .

2 回答

  • -1

    这应该可以帮到您:在您自己的Excel工作簿中使用VBA,让它提示用户输入其数据文件的文件名,然后将该固定范围复制到您的目标工作簿中(可能与您启用宏的工作簿相同) ,或第三个工作簿) . 这是一个快速的vba示例,说明它是如何工作的:

    ' Get customer workbook...
    Dim customerBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim customerFilename As String
    Dim customerWorkbook As Workbook
    Dim targetWorkbook As Workbook
    
    ' make weak assumption that active workbook is the target
    Set targetWorkbook = Application.ActiveWorkbook
    
    ' get the customer workbook
    filter = "Text files (*.xlsx),*.xlsx"
    caption = "Please Select an input file "
    customerFilename = Application.GetOpenFilename(filter, , caption)
    
    Set customerWorkbook = Application.Workbooks.Open(customerFilename)
    
    ' assume range is A1 - C10 in sheet1
    ' copy data from customer to target workbook
    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(1)
    Dim sourceSheet As Worksheet
    Set sourceSheet = customerWorkbook.Worksheets(1)
    
    targetSheet.Range("A1", "C10").Value = sourceSheet.Range("A1", "C10").Value
    
    ' Close customer workbook
    customerWorkbook.Close
    
  • 26

    可以通过工作簿方法或外部参考或通过数据导入工具将数据从另一个Excel中提取到Excel中 .

    如果您想要阅读或甚至想要更新另一个Excel工作簿,可以使用这些方法 . 我们可能不仅仅依赖于VBA .

    For more info on these techniques, please click here to refer the article

相关问题