- ベストアンサー
EXCELの複数のシートに同じページ設定をしたいです
いつもお世話になっております。 マクロに初挑戦です。 すでに作成してあるブックに、同じページ設定をしたいです。 「マクロの記録」で、以下のマクロを作成しました。 Excel2003です。 Sub PageSetting() ' Keyboard Shortcut: Ctrl+Shift+F With ActiveSheet.PageSetup .RightHeader = "&A" '右上にシート名 .CenterFooter = "- &P -" '下中央にページ番号 .RightMargin = Application.InchesToPoints(0.393700787401575) '右余白1cm .TopMargin = Application.InchesToPoints(0.78740157480315) '上余白2cm .BottomMargin = Application.InchesToPoints(0.393700787401575) '下余白1cm .HeaderMargin = Application.InchesToPoints(0.590551181102362) 'ヘッダー1.5cm .FooterMargin = Application.InchesToPoints(0.196850393700787) 'フッター0.5cm End With End Sub ブック内のすべてのシートに適用するわけではないので、 複数シートを選択しておいて(作業グループ)、 まとめて適用したいのですが、どうすればよいのでしょうか? 今現在は、設定したいシートを1つずつ選択して、 Ctrl+Shift+F を押しています。 よろしくご指導お願い致します。
- go5koneko
- お礼率85% (12/14)
- オフィス系ソフト
- 回答数4
- ありがとう数4
- みんなの回答 (4)
- 専門家の回答
質問者が選んだベストアンサー
#1です。 > 残念ながら、シート名もページ番号も表示されず、変化なしでした。 マクロを含むブックでは無く、他のブックに適用させたい? そんな事、何処にも書いてなかったので処理をマクロを含むブック内に限定した書き方になってます。 For Each ws In ThisWorkbook.Windows(1).SelectedSheets ↓ For Each ws In ActiveWorkbook.Windows(1).SelectedSheets に直したらどうでしょうか? > 「選択したシート名を取ってくる」というサブルーチン(?) > を追加すればよい、ということになるのでしょうか・・・? > 何をどう書けばよいのか、わかりません。 For Each ws In ActiveWorkbook.Windows(1).SelectedSheets これがそれです。
その他の回答 (3)
- imogasi
- ベストアンサー率27% (4737/17068)
#2です。 >合計400枚くらいあります. >ブック内のすべてのシートに適用するわけではないので それであれば、本番データのないシートのA列に、そのブックの中で、同じ内容のPageSetUpすることを、適用するシート名を書き込む。 そしてシート名シートの最下行を探し(仮にdとする) For i=1 to d sn=Worksheets("シート名シート").Cells(i,"A") Worksheets(sn)).Activate MsgBox ActiveSheet.Name '確認用 以下#2と同じ Next i ・・・ をブックの数だけそれぞれのブックへコピーして実行してはどうでしょう。
お礼
無事できました!!ありがとうございます!! 勉強してVBAを扱えるようになりたいです。 ありがとうございました!!!
- imogasi
- ベストアンサー率27% (4737/17068)
Sub Macro1() sn = Array("s1", "s2", "s3") '選択するシート名 For i = 0 To 2 Worksheets(sn(i)).Activate MsgBox ActiveSheet.Name With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "シート " & sn(i) .RightHeader = "" .LeftFooter = "" .CenterFooter = "" ' 以下省略 End With Next i End Sub を実行し、各シートを印刷プレビューで見ると CenterHeaderが意図通りになっていた。 指定しないシートはCenterHeaderは空白のまま。 各シートごとに、本当にPageSetup情報を別に持っていて くれているのか(現瞬間のActiveシートだけではなイか)心配だったからですが、杞憂のようです。 だから本質問では、選択したシートのPageSetupに同じプロパティを設定したいのなら、上記CenterHeaderの行を含めて同じにして、ループさせれば良い。 シート選択(指定)の仕掛けは別方法もあるでしょうが メインテーマで無いようなので、上記にした。
お礼
ご回答ありがとうございます!! 同じページ設定にしたいものは、複数のブックに渡り 合計400枚くらいあります。となると、 「選択したシート名を取ってくる」というサブルーチン(?) を追加すればよい、ということになるのでしょうか・・・? 何をどう書けばよいのか、わかりません。 「マクロの記録」の不要な記述部分を削ることは、なんとかできたのですが。 VBAの本を借りてきたので、勉強します。
- papayuka
- ベストアンサー率45% (1388/3066)
3行程追加しました。 これでどうでしょう? Sub PageSetting() Dim ws As Worksheet ' Keyboard Shortcut: Ctrl+Shift+F For Each ws In ThisWorkbook.Windows(1).SelectedSheets With ws.PageSetup .RightHeader = "&A" '右上にシート名 .CenterFooter = "- &P -" '下中央にページ番号 .RightMargin = Application.InchesToPoints(0.393700787401575) '右余白1cm .TopMargin = Application.InchesToPoints(0.78740157480315) '上余白2cm .BottomMargin = Application.InchesToPoints(0.393700787401575) '下余白1cm .HeaderMargin = Application.InchesToPoints(0.590551181102362) 'ヘッダー1.5cm .FooterMargin = Application.InchesToPoints(0.196850393700787) 'フッター0.5cm End With Next ws End Sub
お礼
早速のご回答ありがとうございます! 残念ながら、シート名もページ番号も表示されず、変化なしでした。 なぜでしょう・・・? 再びご指導お願い致します。
関連するQ&A
- 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 このように記述されますが、どのように加工したらよろしいでしょうか? 教えてください。
- ベストアンサー
- オフィス系ソフト
- エクセルのマクロについて
エクセルの印刷マクロを作りましたが、動作が非常に遅いのです。 スタッフ用と管理者用のマクロを作り、スタッフ用が遅いのです。 余計なものが含まれているのでしょうか? お願いします。 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 で、フォントの設定は見当たりません。
- 締切済み
- オフィス系ソフト
- 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 というようなページ表示にしたいのですが、どのようにマクロを組めばいいかわからなくて困っています。 よろしくお願いいたします。
- 締切済み
- その他([技術者向] コンピューター)
- エクセル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
- ベストアンサー
- オフィス系ソフト
- 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列の最終行までとしたい。 上記マクロでここを修正すればいいというところがあれば教えてください。。
- 締切済み
- オフィス系ソフト
- 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
- ベストアンサー
- オフィス系ソフト
- 【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(エクセル)
- ExcelVBAでのページ設定
ExcelVBAで、新しいブックを開き、そのページ設定をしたく、以下のような記述をしたのですが、余白の値が変わりません。 Private Sub Workbook_Open() Workbooks.Add With ActiveSheet.PageSetup .TopMargin = Application.CentimetersToPoints(1.7) .BottomMargin = Application.CentimetersToPoints(1.7) .LeftMargin = Application.CentimetersToPoints(0.9) .RightMargin = Application.CentimetersToPoints(1.1) .HeaderMargin = Application.CentimetersToPoints(1.3) .FooterMargin = Application.CentimetersToPoints(1.3) End With (以下省略) Workbooks.Addを記述せず、新しいをブックを開かなければ、思い通りの動作をします。 外部ファイルを読み込んで、帳票を作るVBAなので、起動時に新しいブックを開きページ設定をしたいのですが、Workbook_Open()で、このような使い方は出来ないのでしょうか? よろしくお願いします。 尚、会社のLANからgooの閲覧が規制されており、返事が遅れるかもしれません。ご理解の程、お願いします。
- ベストアンサー
- Visual Basic
- 複数のシートの1ページ目と2ページ目を連続印刷したい
Vista Excel2007でマクロ作成中の初心者です。 複数のシートが12個あります。(増減あり) それぞれのシートには、必ず2ページの改ページが設定してあります。 この複数シートの1ページ目だけを連続印刷したいです。 また、2ページ目だけを連続印刷したいです。以下のようにしたのですが うまく印刷できません。よろしくお願いします。 Sub シートの1ページ目の印刷() Dim i As Integer For i = 1 To 12 With Worksheets(i) .Range("A1:Q44").PrintOut End With Next i End If End Sub ------------------------------------- Sub シートの2ページ目の印刷() Dim i As Integer For i = 1 To 12 With Worksheets(i) .Range("Q46:Q89").PrintOut End With Next i End If End Sub
- ベストアンサー
- Visual Basic
お礼
無事できました!!!ありがとうございます!!! 説明不足で申し訳ありませんでした。 複数のブックを処理したいのでマクロの保存先を 「個人用マクロブック」にしてPERSONAL.XLSに記述しています。 VBAが扱えると仕事を楽に進められるようになるのですね。 これを機にVBAを勉強していきたいと思います。 ありがとうございました!!!