2011-10-24 113 views
12

我们需要编写一个包含VBA代码的Excel电子表格;该代码读取并执行第一个工作表中的数据操作。将Excel电子表格数据导入包含VBA的另一个Excel电子表格

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

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

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

非常感谢。

+0

另一个......链接点击进入制药业骗局。请删除并用Wayback机器副本进行更换。同时,读者请注意点击上面的链接(vba4all [dot] com ...)。 –

回答

25

这应该让你开始: 在自己的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 
+0

谢谢 - 那正是我以后的样子。 – Anthony

相关问题