EXCEL VBAでシートの余白を統一したい

このQ&Aのポイント
  • EXCEL VBAを使用して、ひとつのファイル内のすべてのシートの余白とA4横サイズを統一する方法について教えてください。
  • マクロを使用して、EXCEL VBAでシートの余白とA4横サイズを統一する方法を解説します。
  • EXCEL VBAでシートの余白を統一し、A4横サイズにする手順をご教示ください。
回答を見る
  • ベストアンサー

EXCEL VBAでシートの余白を統一したい。

こんばんは。 EXCEL VBAでひとつのファイルの中のすべてのシートの余白とA4横サイズに統一したいのです。 マクロの記録をとると With ActiveSheet.PageSetup .PrintTitleRows = "" .PrintTitleColumns = "" End With ActiveSheet.PageSetup.PrintArea = "" With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.393700787401575) .RightMargin = Application.InchesToPoints(0.393700787401575) .TopMargin = Application.InchesToPoints(0.393700787401575) .BottomMargin = Application.InchesToPoints(0.393700787401575) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperA4 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 100 .PrintErrors = xlPrintErrorsDisplayed End With End Sub このように記述されますが、どのように加工したらよろしいでしょうか? 教えてください。

質問者が選んだベストアンサー

  • ベストアンサー
  • soixante
  • ベストアンサー率32% (401/1246)
回答No.1

Sub aaa() Dim i As Integer For i = 1 To Worksheets.Count   With Worksheets(i).PageSetup    .Orientation = xlLandscape    .LeftMargin = Application.InchesToPoints(0.393700787401575)    .RightMargin = Application.InchesToPoints(0.393700787401575)    .TopMargin = Application.InchesToPoints(0.393700787401575)    .BottomMargin = Application.InchesToPoints(0.393700787401575)   End With Next i End Sub とか。 数値は適当に。 また、以下のURLも参照ください。 http://homepage1.nifty.com/tabotabo/insat/excelp/excelp06.htm

milomilo
質問者

お礼

できました。 ありがとうございました。

その他の回答 (1)

  • soixante
  • ベストアンサー率32% (401/1246)
回答No.2

これ忘れました。 With~End With の中に追加してください。 .PaperSize = xlPaperA4

