Microsoft Excel - How to get 3 statements for next to work in order

Asked By Cherifa Hima on 11-Sep-15 03:36 PM
Hi , I am trying to get this code work in a way the 3 for next statements work in order, first change r, then f, then s . The next  statement right now is going straight to S. I am trying to copy sheets from one file to different files with different names in different folders.
Thank you

Sub Macro1()
Dim directory As String, sheet As Worksheet, i As Integer, j As Integer, fileName As String, r As Range, A1 As Range
Dim sh As Worksheet, s As Range, MySource As Object, f As Range, FName As String, fs As Range, wkb As Workbook, FPath As String
Application.ScreenUpdating = False


Sheets("Departments codes").Select

For Each r In Sheets("Departments codes").Range("l6", Range("l" & Rows.Count).End(xlUp)) ' file name

For Each f In Sheets("Departments codes").Range("H6", Range("H" & Rows.Count).End(xlUp))
 
For Each s In Sheets("Departments codes").Range("k6", Range("k" & Rows.Count).End(xlUp))
Sheets("Departments codes").Select
 fileName = r.Value
 Set sh = Worksheets(s.Value)
 
 FName = f.Value & ".xlsm"

   
   Workbooks.Open (fileName)
   Windows("Monthly Results Comments.xlsm").Activate
   sh.Select
    sh.Copy Before:=Workbooks(FName).Sheets(2)
   
   
    Range("H20").Select
    ActiveWindow.SmallScroll Down:=-18
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("B13").Select
    Application.CutCopyMode = False
    ActiveWindow.SmallScroll Down:=-3
    ActiveWorkbook.Save
    ActiveWindow.Close
   
   
  
    Next r
   
    Next f
   
Next s
   
   
End Sub



Robbe Morris replied to Cherifa Hima on 12-Sep-15 10:01 AM
Your "next" statements are out of order

should be

next s
next f
next r
Cherifa Hima replied to Robbe Morris on 14-Sep-15 08:36 AM
when I put next s Next f, Next r, it starts with s1,s2... until s is done, then goes to f, then to r. That's not what I am looking for. I want the Next to get first s1, then f1, then r1 at the same time. then  get s2,f2,r2 and so on
Robbe Morris replied to Cherifa Hima on 14-Sep-15 08:39 AM
For next has to be in matching order

  for a
    for b
   for c
   next c
    next b
  next a

Your code has them out of order.
Cherifa Hima replied to Robbe Morris on 14-Sep-15 10:12 AM
The code is working when I do what you said. But it is not running in the order I want. It is running only Next s until s range is done, then  next f until f range is done, then next r until r range is done . I want the three variables to change all in one next statement.  Like the first next for S, should grab s1, after that, the first next for f, should get f1, and the first next for r should get r1, in that order. Not get s1, next s2, next s3..... then go to f, and get f1, f2, fe3 and  then get  r1, r2, and r3 .....
Robbe Morris replied to Cherifa Hima on 14-Sep-15 10:17 AM
For Next logic is not going to dynamically let you jump all over the place and still remember where it supposed to be.

You are going to have to write different logic than what you have here.  Good luck.
Harry Boughen replied to Cherifa Hima on 14-Sep-15 10:57 AM
Hello Chirifa,

Perhaps you could do it in one For-Next loop and use offsets to reference the other two columns that your data is in as they are all obviously on the same page and (presumably) the same size (column length).

I don't have reliable access at the moment or access to excel so any further help might be limited until I get back in  about 10 days.

Harry
Cherifa Hima replied to Harry Boughen on 14-Sep-15 02:23 PM
Thanks
Harry Boughen replied to Cherifa Hima on 27-Sep-15 03:49 PM
Hello Cherifa,

Did you get your problem sorted?

I am back on deck now.

Regards

Harry