Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

pop message when print again for the same range

0

hello

I  have  this  macro .  what  I  want   when  print   repeatedly   after  first  time  it  should  show  message    "the  data  has  already printed", do  you want   printing   again? and  gives  me    two  choices     if  press ok print  , if  press no  nothing  happens. 

Sub sPrint()
    Set sh = Sheets("print")
With sh
    If .Range("b2") = "" Then
    MsgBox "there is  no data ", vbInformation + vbMsgBoxRight, "print "
    Exit Sub
    End If
    Last = .Cells(.Rows.Count, 6).End(xlUp).Row
   .PageSetup.PrintArea = "a1:f1" & Last
   .PrintOut
End With
End Sub

Answer
Discuss

Answers

0
Selected Answer

Halk

If you're only interested in repeating the same range (not and chnaged content in that), then the code below will work for you.

You'll see that I've added a new varaible NewPrint and created a stringso it can be compared with what VBA returns as the PrintArea (i.e. including the $ signs). Key changes on that are in bold below:

Sub sPrint()
    Set sh = Sheets("print")
With sh
    If .Range("B2") = "" Then
        MsgBox "There is  no data in column F", vbInformation + vbMsgBoxRight, "Print"
        Exit Sub
    End If

    Last = .Cells(.Rows.Count, 6).End(xlUp).Row
    NewPrint = "$A$1:$F$" & Last

    If NewPrint = .PageSetup.PrintArea Then ' Check if print area has changed
        r = MsgBox("This area has been printed; do you want to print it again?", vbYesNo, "Range unchanged")
        If r = vbNo Then Exit Sub ' if the user says no, quit macro
    End If
    'otherwise proceed to print...
    .PageSetup.PrintArea = NewPrint
    .PrintOut
End With
End Sub
Discuss

Discussion

John
you're  legend    !  thanks  very much
Halk (rep: 30) Jun 29, '21 at 4:05 am
Thanks Halk, glad it worked.
John_Ru (rep: 6152) Jun 29, '21 at 4:08 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login