関連するQ&A

  • エクセルのマクロについて

    エクセルの印刷マクロを作りましたが、動作が非常に遅いのです。 スタッフ用と管理者用のマクロを作り、スタッフ用が遅いのです。 余計なものが含まれているのでしょうか? お願いします。 Sub スタッフ() ' スタッフ Macro   ActiveSheet.PageSetup.PrintArea = "$A$1:$AJ$55" With ActiveSheet.PageSetup .PrintTitleRows = "" .PrintTitleColumns = "" End With ActiveSheet.PageSetup.PrintArea = "$A$1:$AJ$55" With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(0) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .CenterHorizontally = True .CenterVertically = True .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperA4 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 76 .PrintErrors = xlPrintErrorsDisplayed End With ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True ActiveSheet.PageSetup.PrintArea = "$A$1:$AJ$60" With ActiveSheet.PageSetup .PrintTitleRows = "" .PrintTitleColumns = "" End With ActiveSheet.PageSetup.PrintArea = "$A$1:$AJ$60" With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(0) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .CenterHorizontally = True .CenterVertically = True .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperA4 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 70 .PrintErrors = xlPrintErrorsDisplayed End With Range("A1").Select End Sub

  • ヘッダーの編集でフォントの設定をした場合は

    マクロの記録で ページ設定→ヘッダーフッター→ヘッダーの編集でフォントの設定をした場合は この設定のマクロの記録は取れないのでしょうか? マクロの記録の結果を見ても Sub Macro1() ' ' Macro11 Macro ' ' Application.PrintCommunication = False With ActiveSheet.PageSetup .PrintTitleRows = "" .PrintTitleColumns = "" End With Application.PrintCommunication = True ActiveSheet.PageSetup.PrintArea = "" Application.PrintCommunication = False With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "あああ" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.590551181102362) .RightMargin = Application.InchesToPoints(0.590551181102362) .TopMargin = Application.InchesToPoints(0.78740157480315) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlPortrait .Draft = False .PaperSize = xlPaperA4 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 100 .PrintErrors = xlPrintErrorsDisplayed .OddAndEvenPagesHeaderFooter = False .DifferentFirstPageHeaderFooter = False .ScaleWithDocHeaderFooter = True .AlignMarginsHeaderFooter = True .EvenPage.LeftHeader.Text = "" .EvenPage.CenterHeader.Text = "" .EvenPage.RightHeader.Text = "" .EvenPage.LeftFooter.Text = "" .EvenPage.CenterFooter.Text = "" .EvenPage.RightFooter.Text = "" .FirstPage.LeftHeader.Text = "" .FirstPage.CenterHeader.Text = "" .FirstPage.RightHeader.Text = "" .FirstPage.LeftFooter.Text = "" .FirstPage.CenterFooter.Text = "" .FirstPage.RightFooter.Text = "" End With Application.PrintCommunication = True End Sub で、フォントの設定は見当たりません。

  • 【Excel2013マクロ】コメント「なし」

    ググったところ同じ質問があったものの解決していないようなので、再度質問させていただきます。 過去の質問 → https://oshiete.goo.ne.jp/qa/9365077.html Excel2010からコピーしてきたページ設定のマクロを実行すると、コメント(M)が2010では「(なし)」だったのが2013では「シートの末尾」に設定されてしまいます。 「(なし)」でマクロを作り直してもダメです。 ------------------------------------- Application.PrintCommunication = False With ActiveSheet.PageSetup .PrintTitleRows = "" .PrintTitleColumns = "" End With Application.PrintCommunication = True ActiveSheet.PageSetup.PrintArea = "" Application.PrintCommunication = False With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.78740157480315) .RightMargin = Application.InchesToPoints(0.393700787401575) .TopMargin = Application.InchesToPoints(0.393700787401575) .BottomMargin = Application.InchesToPoints(0.393700787401575) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlPortrait .Draft = False .PaperSize = xlPaperA4 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = False .PrintErrors = xlPrintErrorsBlank .OddAndEvenPagesHeaderFooter = False .DifferentFirstPageHeaderFooter = False .ScaleWithDocHeaderFooter = True .AlignMarginsHeaderFooter = True .EvenPage.LeftHeader.Text = "" .EvenPage.CenterHeader.Text = "" .EvenPage.RightHeader.Text = "" .EvenPage.LeftFooter.Text = "" .EvenPage.CenterFooter.Text = "" .EvenPage.RightFooter.Text = "" .FirstPage.LeftHeader.Text = "" .FirstPage.CenterHeader.Text = "" .FirstPage.RightHeader.Text = "" .FirstPage.LeftFooter.Text = "" .FirstPage.CenterFooter.Text = "" .FirstPage.RightFooter.Text = "" End With Application.PrintCommunication = True End Sub ------------------------------------- 「(なし)」にする方法はありませんか?

  • EXCELで印刷範囲指定

    マクロの記録で印刷範囲の指定を考えています。 以下のマクロを記録しました。 Sub 印刷範囲指定() Cells.Select With Selection.Font .Name = "MS Pゴシック" .Size = 9 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Range("A1").Select With ActiveSheet.PageSetup .PrintTitleRows = "$6:$8" .PrintTitleColumns = "" End With ActiveSheet.PageSetup.PrintArea = "" With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&A" .RightHeader = "" .LeftFooter = "" .CenterFooter = "&P / &N ページ" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.78740157480315) .RightMargin = Application.InchesToPoints(0.78740157480315) .TopMargin = Application.InchesToPoints(0.984251968503937) .BottomMargin = Application.InchesToPoints(0.984251968503937) .HeaderMargin = Application.InchesToPoints(0.511811023622047) .FooterMargin = Application.InchesToPoints(0.511811023622047) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperA4 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 100 End With ActiveWindow.View = xlPageBreakPreview ActiveWindow.LargeScroll ToRight:=1 ActiveSheet.PageSetup.PrintArea = "$A$1:$I$50" ActiveWindow.View = xlNormalView Columns("B:I").Select Columns("B:I").EntireColumn.AutoFit Range("A1").Select End Sub やりたいことは ・まず最初にcsvファイルをxlsファイルに取り込む機能をつけたい。 ・取り込んだcsvをシートの一番最後につける。 そのファイルを以下のように設定していきたいです。 ・列の印刷範囲を数ある列のうちA列からI列までとする。 ・行の印刷範囲をA列の最終行までとしたい。 上記マクロでここを修正すればいいというところがあれば教えてください。。

  • エクセルVBAでヘッダーに別ブックのセルにある文字などを入れる方法をお

    エクセルVBAでヘッダーに別ブックのセルにある文字などを入れる方法をお尋ねします。 マクロ記録をとり、下のようなコードの部分でヘッダー中央に元のブックの元シートC3の文字を入れています。これでできているのですが、さらにE1にある日付、F1にある日付と文字列を入れたいと思っています。 今現在C3は会社名が入っています。(仮に「NTT商事」とします) E1には「2010/1/1」の日付が入っているとします F1には「2010/8/22」の日付が入っているとします これをヘッダー中央に、[NTT商事(20100101~20100822)売り上げ]というように元のセルにあるデータの利用と、“~”や“売り上げ”などの文字列を複合させてタイトル付けをしたいと思っています。 こうするためにはどのようにコードを入れればいいでしょうか? よろしくお願いします。 With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = Workbooks("元ブック").Sheets("元シート").Range("C3").Value .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.787) .RightMargin = Application.InchesToPoints(0.787) .TopMargin = Application.InchesToPoints(0.984) .BottomMargin = Application.InchesToPoints(0.984) .HeaderMargin = Application.InchesToPoints(0.512) .FooterMargin = Application.InchesToPoints(0.512) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperA4 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 100 .PrintErrors = xlPrintErrorsDisplayed End With

  • EXCEL97で印刷がB4になってしまう!

    エクセルで横長の表を作成しました。 多少大き目なので印刷範囲を指定し、ページ設定で「拡大縮小印刷」で横1×縦1ページを選択しました。用紙サイズもA4を指定しました。(その結果縮小率は91%となります。) ところが印刷をかけると自動的にB4になってしまうのです。プリンターにはB4用紙を入れていませんので毎回印刷エラーになってしまいます。(B4用紙を入れればOK) そこでVBAでやってみようと、下記のようにしてみました。 Sub 表印刷() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.590551181102362) .RightMargin = Application.InchesToPoints(0.590551181102362) .TopMargin = Application.InchesToPoints(0.118110236220472) .BottomMargin = Application.InchesToPoints(0.118110236220472) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintHeadings = False .PrintGridlines = False .CenterHorizontally = True .CenterVertically = True .Orientation = xlLandscape .PaperSize = xlPaperA4 .FirstPageNumber = xlAutomatic .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = 1 End With ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True End Sub しかしこれでやっても同じ結果でした。 なお、同ブックの他のページの表は正常に縮小印刷ができます。 原因と対策がまったくわからず困っています。 お助けください。

  • ヘッダーのタイトルとページ表示のコードがわからない

    表のタイトルと日付、ページをヘッダーあるいはフッダーに入れたいのですが うまく表示されません。 1.左ヘッダーに【名古屋市】登録状までしか表示しません。  【名古屋市】登録状況一覧 と表示させたいです。 2.日付は出てきたのですが、作表した日付のままでしょうか?  次の日に印刷したら日付がかわるのでしょうか?  システムからダウンロードした日付をいつも表示するようにしたいです。 3.1/しか表示しません。総ページ数が表示されません。 間違っているところを指摘していただけないでしょうか? よろしくお願いします。 With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftHeader = "&""MS Pゴシック,太字""&20【&A】登録状況一覧" .CenterHeader = "" .RightHeader = Format(Date, "ggge年m月d日") & "現在" '日付 .LeftFooter = "" .CenterFooter = "&P/&N" .RightFooter = "" End With

  • Excel2003VBA 印刷設定

    お世話になります。 下記、手入力による(?)印刷設定のコードなのですが不要な行をご教示いただきたく投稿致しました。 ・設定した内容は上下左右の余白を「0」にした。 ・用紙サイズと向きを選択した。 ・「1ページにあわせる」ように設定をした。 だけなのですが、恐らく単なる「マクロ記録」を利用したので余計な記述も含まれていると思います。 すでに削除してありますが 上から4行ほど何かを「""」という設定があったので 「勘」で削除し 動作確認ではOKでした。 でも見れば見るほど迷いが生じ どなたか ご存知の方にご教示いただこうと投稿致しました。 不要な行をご教示ください。 宜しくお願い致します。 Sub 印刷設定_A3横() ' ' 印刷設定_A3横 Macro ' マクロ記録日 : 2009/5/14 ' ' With ActiveSheet.PageSetup .LeftMargin = Application.InchesToPoints(1) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(0) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0.511811023622047) .FooterMargin = Application.InchesToPoints(0.511811023622047) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperA3 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = 1 .PrintErrors = xlPrintErrorsDisplayed End With ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True, Collate:= _ True End Sub

  • 【365】マクロでヘッダー・フッターが消えない

    頻繁に設定するページレイアウトをマクロ化してありますが、すでにヘッダー・フッターが設定されている状態でそのマクロを実行してもヘッダー・フッターが消えません。 .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" EvenPageとFirstPageも""に設定してあります。 他に設定しなければならないマクロがあるのでしょうか?

  • Excelでのページ番号付与について(WindowsXP)

    Excelでのページ番号付与について(WindowsXP) 何枚かのシートがあるファイルに一括でヘッダー・フッターを付けたいと思っています。 Sub SetHeader() Dim mySheet As Worksheet Application.ScreenUpdating = False For Each mySheet In ActiveWindow.SelectedSheets With mySheet.PageSetup .LeftHeader = "左ヘッダー" .CenterHeader = "中央ヘッダー" .RightHeader = "右ヘッダー" .LeftFooter = "左フッター" .CenterFooter = "中央フッター" .RightFooter = "右フッター" End With Next Application.ScreenUpdating = True End Sub 上記のようにマクロを作成したのですが、選択したシートの連番という単純な物ではなく ちょっと面倒なページ表示をしなければいけなくなりました。 一つのファイルの中で 1シート目→1ページ 2シート目→3ページ 3シート目→2ページ などとなっている場合 1枚目→1 2枚目→2-1/3    2-2/3    2-3/3 3枚目→3-1/2    3-2/2 というようなページ表示にしたいのですが、どのようにマクロを組めばいいかわからなくて困っています。 よろしくお願いいたします。  

専門家に質問してみよう