【问题标题】:Auto-formatting in ExcelExcel 中的自动格式化
【发布时间】:2014-11-26 00:46:07
【问题描述】:

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

注意:这需要通过 VBA 代码完成。

另请注意,需要对有数据的“n”行进行格式化,其余为空白。

我的页面布局代码,

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

【问题讨论】:

  • 我们不是您的代码猴子...到目前为止您尝试了什么?一些源代码?
  • Public Function SetPageLayout(pworksheet) '将工作表的页面布局设置为横向,格式为适合1页 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
  • 编辑您的问题以适当地突出显示代码。
  • Bash,我不确定要添加什么代码来生成备用单元格 BG 颜色,这就是本文的目的。
  • 您是否考虑过功能区主页选项卡中的条件格式?使用 Office 的内置功能往往比使用 VBA 代码更快。

标签: vba excel


【解决方案1】:

记录 Alt + O, A。这给了你

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

在excel宏记录器中记录步骤。你必须稍微重写一下,因为它使用了一种 vbs 没有的语法。

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

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 中工作)(正如您所见,您可以找出正确的方法而无需通常查找函数)

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

所以你的代码变成了

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

您正在使用 Excel,您可以将其记录在 Excel 中并让 Excel 编写您的代码。

Alt + T、M、R

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

看看 Excel 写了什么

Selection.End(xlUp).Select

或者如果您录制了“转到”对话框

Application.Goto Reference:="R1C1"

或者如果你已经录制了 Ctrl + Home

Range("A1").Select

【讨论】:

    猜你喜欢
    • 2021-05-16
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    相关资源
    最近更新 更多