Zoom couple of columns to fit page with VBA











up vote
2
down vote

favorite












I'm having trouble fitting my columns in Excel on a sheet.
I have a sheet with columns from A to CK (can be different per project).
I don't need to print column A, but column B has to be on all pages and next to column B has to be 3 columns. So that will make column "B,C:E" on first page, next page "B,F:H", and so on... Column B is set as title, so it will be printed on every page.



My problem is to set the scale. What I'm doing:




  • Take pagesize and translate to points, take off margin left and margin right = my printable area

  • Get the width of range("B:E") = my range to fit the page

  • Divide my printable area by my range to fit, multiply that with 100%, and extract 1% to make sure it will fit


The outcome in my situation is 83, but is has to be 77 to fit the page. I'll have to find other numbers I think, but I don't know how and which...



My code:



If ActiveSheet.Name = "Meterkastlijst" Then
Dim lngZoom As Long
Dim lngKolB As Long
Dim lngPagB As Long
lngKolB = ActiveSheet.Range("B:E").Width
If ActiveSheet.PageSetup.PaperSize = xlPaperA4 Then
lngPagB = CLng(Application.CentimetersToPoints(21)) - CLng((ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin))
ElseIf ActiveSheet.PageSetup.PaperSize = xlPaperA3 Then
lngPagB = CLng(Application.CentimetersToPoints(29.7)) - CLng((ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin))
End If
If lngPagB <> 0 And lngKolB <> 0 Then
lngZoom = ((lngPagB / lngKolB) * 100) - 1
With ActiveSheet.PageSetup
.Zoom = lngZoom
End With
End If
End If


Different widths:




  • Column B: 45 (319 pixels) -> in Excel, set with VBA

  • Column C: 15 (109 pixels) -> in Excel, set with VBA

  • Column D: 30 (214 pixels) -> in Excel, set with VBA

  • Column E: 20 (144 pixels) -> in Excel, set with VBA

  • Column B-E: 589 points -> with VBA

  • Page: 21 centimeters (595 points)

  • Margins (left & right): 1.8 centimeters (50.4 points)

  • Print area: 595 - 101 (100.8) = 494 points


With numbers above it calculates 83%, but then it doesn't fit, when I set it manually to 77% it does fit, but how can I get this number with VBA? I don't understand the column widths, what I see in Excel and how I set it in VBA (45+15+30+20) is different from what VBA tells me it should be (589)...










