在多张纸上应用格式化

我已经search并尝试了多种不同的代码和方式,但没有find解决scheme的运气。 我试图采取一个macros设置来格式化一张工作表,完美地工作,并将相同的代码应用于工作簿中的所有工作表。 我已经search了多个代码和表单数组公式,但无法将它们应用于我所拥有的代码或足够了解这些代码,以便改变需要更改的内容以使其工作。 我对macros观世界相当陌生,完全不了解编程语言。 我感谢任何人的时间,他们帮助我,因为我已经为此奋斗了几个星期了。 谢谢。 以下代码是我到目前为止:

Sub DARprintready() ' ' DARprintready Macro ' ' Columns("A:A").Select Selection.columnwidth = 2.86 Columns("B:B").Select Selection.columnwidth = 4.57 Columns("C:C").Select Selection.columnwidth = 13.57 Columns("D:D").Select Selection.columnwidth = 8.57 Columns("E:E").Select Selection.columnwidth = 20.86 Columns("F:F").Select Selection.columnwidth = 8.43 Columns("G:H").Select Selection.columnwidth = 9.43 Columns("I:I").Select Selection.columnwidth = 9.14 Columns("J:J").Select Selection.columnwidth = 9.43 Columns("K:K").Select Selection.columnwidth = 50.4 Columns("L:L").Select Selection.columnwidth = 9 Range("E:E,K:K").Select Range("K1").Activate Selection.NumberFormat = "@" With Selection .HorizontalAlignment = xlGeneral .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With ActiveWindow.SmallScroll Down:=-15 Columns("A:L").Select With Selection .HorizontalAlignment = xlGeneral .VerticalAlignment = xlCenter .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With ActiveWindow.SmallScroll Down:=-6 Columns("A:A").Select ActiveWindow.SmallScroll Down:=-15 Range("A1").Select Sheets("Header").Select Range("A1:L4").Select Selection.Copy Sheets("Firmwide").Select Rows("1:1").Select Selection.Insert Shift:=xlDown Application.CutCopyMode = False With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "Page &P of &N" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.18) .RightMargin = Application.InchesToPoints(0.16) .TopMargin = Application.InchesToPoints(0.17) .BottomMargin = Application.InchesToPoints(0.39) .HeaderMargin = Application.InchesToPoints(0.17) .FooterMargin = Application.InchesToPoints(0.16) .PrintHeadings = False .PrintGridlines = True .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperLetter .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 80 .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 End Sub 

要添加一点到另一个答案,使用一个with语句作为你的所有改变的简写,所以你不必一直input工作表名称

 Sub ColWidth() Dim wkst As Worksheet For Each wkst In ThisWorkbook.Sheets With wkst .Columns("A:A").ColumnWidth = 2.86 .Columns("B:B").ColumnWidth = 4.57 .Columns("C:C").ColumnWidth = 13.57 .Columns("D:D").ColumnWidth = 8.57 End With Next End Sub 

(你将不得不采取其余的这个表格)

另外,请考虑将列宽保留在数组中,并将其分配给循环中的列。 它不会加快速度,但是你的代码会更紧凑,而且我认为可读。

例如,

 Dim i As Integer Dim widths() As Variant widths = Array(4.5, 3.67, 5, 6.45, 10) For i = 1 To 5 Columns(i).ColumnWidth = widths(i) `Thank you iDevlop for the less Rube Goldberg approach Next 

这样,您可以随意添加更多列,而无需input所有内容。

第1步将学习一些VBA。 幸运的是,你正在尝试的任务并不需要你学习一吨。

假设您需要在所有图纸上使用完全相同的格式,则需要循环显示图纸。

为了做到这一点,你需要做3件事情。

  1. 为目标工作表名称创build一个variables
  2. 把你的格式放在一个遍历每个表单的循环中
  3. 用您的variables名称replacemacros中的硬编码表名称

你的代码最终会是这样的

 Sub DARprintready() ' ' DARprintready Macro ' dim Outputsheet as workhsheet for each Outputsheet in activeworkbook.sheets outputsheet.select 'your formatting code here next 

您需要通过引用刚刚创build的variables来更改公司范围内表单的明确引用。

取代这个:

 Sheets("Firmwide").Select 

有了这个:

 Outputsheet.Select 

希望有所帮助,

像往常一样,我有点晚,但这是一个更好的解决scheme。 如果您觉得这是一个更好的解决scheme,请随意标记我的权利。 这种方式一次格式化所有的表格,避免循环,并且更快,因为它是循环发生的Excel的内部。

  Dim shs As Sheets, wks As Worksheet Dim rFormat As Range Set wks = ActiveWorkbook.Worksheets("Sheet1") Set shs = ActiveWorkbook.Sheets(Array("Sheet1", "Sheet2")) shs.Select Set rFormat = wks.Range("A1:A2,C3:C4") rFormat.Select With Selection .Font.ColorIndex = 3 .Interior.ColorIndex = 6 .Interior.Pattern = xlSolid End With wks.Select 

为了一个快速的方法:

  Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select Columns("A:E").EntireColumn.AutoFit