98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Collecting data from excel file
Collecting data from excel file
Resolved · Medium Priority · Version 2007
Kees has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course
Collecting data from excel file
Is there a code available that can collect data from excel files in the root without this file even opened ?
Thanks and kind regards,
Kees
RE: Collecting data from excel file
Hi Kees, thanks for your query. Stick three Excel spreadsheets into C:\test and make sure cell D10 in each has a month in it. Then try running the following:
--------------------------------------------
Sub GetMyData()
Dim wbOpen As Workbook
Const strPath As String = "C:\test\" '<------- change accordingly
Dim strExtension As String
Dim myValue As String
Dim intTargetrowcount As Integer
intTargetrowcount = 1
Application.ScreenUpdating = False
Application.DisplayAlerts = False
ChDir strPath
'Change extension
strExtension = Dir(strPath & "*.xls")
'Dir returns the first file name that matches pathname.
'To get any additional file names that match pathname, call Dir again with no arguments.
'When no more file names match, Dir returns a zero-length string ("").
'Once a zero-length string is returned, you must specify pathname in subsequent calls or an error occurs.
'You can change to a new pathname without retrieving all of the file names that match the current pathname.
'However, you can't call the Dir function recursively.
'Calling Dir with the vbDirectory attribute does not continually return subdirectories.
Do While strExtension <> ""
Set wbOpen = Workbooks.Open(strPath & strExtension)
'MsgBox wbOpen.Sheets("mydata").Cells(10, 4).Value
'*************************************************************
'alter the following code to tailor to your forms:
'dump the value from the returned form into a variable
'do this (with multiple variables) for multiple form results if necessary
myValue = wbOpen.Sheets("MyData").Range("d10").Value
'close down the form
wbOpen.Close SaveChanges:=False
'add the collected value to your results sheet:
Sheets("Results").Cells(intTargetrowcount, 1).Value = myValue
'and don't forget to create a new row for each returned form
intTargetrowcount = intTargetrowcount + 1
'*************************************************************
strExtension = Dir 'go to the next file in the folder
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
--------------------------------------------
Hope this helps,
Anthony
Mon 21 May 2012: Automatically marked as resolved.
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Move or Highlight CellsUse any of your movement keys, cursor, Home, End, PgUp or PgDn to highlight cells rows or columns by holding down the Shift key as you move. |