简体   繁体   中英

Extracting and compiling data from multiple excel workbooks

I am struggling with extracting columns of many workbooks into one compilation file where I draw Charts of the compiled data... The struggle is that the file referring isn't working if less I have the workbooks open. Secondly, I have tried opening and copying values (by vba), but that is very slow... And some times neither that is working. Even when I have also used the full filepath and file name (with Dir() and everything...). For whatever reason, this code presented here works much faster, but only when the workbooks are open.

I guess it is partly that I am not familiar with most optimal way of extracting and compiling data from other workbooks... Will be greatfull for any tips: .) The dbug.prints are only for overview that everything runs as intended.

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

I'll gladly provide an example as you ask. I don't quite get what you are doing in the code provided, but I'll try to adapt this example

First, you declare the type group on top of everything, including any sub (as you would do with a public variable):

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

Then you set a special variable inside a sub, with a max of entries you think you'll need (38 in your case, but to be sure, we'll say 200)

Sub hente_inn_celleverdier_original()
Dim WIn(200) As WInfo

You fill the WName variable...

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

And you can call the value inside the WIn(i - 1).WName with a loop. WIn(0).WName would be the name of the first workbook, WIn(1).WName is the next one... So no need to copy-paste.

Next would be finding a way of getting the info of each workbook. For example, declaring a function to get info from closed workbooks used in [this video][1]. Instead of using your "A" variable, use another for i = 1 loop, filling the WIn(i - 1).WNum variable of the Type group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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