VBA Print - Help Modify it since it delete active cell before running it

Myproblem

Board Regular
Joined
May 24, 2010
Messages
198
Code below do great job printing my target area, but it also delete, clear active cell after I running this macro.
any idea why, help me modifying it.
thx
Code:
Sub Print_Myproblem()
    Application.EnableEvents = False
    Dim myRange As Range
    Set myRange = Selection
    Dim S As Shape
    On Error GoTo 1
    myRange = ActiveSheet.Range("A1:AG107")
    ActiveSheet.PageSetup.PrintArea = myRange
     
     For Each S In ActiveSheet.Shapes
    S.ControlFormat.PrintObject = False
    Next S
     
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.5)
        .HeaderMargin = Application.InchesToPoints(0.5)
        .FooterMargin = Application.InchesToPoints(0.5)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .PrintErrors = xlPrintErrorsDisplayed
    End With
     
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
1:     Exit Sub
    Application.EnableEvents = True
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
anrew thx for quick reply, but there is no msg that some line cause problem.
I think I resolve problem, setting active cell before running code outside myRange. I tested and it seems it works.
Maybe this is not general rule for this kind of problem, but it works for me.

Dear moderator, please close this thread (if i have right to ask for it)
 
Upvote 0
myRange is declared as a Range object so that line will cause an error (no Set ketword). You won't see a message because you have:

Code:
On Error GoTo 1

immediately before it.
 
Upvote 0

Forum statistics

Threads
1,214,432
Messages
6,119,468
Members
448,900
Latest member
Fairooza

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