Help Reviewing My Loop

F

Filo

Hi- Could you help me determine why this program is not looping correctly? In
particular, the loop problem is that it does not open the next kregfile in
the for J loop.

Thank you!

Filo

---------------------------------------

Sub REENTERDETAIL2()


Dim deptnumber As String
Dim j As Integer
Dim Kregfile As String
Dim x As String


Application.ScreenUpdating = False
Application.DisplayAlerts = False

Rownumber = 21

For j = Rownumber To 22

ThisWorkbook.Activate
Sheets(1).Activate

Kregfile = Range("a" & j).Value

If Range("a" & j).Value = "" Then

Else

Workbooks.Open Filename:=Kregfile, UpdateLinks:=0
x = ActiveWorkbook.Name
MsgBox x

Sheets(2).Select
ActiveSheet.Unprotect "bud"

deptnumber = Left(Range("A1").Value, 5)
MsgBox deptnumber

For z = 1 To 65530
ThisWorkbook.Activate
Sheets("Detail").Select
If Cells(z, "A").Value = deptnumber Then

firstrow = z

For m = 65530 To 1 Step -1

If Range("A" & m).Value = deptnumber Then
lastrow = m



For f = firstrow To lastrow

MsgBox f


ThisWorkbook.Activate
Sheets("Detail").Select


t = Range("B" & f).Value
u = Range("F" & f).Value
o = Range("G" & f).Value

For y = 2 To 350

Workbooks(x).Activate
Sheets(2).Select
'" GRAND TOTAL - EXPENSES "

If Range("A" & y).Value = t Then

MsgBox t

s = y

y = 10000

Range("E" & s).Value = u
Range("F" & s).Value = o

Else

End If

Next y

Exit For

Next f

End If

Next m

End If

Next z

End If

Next j


End Sub
 
R

Rick Rothstein \(MVP - VB\)

I haven't reviewed your logic (you should consider indenting your loops to
make your code more readable); however, that Exit For statement between your
Next y and Next f statements looks wrong to me. Won't that jump out of the
"f" loop immediately after the "y" loop finishes without giving the "f" loop
the chance to execute more than one of its own iterations?

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top