+ Reply to Thread
Results 1 to 27 of 27

Looping Problem

Hybrid View

Spasm Looping Problem 07-08-2011, 08:30 PM
Leith Ross Re: Looping Problem 07-08-2011, 09:12 PM
Spasm Re: Looping Problem 07-08-2011, 09:46 PM
Mordred Re: Looping Problem 07-08-2011, 10:18 PM
Spasm Re: Looping Problem 07-08-2011, 10:28 PM
Mordred Re: Looping Problem 07-08-2011, 10:41 PM
Leith Ross Re: Looping Problem 07-08-2011, 10:50 PM
Spasm Re: Looping Problem 07-08-2011, 11:19 PM
Leith Ross Re: Looping Problem 07-08-2011, 11:22 PM
Spasm Re: Looping Problem 07-08-2011, 10:59 PM
Spasm Re: Looping Problem 07-08-2011, 11:04 PM
Spasm Re: Looping Problem 07-08-2011, 11:23 PM
Spasm Re: Looping Problem 07-08-2011, 11:37 PM
royUK Re: Looping Problem 07-09-2011, 11:11 AM
Marcol Re: Looping Problem 07-09-2011, 12:04 PM
Spasm Re: Looping Problem 07-09-2011, 01:07 PM
Spasm Re: Looping Problem 07-09-2011, 01:33 PM
Spasm Re: Looping Problem 07-09-2011, 01:52 PM
shg Re: Looping Problem 07-09-2011, 02:08 PM
Spasm Re: Looping Problem 07-09-2011, 02:18 PM
Mordred Re: Looping Problem 07-09-2011, 01:43 PM
Mordred Re: Looping Problem 07-09-2011, 02:10 PM
Mordred Re: Looping Problem 07-09-2011, 02:16 PM
Mordred Re: Looping Problem 07-09-2011, 02:20 PM
Spasm Re: Looping Problem 07-09-2011, 02:25 PM
Marcol Re: Looping Problem 07-09-2011, 02:28 PM
Spasm Re: Looping Problem 07-09-2011, 02:32 PM
  1. #1
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Looping Problem

    hi...

    i use the following code to update some files.

     Sub Macro4update()
          Cells(2, 1).Select
        Do While IsEmpty(ActiveCell.Offset(0, 0)) = False
          Range(ActiveCell, ActiveCell.Offset(0, 7)).Select
         Selection.Copy
        Workbooks.Open Filename:=Range(ActiveCell, ActiveCell.Offset(0, 0)).Value
        On Error GoTo ErrHandler:
        ActiveCell.Offset(1, 0).Select
        ActiveSheet.Paste
        Application.DisplayAlerts = False
        ActiveWorkbook.Save
        ActiveWindow.Close
    Label1:
        ActiveCell.Offset(1, 0).Select
           Loop
    ErrHandler:
          Resume Label1
    End Sub
    When it reaches the last row, it keeps on looping.
    i want it to stop after the last row is done......

    Please help...

    Thanks






    Additional details:-

    the cell values in the first column are the names of the files stored on my hard disk,
    if a file is not present on my hard disk for upgradation, it will give error...
    hence i m forced on using the error handler,
    but its messing up the loop.

    Plz help
    Last edited by Spasm; 07-09-2011 at 02:29 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Looping Problem

    Hello Spasm,

    Here is your macro with a few revisions.
     Sub Macro4update()
     
       Dim Cell As Range
       Dim R As Long
       
         Set Cell = Cells(2, "A")
       
         While Not IsEmpty(Cell.Offset(R, 0))
           If Dir(Cell) <> "" Then
              Cell.Resize(1, 8).Copy
              Workbooks.Open Filename:=Cell.Value
                ActiveCell.Offset(1, 0).PasteSpecial Paste:=xlPasteAll
              ActiveWorkbook.Close SaveChanges:=True
              R = R + 1
           End If
         Wend
       
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    hi....
    thank u for helping me with the code
    but it gives an error at
    END IF
    ( 3rd line from bottom )

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Looping Problem

    Does the following work?
    Sub Macro4update()
          Cells(2, 1).Select
          Do While IsEmpty(Cells(2, 1)) = False
            Set rng1 = Range(Cells(2, 1), Cells(2, 1).Offset(0, 7))
            On Error Resume Next
            Workbooks.Open Filename:=Cells(2, 1).Value
            Set rng2 = Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(1, 7))
            rng2.Cells.Value = rng1.Cells.Value
            ActiveWorkbook.Save
            ActiveWindow.Close
          Loop
    End Sub
    This was not tested except for at compile time. I really don't know if it will do what you want at run-time. I was just playing with your code.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  5. #5
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    thank U for ur effort
    but the code has some problems
    i tried it....
    it keeps on looping on the 2nd row... it does not increment,, and it also keeps on prompting to save the file...

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Looping Problem

    Maybe:
    Option Explicit
    Sub Macro4update()
        Dim Rng1 As Range, Rng2 As Range
        Dim RwCnt As Long, x As Long
        RwCnt = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
          For x = 2 To RwCnt
          If IsEmpty(Cells(x, 1)) = False Then
            Set Rng1 = Range(Cells(x, 1), Cells(x, 1).Offset(0, 7))
            On Error Resume Next
            Workbooks.Open Filename:=Cells(x, 1).Value
            Set Rng2 = Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(1, 7))
            Rng2.Cells.Value = Rng1.Cells.Value
            ActiveWorkbook.Save
            ActiveWindow.Close
          End If
          Next x
    End Sub
    Last edited by Mordred; 07-08-2011 at 10:45 PM.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Looping Problem

    Hello Spasm,

    Sorry, I forgot to exit when the cell is empty. Try this instead...
     Sub Macro4update()
     
       Dim Cell As Range
       Dim R As Long
       
         Set Cell = Cells(2, "A")
       
         While Not IsEmpty(Cell.Offset(R, 0))
           If Dir(Cell) <> "" Then
              Cell.Resize(1, 8).Copy
              Workbooks.Open Filename:=Cell.Value
                ActiveCell.Offset(1, 0).PasteSpecial Paste:=xlPasteAll
              ActiveWorkbook.Close SaveChanges:=True
              R = R + 1
           Else
              Exit Sub
           End If
         Wend
       
    End Sub

  8. #8
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    hi Leith Ross
    i tried the second code
    but it didnt run at all


    thank u for ur effort n time

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Looping Problem

    Hello Spasm,

    I can not help you further on this problem because I have no idea what you are doing on your end that is causing the code to fail. The code works on my machine and I am running Excel 2003 under Windows XP. Perhaps, you will figure it out in time.

  10. #10
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    thanks ......
    Last edited by Spasm; 07-08-2011 at 11:02 PM.

  11. #11
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    thanks

  12. #12
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    hi Mordred
    i tried ur second code
    it increments but always prompts to save the file
    and when an error occurs,,, it stops incrementing and auto closes itself

    thanks for ur effort

  13. #13
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    its ok


    i m sorry for not providing enough information about my problem

    thanks for your time and effort
    Last edited by Spasm; 07-09-2011 at 06:47 AM.

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Looping Problem

    This is a duplicate post and as such does not comply with Rule 5 of our forum rules. This thread will now be closed, you may continue in your other thread.

    Thread Closed.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  15. #15
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Looping Problem

    Hmm?
    We seem to be in a bit of a mess here!

    Is this thread closed or not?
    The duplicate certainly is and that's where I posted a solution that works for me!

  16. #16
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    ok
    thanks

  17. #17
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    2 files for my code problem
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    ur code did everything perfectly as i wanted
    but once the last row was done
    it kept on looping until row 65536
    Last edited by Spasm; 07-09-2011 at 02:01 PM.

  19. #19
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Looping Problem

    At a glance, you need an Exit Sub statement after the Loop statement.
    Entia non sunt multiplicanda sine necessitate

  20. #20
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    yesssssssssss yaaaaaayyyy,,
    i added EXIT SUB after the loop, and the macro worked perfectly fine.. did everything perfectly and didnt kept on looping after the last row

    thank U Mordred
    &
    thank u SHG

    for your amazing help and for giving ur precious time to solve my problem

    thank u ALL

  21. #21
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Looping Problem

    I added one line of code to your existing code from post 1, will it work? Maybe.
     Sub Macro4update()
          Cells(2, 1).Select
        Do While IsEmpty(ActiveCell.Offset(0, 0)) = False
          Range(ActiveCell, ActiveCell.Offset(0, 7)).Select
         Selection.Copy
        Workbooks.Open Filename:=Range(ActiveCell, ActiveCell.Offset(0, 0)).Value
        On Error GoTo ErrHandler:
        ActiveCell.Offset(1, 0).Select
        ActiveSheet.Paste
        Application.DisplayAlerts = False
        ActiveWorkbook.Save
        ActiveWindow.Close
    Label1:
        ActiveCell.Offset(1, 0).Select
        If ActiveCell.Value = "" Then Exit Do
           Loop
    ErrHandler:
          Resume Label1
    End Sub
    I added
    If ActiveCell.Value = "" Then Exit Do

  22. #22
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Looping Problem

    Have you tried the code that I provided in post #18? shg is no doubt right so maybe instead of
    Exit Do
    you should have
    Exit Sub

  23. #23
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Looping Problem

    It could be
    Label1:
        ActiveCell.Offset(1, 0).Select
        If ActiveCell.Value = "" Then Exit Do
           Loop
    ErrHandler:
          Resume Label1
    that is causing the most problems, specifically
    Resume Label1
    which puts you right back into the loop.

  24. #24
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Looping Problem

    We are glad to help. If you feel that your problem has been solved, mark this thread as solved and, if you are happy with the help you have received, don't be shy and tap the scales of those that helped.

    Cheers

  25. #25
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    yes .... added reputation for both of u

  26. #26
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Looping Problem

    The solution I offered in your closed duplicate thread works, and solves other problems that might arrise.

    You PMd me to say it doesn't work, I can't see why this should be the case, but if your happy, so am I.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  27. #27
    Registered User
    Join Date
    07-07-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Looping Problem

    no no.... plz dont take me wrong, ur code has to b precise
    i appreciate what u did for me

    it was my fault
    i hadnt mentioned precisely what i wanted to do with the code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1