+ Reply to Thread
Results 1 to 4 of 4

VBA code for printing a specific area in a Worksheet

  1. #1
    Registered User
    Join Date
    02-15-2013
    Location
    Pretoria, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    13

    VBA code for printing a specific area in a Worksheet

    Hi all,

    I am new to VBA programming. My aim is a macro that will print a specific area in a sheet. Please see the code below. When I press F5 to run VBA gives me a runtime error '1004'. I can't see where the problem might be Please help.

    Sub print_sheet1()

    With Worksheets("sheet1").PageSetup
    .PrintArea = Range("$A$1:$J$50")
    .Orientation = xlPortrait
    .LeftMargin = Application.CentimetersToPoints(2)
    .TopMargin = Application.CentimetersToPoints(2)
    .RightMargin = Application.CentimetersToPoints(1)
    .BottomMargin = Application.CentimetersToPoints(1)
    .Zoom = False
    .FitToPagesTall = 1
    .FitToPagesTall = 1
    End With

    Msg = "Are you sure you want to print Sheet 1?"
    Ans = MsgBox(Msg, vbYesNo)
    If Ans = vbYes Then
    Worksheets("sheet1").PrintOut Copies:=1, Preview:=1
    End If

    End Sub

    Also, you all can see I have not figured out how to include my code above in the "window" in this post. Anyone who can direct me in the right direction?

    Cheers,
    B

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: VBA code for printing a specific area in a Worksheet

    Change the code
    Please Login or Register  to view this content.
    To

    Please Login or Register  to view this content.
    Delete this one

    Please Login or Register  to view this content.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Registered User
    Join Date
    02-15-2013
    Location
    Pretoria, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: VBA code for printing a specific area in a Worksheet

    Thanks Sixthsense!

    I also figured out that a workbook has to be active for the macro to run (where the macro was written). I had two workbooks open and the one without the macro was active, so it gave an error. But I guess that is VBA programming 101.

    On a separate note, how do I post VBA code in the windows as will all/most other posts on this website, the same as Sixthsense done above?

    Cheers
    B

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: VBA code for printing a specific area in a Worksheet

    Select your code and press the # button while you are in draft mode which will add the Code Tags

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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