Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop within a loop not letting ( variable = dir ) work

Status
Not open for further replies.

stibbetts

Technical User
May 6, 2004
64
US
i have a loop that does a couple things there is a counter and debug.print and a couple other litle things, but inside this loop is another loop, this loop runs till its done then its supposed to go back to the other loop like this:

public sub loopy()

myname = dir(Specified path, vbdirectory)

do while cntr <= 8
myname = dir
debug.print dir
do while cntrx<=6
'does stuff
cntrx=cntrx+1
loop
cntr = cntr+1
loop
end sub

the sub wont cycle to the next 'myname' in this situation however if i get rid of the second loop like this it works fine:

do while cntr <= 8
myname = dir
debug.print dir
cntr = cntr+1
loop
end sub

is ther eosme rule that doesnt let this loop in a loop work?

 
Hi stibbetts,

No reason it shouldn't work based on what you've posted. How about posting the "stuff" it does so we can see if we can find the problem.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
here's the whole messy sub, the iptname2=dir works fine but the mynamex=dir wont work if the second loop is there, if i delete it then everything works fine. any variables not defined here are defined as public in the declarations area of the macro:


Public Sub PackNGo()

Dim SpecificTemplateFolder, TemplatePath, IPTName2
SpecificTemplateFolder = dlgimport.cmbTemplateType.Text
iptpath2 = "W:\Inventor\Templates\Drawing Models\" & SpecificTemplateFolder & "\*.ipt"
IPTPath3 = "W:\Inventor\Templates\Drawing Models\" & SpecificTemplateFolder & "\"

mypathx = "W:\LHW_Jobs\" & myjob & "\Design\DWGS\"

Dim thingy, TemplateFilePath, icntr
mynamex = Dir(mypathx, vbDirectory)
Do While rep1 <= folderqty - 3

Debug.Print mypathx & mynamex
rep = 1
IPTName2 = Dir(iptpath2)
icntr = 1

Do While rep <= iptqty - 1
IPTName2 = Dir
chkbxnumber = (rep) + (rep1 * (iptqty - 1))
TemplateFilePath = IPTPath3 & IPTName2
If GetCheck("CheckBox" & chkbxnumber) = True Then
Debug.Print (TemplateFilePath)
End If
rep = rep + 1
Loop

rep1 = rep1 + 1
mynamex = Dir
Debug.Print " "
Loop

End Sub
 
A problem I see is that cntrx is never reset.
So you execute the inner loop only once as cntrx is already = 7 on the second iteration of the outer loop.
You may consider using For ... Next construct, like this:
For cntr = 0 To 8
myname = dir
debug.print myname
For cntrx = 0 To 6
'does stuff
Next cntrx
Next cntr

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
ok i have narrowed down the problem a little bit, it seems that the mynamex = dir workd fine right up untill i add the iptname2 = dir(iptpath2) at wich point it returns the value for iptname2 instead of mynamex when i tell it to debug.print. so frusterating, any ideas?
 
You can't use nested Dir iteration.
You may consider using the FileSystemObject of the Microsoft Scripting Runtime Library.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
that does what i want for the most part the only problem is that there are 5 .IPT files that i want and then there are a couple hidden files or system files or something that it picks up that arent actually in the file, this throws my count off, is there any way to cull out the bad files?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top