macro to print range of IDs

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello All,
I have this macro that i use to print in my workbook. Instead of it printing from 1 always, i want it to use an inputbox to specify where to start from and where to end at. For example when i run it, it should ask me for entry for start ID and end ID. Thanks
Code:
Sub Print()
         Dim CountIt, IdNum As Long
CountIt = Application.InputBox("How many IDs do you wanna print?", Type:=1)
For IdNum = 1 To CountIt
      With ActiveSheet
             .Range("G6").Value = IdNum
             .PrintOut
      End With
Next IdNum
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Just change the For statement:

Code:
Sub PrintIDs()
         Dim StartID As Long
         Dim EndID As Long
         Dim IdNum As Long
         
StartID = Application.InputBox("Enter ID to start printing at", Type:=1)
EndID = Application.InputBox("Enter ID to finish printing at", Type:=1)
For IdNum = StartID To EndID
      With ActiveSheet
             .Range("G6").Value = IdNum
             .Printout
      End With
Next IdNum
End Sub
 
Upvote 0
Just change the For statement:

Code:
Sub PrintIDs()
         Dim StartID As Long
         Dim EndID As Long
         Dim IdNum As Long
         
StartID = Application.InputBox("Enter ID to start printing at", Type:=1)
EndID = Application.InputBox("Enter ID to finish printing at", Type:=1)
For IdNum = StartID To EndID
      With ActiveSheet
             .Range("G6").Value = IdNum
             .Printout
      End With
Next IdNum
End Sub

Thanks very much. Wanna run test on it will verify when through
 
Upvote 0
Okay it it working fine. Just that when there is no input in the inputnox and i click cancel it takes it as zero(0) and sets the .Range("G6").Value = 0. I want to exit sub when the inputboxes did not take any input or when i click cancel. What should i do?

Regards
Kelly
 
Upvote 0
Check the value once it has been input:

Code:
Sub PrintIDs()
         Dim StartID As Long
         Dim EndID As Long
         Dim IdNum As Long
         
StartID = Application.InputBox("Enter ID to start printing at", Type:=1)
If StartID = vbCancel Or StartID = 0 Then MsgBox "Cancelled": Exit Sub

EndID = Application.InputBox("Enter ID to finish printing at", Type:=1)
If EndID = vbCancel Or EndID = 0 Then MsgBox "Cancelled": Exit Sub

For IdNum = StartID To EndID
      With ActiveSheet
             .Range("G6").Value = IdNum
             .PrintOut
      End With
Next IdNum
End Sub
 
Upvote 0
Okay working. And one thing what if there is no entry and i click okay? I want it show a message box prompting me to input ID.

Thanks
Kelly
 
Upvote 0
I tried this but did not work any help?
Code:
If StartID = vbOk And StartID = 0 Then MsgBox "Cancelled": Exit Sub
 
Upvote 0
Still need help with the vbOk button behaviour. Thanks
 
Upvote 0
Hi Kelly,

You only need one InputBox with a defined character (I used a dash in the following) to split the to and from values i.e. try this:

Code:
Option Explicit
Sub Macro1()

    Dim strMyArray() As String
    Dim dblMyNumber  As Double
    Dim strReponse   As String
    
MyInputBox:
    
    strReponse = InputBox("Enter the ID's to print from and to like so 1-20 for ID's 1 to 20:", "ID Range Selection")
    
    'Quit if the <Cancel> button has been pressed or no entry was made
    If Len(strReponse) = 0 Then
        Exit Sub
    'Ensure the reponse has a dash in it
    ElseIf InStr(strReponse, "-") = 0 Then
        If MsgBox("A valid entry like 1-20 was not made." & vbNewLine & "Try again?", vbYesNo + vbCritical) = vbYes Then
            GoTo MyInputBox
        Else
            Exit Sub
        End If
    End If
    
    strMyArray() = Split(strReponse, "-")
    
    'Ensure both to and from are numeric
    If IsNumeric(strMyArray(0)) = False Or IsNumeric(strMyArray(1)) = False Then
        If MsgBox("A valid entry like 1-20 was not made." & vbNewLine & "Try again?", vbYesNo + vbCritical) = vbYes Then
            GoTo MyInputBox
        Else
            Exit Sub
        End If
    'Ensure both to and from are positive
    ElseIf Val(strMyArray(0)) <= 0 Or Val(strMyArray(1)) <= 0 Then
        If MsgBox("A valid entry like 1-20 was not made." & vbNewLine & "Try again?", vbYesNo + vbCritical) = vbYes Then
            GoTo MyInputBox
        Else
            Exit Sub
        End If
    End If
    
    'If we get here print out the selected ID's onre-by-one
    Application.ScreenUpdating = False
    
    For dblMyNumber = strMyArray(0) To strMyArray(1)
        With ActiveSheet
            .Range("G6").Value = Val(dblMyNumber)
            .PrintOut
        End With
    Next dblMyNumber
    
    Application.ScreenUpdating = True
    
    MsgBox "ID's have now been printed.", vbInformation

End Sub

I've made some notes along the way to show what's happening should you need to tweak the macro.

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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