繁体   English   中英

从多个 excel 工作簿中提取和编译数据

[英]Extracting and compiling data from multiple excel workbooks

我正在努力将许多工作簿的列提取到一个编译文件中,我在其中绘制编译数据的图表......问题在于,如果我打开的工作簿较少,则文件引用不起作用。 其次,我尝试过打开和复制值(通过 vba),但这非常慢......有时这两者都不起作用。 即使我还使用了完整的文件路径和文件名(使用 Dir() 和所有内容......)。 无论出于何种原因,此处提供的这段代码的运行速度要快得多,但前提是工作簿处于打开状态。

我想这在一定程度上是因为我不熟悉从其他工作簿中提取和编译数据的最佳方式......对于任何提示都会非常有用:.) dbug.prints 仅用于概述一切按预期运行。

Sub hente_inn_celleverdier_original()
'Filenames are listed here: (165 + i, 8) (H166 and underneath) *Edit: wrote wrong celladdress here (G166), fixed now...*
'In the target files, EP23 and EQ23 tells the number of cells in that column/list extempting: "", blank, etc.
'(7, 263 + 2 * i [= 265]) is the upper left cell in the table which we are filling in the compilation file…
Application.ScreenUpdating = False
For i = 1 To 38 '=Number of series(number of files I am extracting these values from) 

Debug.Print (Cells(165 + i, 8)) ' Prints filename (except part with filetype &".xlsx")
A = Workbooks(Cells(165 + i, 8) & ".xlsx").Sheets(1).Range("EP23")
Debug.Print A' Prints number from EP23.
Cells(7, 263 + 2 * i).Value2 = "='[" & Cells(165 + i, 8) & ".xlsx]CPTU'!EP28"
Cells(7, 263 + 2 * i).Select
Selection.AutoFill Destination:=Range(Cells(7, 263 + 2 * i), Cells(6 + A, 263 + 2 * i)),Type:=xlFillDefault

B = Workbooks(Cells(165 + i, 8) & ".xlsx").Sheets(1).Range("EQ23")
Debug.Print B' Prints number from EQ23.
Cells(7, 264 + 2 * i).Value2 = "='[" & Cells(165 + i, 8) & ".xlsx]CPTU'!EQ28"
Cells(7, 264 + 2 * i).Select
Selection.AutoFill Destination:=Range(Cells(7, 264 + 2 * i), Cells(6 + B, 264 + 2 * i)), Type:=xlFillDefault
Next 'i
Application.ScreenUpdating = True
End Sub

我很乐意按照您的要求提供示例。 我不太明白你在提供的代码中做了什么,但我会尝试调整这个例子

首先,您在所有内容之上声明类型组,包括任何子项(就像您对公共变量所做的那样):

Type WInfo
  WName as string
  WNum as long 'or double, if it is not an integer
end Type

然后你在 sub 中设置一个特殊变量,你认为你需要的最大条目数(在你的情况下是 38,但可以肯定的是,我们会说 200)

Sub hente_inn_celleverdier_original()
Dim WIn(200) As WInfo

您填写 WName 变量...

for i = 1 to 38
  WIn(i - 1).WName = Workbooks(Cells(165 + i, 8) & ".xlsx").Sheets(1).Range("EP23")'The entries begin un No.0 with this method
next i

您可以使用循环调用 WIn(i - 1).WName 中的值。 WIn(0).WName 将是第一个工作簿的名称,WIn(1).WName 是下一个...因此无需复制粘贴。

接下来是找到一种获取每个工作簿信息的方法。 例如,声明 function 以从 [此视频][1] 中使用的已关闭工作簿中获取信息。 不要使用“A”变量,而是使用另一个 for i = 1 循环,填充 Type 组的 WIn(i - 1).WNum 变量。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM