Loop until a cell is a certain number

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have some code below where H8 is an invoice number which I will start at the number 4000. Cell Z1 is a customer name.

I then want to run this code until all invoices are ran up to number 4300. So in essences running this piece of code 300 times with Invoice 4000 being the first one, 4001 the second and so on. Could someone advise please and what i need to add 1 to change cell H8 after each time the code is run and then run the code again. I am guessing its Do until but not sure how to implement, TIA

VBA Code:
Sub saveinvoice()

Call hide

v = "Invoice " & ActiveSheet.Range("H8") & "-" & ActiveSheet.Range("Z1") & ".pdf"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Invoice " & ActiveSheet.Range("H8") & "-" & ActiveSheet.Range("Z1") & ".pdf", Quality _
        :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

Range("B1:I204").Select
    ActiveSheet.PageSetup.PrintArea = "$B$1:$I$204"


     
    With ActiveSheet
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=v, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, From:=1, To:=16, OpenAfterPublish:=False
    End With
  

Call unhide

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
How about this:

VBA Code:
Sub saveinvoice()

    Dim x As Long, v As String

    Call Hide
    
    For x = 4000 To 4300
        v = "Invoice " & x & "-" & ActiveSheet.Range("Z1") & ".pdf"
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Invoice " & x & "-" & ActiveSheet.Range("Z1") & ".pdf", Quality _
            :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
            OpenAfterPublish:=False

        Range("B1:I204").Select
        ActiveSheet.PageSetup.PrintArea = "$B$1:$I$204"
     
        With ActiveSheet
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=v, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, From:=1, to:=16, OpenAfterPublish:=False
        End With
    Next

    Call unhide

End Sub
 
Upvote 0
Solution
Thanks but that runs the code in a loop but doesnt change the value in Cell H8 (the invoice number) by 1 each time meaning its essentially PDFing the exact sale thing every time just calling it something else.

So the steps if doing manually would be.
1. Enter 4000 in cell H8
2. PDF invoice
3. Change cell H8 to 4001
4. Repeat step 2
5. Change cell H8 to 4002

and so on
 
Upvote 0
How about this:

VBA Code:
Sub saveinvoice()

    Dim x As Long, v As String

    Call Hide
   
    For x = 4000 To 4300
        v = "Invoice " & x & "-" & ActiveSheet.Range("Z1") & ".pdf"
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Invoice " & x & "-" & ActiveSheet.Range("Z1") & ".pdf", Quality _
            :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
            OpenAfterPublish:=False

        Range("B1:I204").Select
        ActiveSheet.PageSetup.PrintArea = "$B$1:$I$204"
    
        With ActiveSheet
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=v, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, From:=1, to:=16, OpenAfterPublish:=False
        End With
    Next

    Call unhide

End Sub

Worked it out, thanks for your help

VBA Code:
Sub saveinvoice()

    Dim x As Long, v As String

    
    
    For x = 4000 To 4300
    Call hide
        v = "Invoice " & x & "-" & ActiveSheet.Range("Z1") & ".pdf"
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Invoice " & x & "-" & ActiveSheet.Range("Z1") & ".pdf", Quality _
            :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
            OpenAfterPublish:=False

        Range("B1:I204").Select
        ActiveSheet.PageSetup.PrintArea = "$B$1:$I$204"
     
        With ActiveSheet
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=v, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, From:=1, To:=16, OpenAfterPublish:=False
        End With
        Call unhide
        Range("H8") = Range("H8") + 1
    Next

    

End Sub
 
Upvote 0
I am glad you got it working. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,146
Messages
6,129,125
Members
449,488
Latest member
qh017

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top