Hi all,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o></o>
first off, I’m a complete beginner in VBA so sorry for asking probably a dumb question.<o></o>
<o></o>
I'm currently working on a macro that can read data from closed workbooks in a folder (specified cells on a sheet) and copy the value to the workbook that is open. I have searched the internet and this forum and found some code that is working. (see below) but..<o></o>
<o></o>
The code below works, however I want to modify it that when the macro finds a workbook without the designated sheet ("toetsing erosiebestendigheid") it just skips the file. I guess this goes with an IF statement somewhere I just can't figure out where to put it and get it working..<o></o>
<o></o>
The code below is also pretty slow. Do any of you guy's now a routine that is faster with the same result?<o></o>
hope someone can help!
thanks!
<o></o>
first off, I’m a complete beginner in VBA so sorry for asking probably a dumb question.<o></o>
<o></o>
I'm currently working on a macro that can read data from closed workbooks in a folder (specified cells on a sheet) and copy the value to the workbook that is open. I have searched the internet and this forum and found some code that is working. (see below) but..<o></o>
<o></o>
The code below works, however I want to modify it that when the macro finds a workbook without the designated sheet ("toetsing erosiebestendigheid") it just skips the file. I guess this goes with an IF statement somewhere I just can't figure out where to put it and get it working..<o></o>
<o></o>
The code below is also pretty slow. Do any of you guy's now a routine that is faster with the same result?<o></o>
Code:
Sub ReadDataFromAllWorkbooksInFolder()
Dim FolderName As String, wbName As String, r As Long
Dim wbList() As String, wbCount As Integer, i As Integer
Dim monsternr As Variant, monsterdiep As Variant, vloeigrens As Variant, plastic As Variant
Dim mineraledelen As Variant, resultaat As Variant
FolderName = ActiveWorkbook.Path
' create list of workbooks in foldername
wbCount = 0
wbName = Dir(FolderName & "\" & "*.xls")
While wbName <> vbNullString
wbCount = wbCount + 1
ReDim Preserve wbList(1 To wbCount)
wbList(wbCount) = wbName
wbName = Dir
Wend
If wbCount = 0 Then Exit Sub
' get values from each workbook
r = 0
'Workbooks.Add
For i = 1 To wbCount
r = r + 1
monsternr = GetInfoFromClosedFile(FolderName, wbList(i), "Toetsing erosiebestendigheid", "F7")
monsterdiep = GetInfoFromClosedFile(FolderName, wbList(i), "Toetsing erosiebestendigheid", "F11")
vloeigrens = GetInfoFromClosedFile(FolderName, wbList(i), "Toetsing erosiebestendigheid", "F21")
plastic = GetInfoFromClosedFile(FolderName, wbList(i), "Toetsing erosiebestendigheid", "F22")
mineraledelen = GetInfoFromClosedFile(FolderName, wbList(i), "Toetsing erosiebestendigheid", "F23")
resultaat = GetInfoFromClosedFile(FolderName, wbList(i), "Toetsing erosiebestendigheid", "F25")
Cells(r, 1).Formula = monsternr
Cells(r, 2).Formula = monsterdiep
Cells(r, 3).Formula = vloeigrens
Cells(r, 4).Formula = plastic
Cells(r, 5).Formula = mineraledelen
Cells(r, 6).Formula = resultaat
Next i
End Sub
Private Function GetInfoFromClosedFile(ByVal wbPath As String, wbName As String, wsName As String, cellRef As String) As Variant
Dim arg As String
GetInfoFromClosedFile = vbNullString
If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\"
If Dir(wbPath & wbName) = vbNullString Then Exit Function
arg = "'" & wbPath & "[" & wbName & "]" & wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)
On Error Resume Next
GetInfoFromClosedFile = ExecuteExcel4Macro(arg)
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
hope someone can help!
thanks!