宏从不同的文件中提取数据

时间:2013-07-12 12:24:08

标签: excel-vba vba excel

我有一个每日仪表板文件,我每天从带有宏的5个不同文件中提取数据。宏工作正常,但它询问我应该为特定步骤选择哪个文件:Workbooks.Open vFile(出现一个窗口,我只选择文件)。这些文件每周或每月更改一次,所以我想要做的是将源插入特定的单元格,宏不会要求我提供该文件,但会将源文件放在那里。

这可能吗?

代码(我不是把整个代码放在我想改变的部分)

vFile = Application.GetOpenFilename("Excel Files (.xl)," & ".xl", 1, "select WF Report", "Open", False) 

Workbooks.Open vFile

然后宏指向活动文件&片。复制完所有数据后,会出现另一个窗口提示,然后选择第二个文件等。

vFile = Application.GetOpenFilename("Excel Files (.xl)," & ".xl", 1, "select Front End Tracker ", "Open", False)

Workbooks.Open vFile

我想要改变的是,我希望宏从Excel工作表中的特定单元格中获取文件的源和名称,而不是选择文件窗口。关于如何做的任何想法?

1 个答案:

答案 0 :(得分:2)

答案很简单,但我会尝试为您提供一些选择。

A)如果你输入单元格A1的完整路径名和文件名,如:

c:\users\user_name\Documents\files\moj plik.xlsx

然后您只需要按如下方式更改代码:

'comment this line by adding ' at the beginning
'vFile = Application.GetOpenFilename("Excel Files (.xl)," & ".xl", 1, "select WF Report", "Open", False) 
'change next line by adding reference to the cell
Workbooks.Open Range("A1")
'or with reference to sheet name and range A1- change according to your situation
Workbooks.Open Sheets("put here sheet name").Range("A1")

B)如果你只想引用文件名,你需要以某种方式定义文件的路径。如果您的单元格A1只有文件名,如:

moj plik.xlsx

然后您的代码将相应更改:

'comment this line by adding ' at the beginning
'vFile = Application.GetOpenFilename("Excel Files (.xl)," & ".xl", 1, "select WF Report", "Open", False) 
'change by adding reference to the cell and path constant
Dim xlsPath as String
    xlsPath = "c:\users\user_name\Documents\files\"
'change above accordingly to what you have
Workbooks.Open xlsPath & Range("A1")
'or with reference to sheet name and range A1- change according to your situation
Workbooks.Open xlsPath & Sheets("put here sheet name").Range("A1")