share|improve this question


























    up vote
    2
    down vote

    favorite












    I'm having trouble fitting my columns in Excel on a sheet.
    I have a sheet with columns from A to CK (can be different per project).
    I don't need to print column A, but column B has to be on all pages and next to column B has to be 3 columns. So that will make column "B,C:E" on first page, next page "B,F:H", and so on... Column B is set as title, so it will be printed on every page.



    My problem is to set the scale. What I'm doing:




    • Take pagesize and translate to points, take off margin left and margin right = my printable area

    • Get the width of range("B:E") = my range to fit the page

    • Divide my printable area by my range to fit, multiply that with 100%, and extract 1% to make sure it will fit


    The outcome in my situation is 83, but is has to be 77 to fit the page. I'll have to find other numbers I think, but I don't know how and which...



    My code:



    If ActiveSheet.Name = "Meterkastlijst" Then
    Dim lngZoom As Long
    Dim lngKolB As Long
    Dim lngPagB As Long
    lngKolB = ActiveSheet.Range("B:E").Width
    If ActiveSheet.PageSetup.PaperSize = xlPaperA4 Then
    lngPagB = CLng(Application.CentimetersToPoints(21)) - CLng((ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin))
    ElseIf ActiveSheet.PageSetup.PaperSize = xlPaperA3 Then
    lngPagB = CLng(Application.CentimetersToPoints(29.7)) - CLng((ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin))
    End If
    If lngPagB <> 0 And lngKolB <> 0 Then
    lngZoom = ((lngPagB / lngKolB) * 100) - 1
    With ActiveSheet.PageSetup
    .Zoom = lngZoom
    End With
    End If
    End If


    Different widths:




    • Column B: 45 (319 pixels) -> in Excel, set with VBA

    • Column C: 15 (109 pixels) -> in Excel, set with VBA

    • Column D: 30 (214 pixels) -> in Excel, set with VBA

    • Column E: 20 (144 pixels) -> in Excel, set with VBA

    • Column B-E: 589 points -> with VBA

    • Page: 21 centimeters (595 points)

    • Margins (left & right): 1.8 centimeters (50.4 points)

    • Print area: 595 - 101 (100.8) = 494 points


    With numbers above it calculates 83%, but then it doesn't fit, when I set it manually to 77% it does fit, but how can I get this number with VBA? I don't understand the column widths, what I see in Excel and how I set it in VBA (45+15+30+20) is different from what VBA tells me it should be (589)...










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm having trouble fitting my columns in Excel on a sheet.
      I have a sheet with columns from A to CK (can be different per project).
      I don't need to print column A, but column B has to be on all pages and next to column B has to be 3 columns. So that will make column "B,C:E" on first page, next page "B,F:H", and so on... Column B is set as title, so it will be printed on every page.



      My problem is to set the scale. What I'm doing:




      • Take pagesize and translate to points, take off margin left and margin right = my printable area

      • Get the width of range("B:E") = my range to fit the page

      • Divide my printable area by my range to fit, multiply that with 100%, and extract 1% to make sure it will fit


      The outcome in my situation is 83, but is has to be 77 to fit the page. I'll have to find other numbers I think, but I don't know how and which...



      My code:



      If ActiveSheet.Name = "Meterkastlijst" Then
      Dim lngZoom As Long
      Dim lngKolB As Long
      Dim lngPagB As Long
      lngKolB = ActiveSheet.Range("B:E").Width
      If ActiveSheet.PageSetup.PaperSize = xlPaperA4 Then
      lngPagB = CLng(Application.CentimetersToPoints(21)) - CLng((ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin))
      ElseIf ActiveSheet.PageSetup.PaperSize = xlPaperA3 Then
      lngPagB = CLng(Application.CentimetersToPoints(29.7)) - CLng((ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin))
      End If
      If lngPagB <> 0 And lngKolB <> 0 Then
      lngZoom = ((lngPagB / lngKolB) * 100) - 1
      With ActiveSheet.PageSetup
      .Zoom = lngZoom
      End With
      End If
      End If


      Different widths:




      • Column B: 45 (319 pixels) -> in Excel, set with VBA

      • Column C: 15 (109 pixels) -> in Excel, set with VBA

      • Column D: 30 (214 pixels) -> in Excel, set with VBA

      • Column E: 20 (144 pixels) -> in Excel, set with VBA

      • Column B-E: 589 points -> with VBA

      • Page: 21 centimeters (595 points)

      • Margins (left & right): 1.8 centimeters (50.4 points)

      • Print area: 595 - 101 (100.8) = 494 points


      With numbers above it calculates 83%, but then it doesn't fit, when I set it manually to 77% it does fit, but how can I get this number with VBA? I don't understand the column widths, what I see in Excel and how I set it in VBA (45+15+30+20) is different from what VBA tells me it should be (589)...










      share|improve this question













      I'm having trouble fitting my columns in Excel on a sheet.
      I have a sheet with columns from A to CK (can be different per project).
      I don't need to print column A, but column B has to be on all pages and next to column B has to be 3 columns. So that will make column "B,C:E" on first page, next page "B,F:H", and so on... Column B is set as title, so it will be printed on every page.



      My problem is to set the scale. What I'm doing:




      • Take pagesize and translate to points, take off margin left and margin right = my printable area

      • Get the width of range("B:E") = my range to fit the page

      • Divide my printable area by my range to fit, multiply that with 100%, and extract 1% to make sure it will fit


      The outcome in my situation is 83, but is has to be 77 to fit the page. I'll have to find other numbers I think, but I don't know how and which...



      My code:



      If ActiveSheet.Name = "Meterkastlijst" Then
      Dim lngZoom As Long
      Dim lngKolB As Long
      Dim lngPagB As Long
      lngKolB = ActiveSheet.Range("B:E").Width
      If ActiveSheet.PageSetup.PaperSize = xlPaperA4 Then
      lngPagB = CLng(Application.CentimetersToPoints(21)) - CLng((ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin))
      ElseIf ActiveSheet.PageSetup.PaperSize = xlPaperA3 Then
      lngPagB = CLng(Application.CentimetersToPoints(29.7)) - CLng((ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin))
      End If
      If lngPagB <> 0 And lngKolB <> 0 Then
      lngZoom = ((lngPagB / lngKolB) * 100) - 1
      With ActiveSheet.PageSetup
      .Zoom = lngZoom
      End With
      End If
      End If


      Different widths:




      • Column B: 45 (319 pixels) -> in Excel, set with VBA

      • Column C: 15 (109 pixels) -> in Excel, set with VBA

      • Column D: 30 (214 pixels) -> in Excel, set with VBA

      • Column E: 20 (144 pixels) -> in Excel, set with VBA

      • Column B-E: 589 points -> with VBA

      • Page: 21 centimeters (595 points)

      • Margins (left & right): 1.8 centimeters (50.4 points)

      • Print area: 595 - 101 (100.8) = 494 points


      With numbers above it calculates 83%, but then it doesn't fit, when I set it manually to 77% it does fit, but how can I get this number with VBA? I don't understand the column widths, what I see in Excel and how I set it in VBA (45+15+30+20) is different from what VBA tells me it should be (589)...







      excel vba excel-vba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 10:42









      Joete

      144




      144





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206054%2fzoom-couple-of-columns-to-fit-page-with-vba%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206054%2fzoom-couple-of-columns-to-fit-page-with-vba%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff