+ Reply to Thread
Results 1 to 9 of 9

VBA DoUntil Issues

Hybrid View

chicagoland8 VBA DoUntil Issues 04-04-2013, 12:58 PM
patel45 Re: VBA DoUntil Issues 04-04-2013, 02:03 PM
chicagoland8 Re: VBA DoUntil Issues 04-04-2013, 02:11 PM
Norie Re: VBA DoUntil Issues 04-04-2013, 02:18 PM
chicagoland8 Re: VBA DoUntil Issues 04-04-2013, 02:29 PM
Norie Re: VBA DoUntil Issues 04-04-2013, 03:58 PM
chicagoland8 Re: VBA DoUntil Issues 04-04-2013, 04:18 PM
Norie Re: VBA DoUntil Issues 04-04-2013, 04:25 PM
chicagoland8 Re: VBA DoUntil Issues 04-04-2013, 04:36 PM
  1. #1
    Registered User
    Join Date
    10-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007/10
    Posts
    43

    VBA DoUntil Issues

    Hi all--

    having an issue with some VBA code. The main goal is to print the desired sheets based on the numbers entered. Ex. user enters 123, 456, and 789. This will open sheets of those names and print them out. I am trying to design it so that if they enter a number that doesn't have a corresponding workbook named after it, it will skip this one and go on to the next (and doesn't enter "Printed" in the cell next to it.). With my current code, if there is one incorrect number, it works fine. However, if there are two in a row, it stops there and doesn't continue even if there are legitimate numbers after.

    Please let me know if that makes sense, here is my code:


    Sub DailyPrints()
    
    Range("C2:C25").Select
    Selection.ClearContents
    
    Dim CellValue As String
    Dim n As Integer
    
    n = 2
    
    Application.ScreenUpdating = False
    
    Sheets("Daily Prints").Range("A" & n).Select
    
    Do Until IsEmpty(Range("A" & n))
    Application.DisplayAlerts = False
    
    CellValue = Sheets("Daily Prints").Range("A" & n).Value
    Workbooks.Open "Z:\5. Postponement\Pick to Cart\" & CellValue & ".xlsx"
    On Error GoTo 30
    
    Call Prints
    ActiveWindow.Close
    Range("A" & n).Offset(0, 2).Value = "Printed"
    
    30 n = n + 1
    Err.Clear
    Application.Goto (ActiveWorkbook.Sheets("Daily Prints").Range("A" & n))
    
    Loop
    GetOut:
    
    Application.DisplayAlerts = True
    
    End Sub
    Last edited by chicagoland8; 04-04-2013 at 02:30 PM. Reason: code tags

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA DoUntil Issues

    attach a sample file for testing
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    10-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007/10
    Posts
    43

    Re: VBA DoUntil Issues

    Here's my file--may be hard to test since it's opening files connected to a network drive here

    Thanks so much!
    Attached Files Attached Files

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VBA DoUntil Issues

    Try this.
    Sub DailyPrints()
    Dim strFileName As String
    Dim CellValue As String
    Dim n As Integer
    Dim wbOpen As Workbook
    
        Range("C2:C25").ClearContents
        n = 2
    
        Application.ScreenUpdating = False
    
        With Sheets("Daily Prints")
    
            Do Until IsEmpty(.Range("A" & n))
                Application.DisplayAlerts = False
    
                CellValue = Sheets("Daily Prints").Range("A" & n).Value
    
                strFileName = "Z:\5. Postponement\Pick to Cart\" & CellValue & ".xlsx"
    
                If Len(Dir(strFileName)) <> 0 Then
    
                    Set wbOpen = Workbooks.Open(strFileName)
    
                    Call Prints
    
                    wbOpen.Close
                    .Range("A" & n).Offset(0, 2).Value = "Printed"
    
                End If
    
                n = n + 1
    
            Loop
            
        End With
    
    End Sub
    Oh, and when posting code please use code tags.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    10-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007/10
    Posts
    43

    Re: VBA DoUntil Issues

    Thanks a lot, that works perfectly for the print part!

    When trying to use it with the openboms macro, it gets an out of range error...the only thing I changed was get rid of

     Call Prints
    wbOpen.Close
    and change the "Printed" to "Opened." Would I need to change something else for this to work? Thanks so much again for your help!

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VBA DoUntil Issues

    The OpenBOMs sub is never referred to in the code you posted and I can't see how changing anything in the posted code that would affect the sub.

    Why did you get rid of those lines of code?

    They are meant to call the sub Prints, as you were in the original code, and then close the workbook that's just been opened, as you do in the original code.

    Changing 'Printed' to 'Opened' shouldn't affect anything at all, it would only change the value shown next to Part #.

  7. #7
    Registered User
    Join Date
    10-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007/10
    Posts
    43

    Re: VBA DoUntil Issues

    Sorry about the confusion--the OpenBoms was in my attached excel sheet but I didn't mention it in my post. I was able to figure it out though; This part was meant to just open the documents, not prints as in the previous code. I had to reactivate the previous workbook in order for it to work properly.

    Thanks again, all solved!

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VBA DoUntil Issues

    Glad you've got it working, though I admit I'm not sure what exactly you are doing.

    I thought you were openinh each workbook the user had specified, printing from it and then closing it.

    Now it sounds like you are opening all the workbooks at the same time.

    Anyway, it's working that's the important thing.

  9. #9
    Registered User
    Join Date
    10-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007/10
    Posts
    43

    Re: VBA DoUntil Issues

    You are absolutely correct--I was opening each workbook, printing, and closing; that part worked perfectly!

    However, as in my posted example, I also had a second button / VBA code that just opens (doesn't print) each workbook specified. That's the one I slightly modified the first code you posted and wasn't working until I re-activated the "Daily Prints" workbook.

    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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