【问题标题】:Importing Excel spreadsheet data into another Excel spreadsheet containing VBA将 Excel 电子表格数据导入另一个包含 VBA 的 Excel 电子表格
【发布时间】:2011-12-14 03:21:56
【问题描述】:

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

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

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

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

非常感谢。

【问题讨论】:

    标签: excel vba import spreadsheet


    【解决方案1】:

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

    如果你想阅读或者即使你想更新另一个excel工作簿,这些方法都可以使用。我们可能不仅仅依赖于 VBA。

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

    【讨论】:

      【解决方案2】:

      这应该可以帮助您入门: 在您自己的 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
      

      【讨论】:

      • 谢谢 - 这正是我所追求的。
      猜你喜欢
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2013-06-15
      • 2011-08-09
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多