Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. excel.sheet.setprintArea

excel.sheet.setprintArea

Scheduled Pinned Locked Moved Visual Basic
questionhelptutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pnpfriend
    wrote on last edited by
    #1

    Hello Gurus... I want the exiting excel file to open it, format it and print it. opening and formating part I have ( mostly) done ... but to printing giving me problems.. when the file get print, there are lots of empty pages because of the last cell. let's say, the sheet contain only two cells that has some data. the first cell is A1 and the last cell is z130 and there are no data in the cells between first and last cells. So when I call PrintOut method it prints all the empty pages. I'm trying to set the print area for the excel sheet because I don't want to print empty pages. But how can i set the print area only to print that two cells??? I tried look every cells whether if it is empty or not, if empty don't do anything but if not empty, set the range and set the printarea... the following is my code..I know it is wrong but don't know how to fix it Function SetPrintArea(sheet As Worksheet) As Worksheet Dim r As Range Set r = sheet.UsedRange Dim newRange As Range Dim i As Integer Dim j As Integer For i = 1 To r.Rows.Count For j = 1 To r.Columns.Count If Not Cells(i, j).Value = "" Then newRange = newRange(Cells(1, i), Cells(i, j)) End If Next j Next i newRange.Cells.Select sheet.PageSetup.PrintArea = Selection.Address Set SetPrintArea = sheet End Function Thanks

    N 1 Reply Last reply
    0
    • P pnpfriend

      Hello Gurus... I want the exiting excel file to open it, format it and print it. opening and formating part I have ( mostly) done ... but to printing giving me problems.. when the file get print, there are lots of empty pages because of the last cell. let's say, the sheet contain only two cells that has some data. the first cell is A1 and the last cell is z130 and there are no data in the cells between first and last cells. So when I call PrintOut method it prints all the empty pages. I'm trying to set the print area for the excel sheet because I don't want to print empty pages. But how can i set the print area only to print that two cells??? I tried look every cells whether if it is empty or not, if empty don't do anything but if not empty, set the range and set the printarea... the following is my code..I know it is wrong but don't know how to fix it Function SetPrintArea(sheet As Worksheet) As Worksheet Dim r As Range Set r = sheet.UsedRange Dim newRange As Range Dim i As Integer Dim j As Integer For i = 1 To r.Rows.Count For j = 1 To r.Columns.Count If Not Cells(i, j).Value = "" Then newRange = newRange(Cells(1, i), Cells(i, j)) End If Next j Next i newRange.Cells.Select sheet.PageSetup.PrintArea = Selection.Address Set SetPrintArea = sheet End Function Thanks

      N Offline
      N Offline
      Nick Seng
      wrote on last edited by
      #2

      okay, I'm gonna ask the obvious question.... why are the only two cells in the sheet with the data so frikkin' far ????. That's gotta say something bout the design! the Range object also accepts parameters in the form of a string, like so : "A1:C5" The above parameter would include everything from cells A1 to cells C5 But if you wanna do composite ranges, then the following string: "A1:C5,D12:G7" would include everything from the first string as well as everything from cell D12 to G7 and nothing in between. I assume you could solve your problem by constructing the composite range string before hand, then pass it into the Range object Hope that helps Notorious SMC


      The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
      Get your facts first, and then you can distort them as much as you please Mark Twain

      P 1 Reply Last reply
      0
      • N Nick Seng

        okay, I'm gonna ask the obvious question.... why are the only two cells in the sheet with the data so frikkin' far ????. That's gotta say something bout the design! the Range object also accepts parameters in the form of a string, like so : "A1:C5" The above parameter would include everything from cells A1 to cells C5 But if you wanna do composite ranges, then the following string: "A1:C5,D12:G7" would include everything from the first string as well as everything from cell D12 to G7 and nothing in between. I assume you could solve your problem by constructing the composite range string before hand, then pass it into the Range object Hope that helps Notorious SMC


        The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
        Get your facts first, and then you can distort them as much as you please Mark Twain

        P Offline
        P Offline
        pnpfriend
        wrote on last edited by
        #3

        You're damn right.. why do they do so?? Can't they just insert the information into the closer cells?? I, myself, want to know why they do that.. my job is to print that excel file..minimizing the number of pages that will print as possible. Thank you for answer SMC, So how can i get the position of the cell? Thank you

        N 1 Reply Last reply
        0
        • P pnpfriend

          You're damn right.. why do they do so?? Can't they just insert the information into the closer cells?? I, myself, want to know why they do that.. my job is to print that excel file..minimizing the number of pages that will print as possible. Thank you for answer SMC, So how can i get the position of the cell? Thank you

          N Offline
          N Offline
          Nick Seng
          wrote on last edited by
          #4

          you're looping method seem to work fine.... just don't assign the cells into the range yet. Convert into the string version first, then after evrything has finished looping, assign the string into the range Notorious SMC


          The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
          Get your facts first, and then you can distort them as much as you please Mark Twain

          P 1 Reply Last reply
          0
          • N Nick Seng

            you're looping method seem to work fine.... just don't assign the cells into the range yet. Convert into the string version first, then after evrything has finished looping, assign the string into the range Notorious SMC


            The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
            Get your facts first, and then you can distort them as much as you please Mark Twain

            P Offline
            P Offline
            pnpfriend
            wrote on last edited by
            #5

            thanks. I did the samething.. Is there anyway that I can find out whether the sheet(s) has any protection?? what if I don't know the password?? Thanks

            N 1 Reply Last reply
            0
            • P pnpfriend

              thanks. I did the samething.. Is there anyway that I can find out whether the sheet(s) has any protection?? what if I don't know the password?? Thanks

              N Offline
              N Offline
              Nick Seng
              wrote on last edited by
              #6

              Win wrote: Is there anyway that I can find out whether the sheet(s) has any protection?? what if I don't know the password?? err..... sorry, don't know much in that area. Notorious SMC


              The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
              Get your facts first, and then you can distort them as much as you please Mark Twain

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups