Get cell values from all workbooks in folder

cgcamal

Active Member
Joined
May 2, 2007
Messages
472
Hi all,

Please help over here.

I have several workbooks in a folder with sub folders. How can I get the values located in C6,
E6 and E9 and put in a new workbook the "file name" in Ai and those 3 values in Bi, Ci and Di
respectively? It´d be desirable get the values without open the files.

something like:

------A-----------B---------C----------D
Filename1.xls---13:11------13:57-----00:46
Filename2.xls---17:42------18:03-----00:21
.
.

Many thanks in advance for any help
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Getting just a few cell values from a closed workbook can easily be done like the code below. Use Offset to offset rows and columns. I would insert the filename into column A and then offset the columns from that cell to keep your data on the same row.

While Application.FileSearch is one of the best methods to get filenames in subfolders, it won't work in Excel 2007.

Is this enough help?

Code:
Sub t()
Range("B" & rows.count).end(xlup)).Offset(1).Value = GetValue("c:\", "test.xls", "Sheetx", "A1")
End Sub

'=GetValue("c:\files", "budget.xls", "Sheet1", "A1")
Private Function GetValue(path, file, sheet, ref)
'    path = "d:\files"
'    file = "budget.xls"
'    sheet = "Sheet1"
'    ref = "A1:R30"

    Dim arg As String
     
    If Right(path, 1) <> "\" Then path = path & "\"
     
    If Dir(path & file) = "" Then
        GetValue = "file not found"
        Exit Function
    End If
     
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
    Range(ref).Range("a1").Address(, , xlR1C1)
     
    GetValue = ExecuteExcel4Macro(arg)
End Function
 
Upvote 0
Thanks Kenneth. I will adjust my code accordingly since we are migrating to Excel 2007.
 
Upvote 0
Most are using fso to iterate subfolders in 2007. If you like FileSearch, I would recommend using the classes in this thread. It also uses fso, you will need to add the reference to Microsoft Scripting Runtime, but the basic syntax to use it is fairly similar to Application.FileSearch.
 
Upvote 0
Hi tiomin,

Thanks for your help, I tryed the code for you question and I´ve got an error with the command "Application.FileSearch". How can translate this command to Excel 2007?

Hello again Kenneth,

Very appreciated your help either. I´ve been testing your code. I changed in ref the following:

ref = "C6,E6,E9"

But only gets value for C6.

In addition,
1-) I´m not getting the filename, only C6 value.
2-) how to introduce a loop for every workbook within the folder?

Thanks again.
 
Upvote 0
The Excel4 method only gets one cell at a time. So do it for each cell that you need.

Use the FileSearch class that I linked to or one of the other fso methods that I referenced in that thread to get your filenames.

PM me after 6 PM central daylight time and I will post a complete solution for tonight or tomorrow if you still need help.
 
Upvote 0
You can get an example xls with all of the classes and modules by clicking 1839452 classFileSearch and Excel4.xls

The main part goes like this:
Code:
Sub GetMyData()
  Dim fs As New cFileSearch
  Dim NextRow As Long, Filestoprocess As Long, i As Long
  Dim theFile As String, thePath As String, val
  Dim skipThisWorkbook As Boolean
  'On Error GoTo EndSpeed
  SpeedOn
  skipThisWorkbook = True
  NextRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
  thePath = ThisWorkbook.path
  With fs 'like Application.FileSearch
    .LookIn = thePath
    .SearchSubFolders = True
    .FileName = "*.xls" '* for all files
    .Execute
    Filestoprocess = .FoundFiles.Count
    If Filestoprocess = 0 Then Exit Sub
    For i = 1 To Filestoprocess
      theFile = FilePart(.FoundFiles(i))
      Cells(NextRow, "A").value = theFile
      
      If ThisWorkbook.Name = theFile And skipThisWorkbook = True Then GoTo Nexti

      val = GetValue(thePath, theFile, "Sheet1", "C6")
      If val <> 0 Then Cells(NextRow, "B").value = val
      
      val = GetValue(thePath, theFile, "Sheet1", "E6")
      If val <> 0 Then Cells(NextRow, "C").value = val

      val = GetValue(thePath, theFile, "Sheet1", "E9")
      If val <> 0 Then Cells(NextRow, "D").value = val

      NextRow = NextRow + 1
Nexti:
    Next i
  End With
  Range("A1:D1").EntireColumn.AutoFit
EndSpeed:
  SpeedOff
End Sub
 
Upvote 0
You say that you had problems with:
Code:
 val = GetValue(thePath, theFile, "Reporte de Falla", "C6")
'      If val <> 0 Then Cells(NextRow, "B").value = val

Your thePath and theFile must exist for the function to get the values. Try the code in a folder where only that xls and one other with values in those cells with the sheet name "Reporte de Falla".

I gather it did get the filenames that you expected.
 
Upvote 0
Hi everyone,

I've tried the above code from Kenneth (thank you by the way) but when I try and run it I get a 'Compile Error: User defined type not defined'.

Any ideas? I'm on Excel 2003. Is this the problem?

:):):)
 
Upvote 0

Forum statistics

Threads
1,216,128
Messages
6,129,028
Members
449,482
Latest member
al mugheen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top