在Excel中自动格式化

我想用特定条件自动格式化电子表格。 在进程完成时,工作表行应该是自动格式化的(即交替行具有相同的单元格背景色),并且标题行(通常是第1行)具有不同的颜色和字体粗体。

注:这需要由VBA代码完成。

另请注意,对于有“n”行的数据,需要格式化,剩下的空白。

我的pagelayout代码,

Public Function SetPageLayout(pworksheet) 'Set the page layout of the worksheet to be landscape and format to fit1 page With Sheets(pworksheet).PageSetup .PaperSize = xlPaperA4 .Orientation = xlLandscape .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = False .LeftMargin = Application.CentimetersToPoints(1) .RightMargin = Application.CentimetersToPoints(1) .TopMargin = Application.CentimetersToPoints(1) .BottomMargin = Application.CentimetersToPoints(1) End With End Function 

loggingAlt + O,A 。 哪个给你

  Selection.AutoFormat Format:=xlRangeAutoFormatList1, Number:=True, Font:= _ True, Alignment:=True, Border:=True, Pattern:=True, Width:=True 

在Excelmacroslogging器中logging步骤。 你必须重写它,因为它使用了一种vbs不支持的语法。

这适用于(我没有一个medium9)xlRangeAutoFormatAccounting4在vba中。

 Selection.AutoFormat Format:=xlRangeAutoFormatAccounting4, Number:=True, _ Font:=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True 

所以首先在vba的对象浏览器中查看常量。 xlRangeAutoFormatAccounting4 = 17

然后在对象浏览器中查看函数,并查看函数定义的底部。

 Function AutoFormat([Format As XlRangeAutoFormat = xlRangeAutoFormatClassic1], [Number], [Font], [Alignment], [Border], [Pattern], [Width]) 

所以vba变成了vbs(而vbs在vba中也是如此)(正如你所看到的,你可以通过正确的方式来find正常的方式)

 Selection.AutoFormat 17, True, True, True,True, True, True 

所以你的代码变成了

 objXLWs.Range("A3").CurrentRegion.Select.AutoFormat 17, True, True, True,True, True, True 

您正在使用Excel,并且可以在Excel中logging它并使用Excel编写代码。

Alt + T,M,R

然后Home键然后向上箭头 。 停止录制。

哎,看看Excel写的

 Selection.End(xlUp).Select 

或者如果您已经logging了转到对话框

 Application.Goto Reference:="R1C1" 

或者如果你有logging的Ctrl + Home

 Range("A1").Select