VBA works on step through, but not when I run

  • OK, so I'm testing some code. It works to a point on step through. It's a loop with a lot of data so I can't step through it all. However when I want to carry out a test run the code doesn't execute. I can't see why. Any help greatly appreciated. Here is the code.


  • Re: VBA works on step through, but not when I run


    For clarity the code doesn't like this line:


    Code
    SvPath = CurDir & "\"


    Or this line:

    Code
    Mth = InputBox("Enter the current reporting month", "Reporting month entry")


    Both of these lines run when I step through although I haven't got to the point of actually saving a workbook in a step through. Therefore I'm focusing on this line:

    Code
    ActiveWorkbook.SaveAs SvPath & fName & " - M" & Mth & " .xlsx", _
        FileFormat:=51, CreateBackup:=False


    As being the culprit. Any help?

  • Re: VBA works on step through, but not when I run


    Try this:


  • Re: VBA works on step through, but not when I run


    Thanks for your help. Unfortunately that doesn't execute either. This is highlighted when I try to run it.


    Code
    SvPath = .SelectedItems.Item(1) & Application.PathSeparator Else Exit Sub
  • Re: VBA works on step through, but not when I run


    Quote

    However when I want to carry out a test run the code doesn't execute. I can't see why.


    How do you initiate a test run ?
    What happens if you put STOP as the first line of code in the sub ?

  • Re: VBA works on step through, but not when I run


    Quote from NoSparks;799592

    How do you initiate a test run ?
    What happens if you put STOP as the first line of code in the sub ?


    I am pressing Run in the script window. No buttons or whatever. Not heard about putting STOP as the first line of code before. I'll try it. For info though, how do you think that might help?

  • Re: VBA works on step through, but not when I run


    It won't help other than letting you know that execution has stopped at that point in the sub.


    Maybe we need a definition or executing.
    First thing you have is a message box, did I miss you saying it is displayed?

  • Re: VBA works on step through, but not when I run


    An update on this. I have taken it off the network at work and run it on a standalone laptop. It works for a about 4 or 5 loops, (there are 91), and then crashes at the point where I am trying to save the workbook. Despite it having previously saved 4 or 5 other workbooks in the loop.


    Could this be some kind of a storage issue or permissions? I'm baffled. Why would it work in 4 or 5 cases, then crash? I'm going to have to do this manually now which is going to be very annoying.

  • Re: VBA works on step through, but not when I run


    OK, apologies to anyone interested. The save issue was because one of the fName strings had a "/" character in it meaning it wouldn't save.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!