ozan efendi
New Member
- Joined
- Nov 6, 2012
- Messages
- 17
Hi, I have a macro, that my Book2 fetches datas from reference workbooks Book1 and Book3.
If i write to for loop 10 instead of 3, the code looks for Book4 to Book10, and just because this workbooks don' t exist in my path, the code gives an error eventually.
Is it possible to ignore this limitation (for example: For iCount= 1 to n) It doesn' t matter how many reference workbooks in my path? Can anyone explain with the codes?
Regards,
Ozan.
Option Explicit
Sub TransferData()
Dim wbStore, wbSource As Workbook, wsStore, wsSource As Worksheet, LastRow As Long
Dim FilePath As String, FileName, FullName As String
Dim blnOpened As Boolean
Dim iCount As Integer
FilePath = "C:\Users\ozzy\Desktop\Makro2\" 'this is my path
FileName = "Book2.xlsm"
Call ToggleEvents(False)
Set wsStore = Workbooks(FileName).Sheets("Sheet1")
For iCount = 1 To 3
If iCount <> 2 Then
FullName = FilePath & "Book" & iCount & ".xlsx"
Set wbSource = Workbooks.Open(FullName)
Set wsSource = wbSource.Sheets("Sheet1") 'change destination sheet name here
LastRow = wsStore.Cells.Find(what:="*", after:=wsStore.Cells(1, 1), searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
wsStore.Cells(LastRow, "A").Value = wsSource.Cells(1, "B").Value
wsStore.Cells(LastRow, "B").Value = wsSource.Cells(4, "B").Value
wsStore.Cells(LastRow, "C").Value = wsSource.Cells(7, "B").Value
wsStore.Cells(LastRow, "D").Value = wsSource.Cells(7, "E").Value
wbSource.Close savechanges:=True
End If
Next iCount
Call ToggleEvents(True)
End Sub
Sub ToggleEvents(blnState As Boolean)
With Application
.DisplayAlerts = blnState
.EnableEvents = blnState
.ScreenUpdating = blnState
If blnState Then .CutCopyMode = False
If blnState Then .StatusBar = False
End With
End Sub
Function WbOpen(wbName As String) As Boolean
On Error Resume Next
WbOpen = Len(Workbooks(wbName).Name)
End Function
If i write to for loop 10 instead of 3, the code looks for Book4 to Book10, and just because this workbooks don' t exist in my path, the code gives an error eventually.
Is it possible to ignore this limitation (for example: For iCount= 1 to n) It doesn' t matter how many reference workbooks in my path? Can anyone explain with the codes?
Regards,
Ozan.
Option Explicit
Sub TransferData()
Dim wbStore, wbSource As Workbook, wsStore, wsSource As Worksheet, LastRow As Long
Dim FilePath As String, FileName, FullName As String
Dim blnOpened As Boolean
Dim iCount As Integer
FilePath = "C:\Users\ozzy\Desktop\Makro2\" 'this is my path
FileName = "Book2.xlsm"
Call ToggleEvents(False)
Set wsStore = Workbooks(FileName).Sheets("Sheet1")
For iCount = 1 To 3
If iCount <> 2 Then
FullName = FilePath & "Book" & iCount & ".xlsx"
Set wbSource = Workbooks.Open(FullName)
Set wsSource = wbSource.Sheets("Sheet1") 'change destination sheet name here
LastRow = wsStore.Cells.Find(what:="*", after:=wsStore.Cells(1, 1), searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
wsStore.Cells(LastRow, "A").Value = wsSource.Cells(1, "B").Value
wsStore.Cells(LastRow, "B").Value = wsSource.Cells(4, "B").Value
wsStore.Cells(LastRow, "C").Value = wsSource.Cells(7, "B").Value
wsStore.Cells(LastRow, "D").Value = wsSource.Cells(7, "E").Value
wbSource.Close savechanges:=True
End If
Next iCount
Call ToggleEvents(True)
End Sub
Sub ToggleEvents(blnState As Boolean)
With Application
.DisplayAlerts = blnState
.EnableEvents = blnState
.ScreenUpdating = blnState
If blnState Then .CutCopyMode = False
If blnState Then .StatusBar = False
End With
End Sub
Function WbOpen(wbName As String) As Boolean
On Error Resume Next
WbOpen = Len(Workbooks(wbName).Name)
End Function