Excel VBAmacros – 自定义边距不保存

在我的脚本中,我设置了客户利润率。 它们在代码中看起来不错,但是当我在新的工作表上运行脚本时,它保留了标准的1“边距。

任何关于如何让他们坚持的build议?

Sub Target_Format() ' ' Target_Format Macro ' ActiveWindow.SmallScroll Down:=66 Rows("85:112").Select Selection.Delete Shift:=xlUp ActiveWindow.SmallScroll Down:=-42 Rows("43:70").Select Selection.Delete Shift:=xlUp ActiveWindow.SmallScroll Down:=-72 Rows("2:2").Select Selection.Delete Shift:=xlUp Rows("15:15").Select Selection.Delete Shift:=xlUp ActiveWindow.SmallScroll Down:=6 Rows("28:28").Select Selection.Delete Shift:=xlUp ActiveWindow.SmallScroll Down:=9 Rows("41:41").Select Selection.Delete Shift:=xlUp ActiveWindow.SmallScroll Down:=-30 Rows("14:14").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("A14").Select ActiveWindow.SmallScroll Down:=9 Rows("28:28").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove ActiveWindow.SmallScroll Down:=6 Rows("42:42").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("I39").Select ActiveWindow.SmallScroll Down:=9 Range("A42:H42").Select ActiveWindow.SmallScroll Down:=-9 Range("A42:H42,A28:H28").Select Range("A28").Activate ActiveWindow.SmallScroll Down:=-15 Range("A42:H42,A28:H28,A14:H14").Select Range("A14").Activate Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone Selection.Borders(xlEdgeLeft).LineStyle = xlNone With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With Selection.Borders(xlEdgeBottom).LineStyle = xlNone Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("I22").Select ActiveWindow.SmallScroll Down:=-18 Rows("15:28").Select Selection.Cut Rows("1:1").Select Selection.Insert Shift:=xlDown Range("I20").Select ActiveWindow.SmallScroll Down:=-24 Columns("I:O").Select Selection.Delete Shift:=xlToLeft Rows("2:7").Select Selection.Copy Rows("2:2").Select Selection.Insert Shift:=xlDown Range("A2:H2").Select Application.CutCopyMode = False ActiveCell.FormulaR1C1 = "Actual" Range("B4:H7").Select Selection.ClearContents ActiveWindow.SmallScroll Down:=-9 Rows("5:5").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("I10").Select Rows("5:5").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Rows("7:7").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Rows("9:9").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("A5").Select ActiveCell.FormulaR1C1 = "WTD" Range("A7").Select ActiveCell.FormulaR1C1 = "WTD" Range("A9").Select ActiveCell.FormulaR1C1 = "WTD" Rows("11:11").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("A11:H11").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("A11").Select ActiveCell.FormulaR1C1 = "WTD" Range("A5:H5,A7:H7,A9:H9,A11:H11").Select Range("A11").Activate Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("J9").Select ActiveWindow.SmallScroll Down:=-12 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.75) .RightMargin = Application.InchesToPoints(0.75) .TopMargin = Application.InchesToPoints(0.5) .BottomMargin = Application.InchesToPoints(0.25) .HeaderMargin = Application.InchesToPoints(0.15) .FooterMargin = Application.InchesToPoints(0.15) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlPortrait .Draft = False .PaperSize = xlPaperLetter .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 Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("A2:H2").Select Selection.UnMerge Range("B2:H2").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Color = -16777216 .TintAndShade = 0 .Weight = xlThin End With Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("B2").Select ActiveCell.FormulaR1C1 = "/" Range("C2").Select ActiveCell.FormulaR1C1 = "/" Range("D2").Select ActiveCell.FormulaR1C1 = "/" Range("E2").Select ActiveCell.FormulaR1C1 = "/" Range("F2").Select ActiveCell.FormulaR1C1 = "/" Range("G2").Select ActiveCell.FormulaR1C1 = "/" Range("H2").Select ActiveCell.FormulaR1C1 = "/" Range("B2:H2").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Range("A1:H1").Select Columns("A:A").Select Range("A2").Activate Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Columns("C:I").Select Range("C2").Activate Selection.ColumnWidth = 7.57 Range("A1").Select 'Macro 5 Columns("B:H").Select Selection.Delete Shift:=xlToLeft Range("J1").Select ActiveCell.FormulaR1C1 = "Week #" Range("J1").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone Selection.Borders(xlEdgeLeft).LineStyle = xlNone Selection.Borders(xlEdgeTop).LineStyle = xlNone With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone 'Macro 7 Columns("J:J").Select Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Selection.ColumnWidth = 1.43 Range("A1").Select End Sub 

在您的代码中,您正在设置边距之前禁用PrintCommunication

 Application.PrintCommunication = False ' <-- disables print communication With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.75) .RightMargin = Application.InchesToPoints(0.75) .TopMargin = Application.InchesToPoints(0.5) .BottomMargin = Application.InchesToPoints(0.25) 

但是你永远不会使用Application.PrintCommunication = True再次启用它,所以更改从不提交。

有关更多信息,请参阅MSDN文档 。


PS我强烈build议你看看这个问题 ,find避免SelectSelection