VBA/Function to read data from a list of closed workbooks(dynamically)

lianli34312

New Member
Joined
Jan 18, 2018
Messages
5
Hello everyone,

I am recently working with some data analysis in my project. In my case I need to run a VBA that can automatically read the data from a list of closed excel workbook named from 1-80, in which the data i would like to read is store at cell F7 .
showimg.php
[/IMG]


I try to study the threads on internet and i come up with the following "function" :

CaseFactor
1='D:\Data\FYP\[1.xlsx]Worksheet1'!$F$7
2
='D:\Data\FYP\[2.xlsx]Worksheet2'!$F$7

<tbody>
</tbody>

But how can i loop the so call "function" to look for all worksheet and return the value for me?

Thank you:)
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
U didn't say where U want the data. This will put it in sheet1 A!:A& whatever. HTH, Dave
Code:
Private Sub test()
Dim fso As Object, FolDir As Object, FileNm As Object, Cnt As Integer
On Error GoTo erfix
Set fso = CreateObject("scripting.filesystemobject")
Set FolDir = fso.GetFolder("D:\Data\FYP")
Application.ScreenUpdating = False
For Each FileNm In FolDir.Files
If FileNm.Name Like "*" & ".xls" & "*" Then
workbooks.Open filename:=FileNm
Cnt = Cnt + 1
ThisWorkbook.Sheets("Sheet1").Range("A" & Cnt).Value = _
          workbooks(FileNm.Name).Sheets("sheet1").Range("F" & 7)
workbooks(FileNm.Name).Close SaveChanges:=False
End If
Next FileNm
Application.ScreenUpdating = True
Set FolDir = Nothing
Set fso = Nothing
Exit Sub
erfix:
On Error GoTo 0
MsgBox "Error"
Application.ScreenUpdating = True
Set FolDir = Nothing
Set fso = Nothing
End Sub
 
Upvote 0
Finally i do it like this after referencing to your code
Thank for you help:)

Code:
Private Sub test()Range("A1:B500").ClearContents
Dim fso As Object, FolDir As Object, FileNm As Object, Cnt As Integer
On Error GoTo erfix
Set fso = CreateObject("scripting.filesystemobject")
Set FolDir = fso.GetFolder("D:\Data\FYP")
Application.ScreenUpdating = False
For Each FileNm In FolDir.Files
If FileNm.Name Like "*" & ".xls" & "*" Then
  
    Application.DisplayAlerts = False
    UpdateLinks = True
  
Workbooks.Open Filename:=FileNm


    Application.DisplayAlerts = True


Cnt = Cnt + 1


ThisWorkbook.Sheets("Sheet1").Range("A" & Cnt).Value = _
          Workbooks(FileNm.Name).Sheets("Sheet11").Range("F" & 7)
                           
Workbooks(FileNm.Name).Close SaveChanges:=False
End If
Next FileNm
Application.ScreenUpdating = True
Set FolDir = Nothing
Set fso = Nothing
Exit Sub
erfix:
On Error GoTo 0
MsgBox "Error"
Application.ScreenUpdating = True
Set FolDir = Nothing
Set fso = Nothing
End Sub
 
Upvote 0
Actually the VBA didn't read the data by the ascending order(I have a set of data named from 1 to 80), may i know how to modify the loop in order to read it by the ascending order? Please:)
 
Upvote 0
Not sure what U mean? Do U have 80 files? The data is going A1:A & whatever.. do U want this changed? Dave
 
Upvote 0
Maybe this...
Code:
Private Sub test()
Range("A1:B500").ClearContents
Dim fso As Object, FileNm As Object, Cnt As Integer
On Error GoTo erfix
Set fso = CreateObject("scripting.filesystemobject")
Application.ScreenUpdating = False
For Cnt = 1 To 80
Set fso = CreateObject("scripting.filesystemobject")
'***change File path to your file
Set FileNm = fso.GetFile("D:\Data\FYP\" & CStr(Cnt) & ".xlsx")
Application.DisplayAlerts = False
UpdateLinks = True
Workbooks.Open Filename:=FileNm
ThisWorkbook.Sheets("Sheet1").Range("A" & Cnt).Value = _
          Workbooks(FileNm.Name).Sheets("Sheet11").Range("F" & 7)
                           
Workbooks(FileNm.Name).Close SaveChanges:=False
Next Cnt
Application.ScreenUpdating = True
Set fso = Nothing
Exit Sub
erfix:
On Error GoTo 0
MsgBox "Error"
Application.ScreenUpdating = True
Set FolDir = Nothing
Set fso = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,851
Members
449,051
Latest member
excelquestion515

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