Do While? Or LBound Ubound?

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
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi ozan,
VBA ->Tools-> References
first tick on "Microsoft Scripting Runtime" references
change in code which is in bold.

This code definitely useful for u.
*************************************************************************
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
Dim n as Long
Dim file1 As Folder
Dim fso As FileSystemObject

Set fso = CreateObject("scripting.filesystemobject")
Set file1 = fso.GetFolder("C:\Documents and Settings\yogesh\My Documents")
n = file1.Files.Count
FilePath = "C:\Documents and Settings\yogesh\My Documents" 'this is my path
FileName = "Book2.xlsm"
Call ToggleEvents(False)
Set wsStore = Workbooks(FileName).Sheets("Sheet1")

For iCount = 1 To n
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


*************************************************************************

Yogesh Dalimbkar
yogeshdalimbkar@gmail.com
 
Upvote 0
Hi, Yogiraj, thanks for the help and quick response.

I have still get the same error message. I ticked the tools-->reference->Microsoft Scripting Runtime

Then added the code but still getting the same error message (run-error '1004': C:\...........\Book4.xlsx could not be found.)

Do i have to clear some codes, becasue i only added the codes?

Regards,

Ozan.
 
Upvote 0
Yogiraj, hi i' m sorry, my bad :)

i only adjusted n = file1.Files.Count to n = file1.Files.Count -1

it works perfectly now,

Thank you so much,

Regards,

Ozan.
 
Upvote 0

Forum statistics

Threads
1,216,746
Messages
6,132,478
Members
449,729
Latest member
davelevnt

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