【问题标题】:Using VBScript to change background color in Excel chart使用VBScript更改Excel图表中的背景颜色
【发布时间】:2013-07-17 12:46:29
【问题描述】:

我正在使用 VBScript 从 Excel 2003 中的数据列创建线散点图。结果很好,但我想编辑图表的一些属性,例如背景颜色和轴标签。我在 Excel 中手动完成并记录了一个宏,它给了我以下 VBA 代码:

ActiveChart.PlotArea.Select
With Selection.Border
    .ColorIndex = 16
    .Weight = xlThin
    .LineStyle = xlContinuous
End With
With Selection.Interior
    .ColorIndex = 2
    .PatternColorIndex = 1
    .Pattern = xlSolid
End With
ActiveChart.Axes(xlCategory).Select
With Selection.TickLabels
    .ReadingOrder = xlContext
    .Orientation = 45
End With
ActiveChart.Axes(xlValue).AxisTitle.Select
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .ReadingOrder = xlContext
    .Orientation = xlHorizontal
End With
ActiveChart.ChartArea.Select
End Sub

这对于 VBA 来说看起来不错,但我在将其转换为 VBScript 时遇到了麻烦。我应该如何开始?这是我目前的代码:

Set objChart = objExcel.Charts.Add()
With objExcel.ActiveChart
    .ChartType = xlXYScatterLinesNoMarkers
    .SeriesCollection(1).Interior.Color = RGB(255, 0, 0)
    .HasTitle = True
    .ChartTitle.Text = "usage"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "units"
    .HasLegend = False
    .SetSourceData objWorksheet.Range("E1","F" & LastRow), xlColumns
    .SetSourceData objWorksheet.Range("E1:F200"), xlColumns
End With

.SeriesCollection(1).Interior.Color = RGB(255, 0, 0) 行导致错误:“无法设置 Interior 类的颜色属性”。我猜我不应该在 .Activechart 下调用 .SeriesCollection。有什么建议么?在这一点上,我很高兴能够将图表的背景颜色更改为白色,并将 x 轴标签旋转 45 度。

提前谢谢你。

【问题讨论】:

  • 那个 VBA 看起来非常接近 VBScript... 你试过吗:用 objExcel.ActiveChart 结尾?
  • 如果您使用的是 VBScript,则不能使用任何常量,例如 xlCategory:Excel VBA 知道这些值是什么,但它们在 VBScript 中不可用。您可以在对象浏览器中找到数值(VBEditor 中的 F2)
  • ... 或直接输入 ?xlCategory 并在即时窗口中按 Enter。

标签: excel vba vbscript colors charts


【解决方案1】:

订单很重要。在访问系列之前,您必须先添加它。其他属性也是如此,例如HasTitle。此外,XY 图的线条没有内部颜色,仅在显示内部的图表(如饼图或柱形图)中可用。您可能是指这里的边框颜色。将您的代码更改为:

Set objChart = objExcel.Charts.Add
With objChart
  ' define chart type
  .ChartType = xlXYScatterLinesNoMarkers

  ' define data
  .SetSourceData objWorksheet.Range("E1:F200"), xlColumns

  ' format chart
  .SeriesCollection(1).Border.Color = RGB(255, 0, 0)
  .PlotArea.Interior.Color = RGB(255, 255, 255)
  '.PlotArea.Interior.ColorIndex = 2  'alternative
  .HasTitle = True
  .ChartTitle.Text = "usage"
  .Axes(xlCategory, xlPrimary).HasTitle = True
  .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
  .Axes(xlValue, xlPrimary).HasTitle = True
  .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "units"
  .HasLegend = False
End With

只要你定义了符号常量,它应该可以工作。

【讨论】:

  • 非常感谢,它现在可以改变线条的颜色。但我希望图表中的背景颜色为白色,而不是线条。我还想将 x 轴标签倾斜 45 度。
  • 图表的背景颜色可以通过.ChartArea.Color改变,图表的背景颜色可以通过.PlotArea.Color改变。您还可以使用 .ColorIndex 而不是 .Color 来分配索引颜色(例如,2 表示白色)。
  • 我得到一个错误:“对象不支持或方法的这个属性:'PlotArea.Color'。我记得以前尝试过这个方法,得到类似的结果......我必须先声明别的东西?谢谢。
  • 我的错。这是.PlotArea.Interior.Color,不是.PlotArea.Color。
【解决方案2】:

有两件事:

  1. 将绘图区域设置为某种颜色

查看示例

'set plotting area to dark gray color
ActiveChart.PlotArea.Select
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorBackground1
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = -0.5
    .Solid
End With

介意 .ForeColor.ObjectThemeColor = msoThemeColorBackground1

  1. 将图表的一般区域设置为某种颜色(我是指图表的整个区域,绘图区域除外)

    '设置图表区域的颜色为灰色 使用 ActiveSheet.Shapes("NameChart").Fill .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorBackground1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = -0.5 。坚硬的 结束于

在生成图表时,在 VBA 代码中已将图表名称设置为“NameChart”

  'set the name of the Chart to "Cooper" to reference it later
ActiveChart.Parent.Name = "NameChart"

介意 .ForeColor.ObjectThemeColor = msoThemeColorBackground1

如果我的回答对您有任何帮助,请评价。谢谢。

【讨论】:

    【解决方案3】:

    我知道的唯一方法是使用 VBScript 或 Python。下面是一个可以帮助你的 VBScript。您需要一个 if 语句来使 Date 变为红色。

    我会使用一个公式来执行此操作,并将其复制到您用于日期的行中的所有单元格中。

    在 cmets 中有一个指向颜色索引的链接。

    另存为 ExcelDB.vbs

    ' Validate Variables.
    Dim fso, strUserName , oShell
    
    ' Create an instance of the Scripting Shell
    Set oShell = CreateObject( "WScript.Shell" )
    
    ' Get the Username from The Shell enviroment
    strUserName = oShell.ExpandEnvironmentStrings( "%USERNAME%" )
    
    ' Create a File System Object named "fso".
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Create Instance of Excel Object
    Set objExcel = CreateObject("Excel.Application")
    
    ' Hide Alert messages
    objExcel.DisplayAlerts = False
    
    ' Make Excel File Visible/Hidden (True=Visible , False = Hidden)
    objExcel.Visible = True 
    
    ' Create a Workbook with 3 sheets named Retired, Styles and Patts
    Set objWorkbook = objExcel.Workbooks.Add
    objExcel.Activeworkbook.Sheets(1).Name = "Retired"
    objExcel.Activeworkbook.Sheets(2).Name = "Styles"
    objExcel.Activeworkbook.Sheets(3).Name = "Patts"
    
    ' Freeze header row (The first row will remain during sorting)
    
    With objExcel.ActiveWindow
         .SplitColumn = 0
         .SplitRow = 1
    End With
    objExcel.ActiveWindow.FreezePanes = True
    
    
    ' Define Names for lookup tables.
    objExcel.ActiveWorkbook.Names.Add "patterns", "=patts!$A$1:$b$150"
    objExcel.ActiveWorkbook.Names.Add "styles", "=Styles!$A$1:$H$100"
    objExcel.ActiveWorkbook.Names.Add "widths", "=Styles!$B$2:$B$101"
    
    'Define Values for Cells
    
    objExcel.Cells(1,1).Interior.ColorIndex = 3
    objExcel.Cells(1,1).Value = "Red Background"                'Cell A1 Value
    objExcel.Cells(2,1).Value = "GreenBorder"                   'Cell A2 Value
    objExcel.Cells(2,2).Value = "1"
    objExcel.Cells(2,3).Value = "1"
    objExcel.Cells(2,4).Value = "1"
    objExcel.Cells(2,5).Value = "12"
    objExcel.Cells(2,6).Value = "12"
    'Format Retired Cells
    
    objExcel.Sheets("Retired").Cells(7, 2).Numberformat = "@"
    objExcel.Worksheets("Retired").Cells(2,1).Borders.Color = RGB(0,255,0)
    objExcel.Worksheets("Retired").Columns("A:A").Columnwidth = 20
    objExcel.Worksheets("Retired").Columns("B:C").Columnwidth = 5
    objExcel.Worksheets("Retired").Columns("D:D").Columnwidth = 15
    objExcel.Worksheets("Retired").Columns("E:E").Columnwidth = 5
    objExcel.Worksheets("Retired").Columns("F:H").columnwidth = 15
    objExcel.Worksheets("Retired").Columns("I:I").Columnwidth = 5
    objExcel.Worksheets("Retired").Columns("B").NumberFormat = "000"
    objExcel.Worksheets("Retired").Columns("C").NumberFormat = "00"
    
    'get a cell value and set it to a variable
    row2Cell2 = objExcel.Cells(2,2).Value
    
    'save the new excel file (make sure to change the location) 'xls for 2003 or earlier
    objWorkbook.SaveAs "C:\Users\"&strUserName&"\Desktop\CreateExcel.xlsx" 
    
    'close the workbook
    'objWorkbook.Close 
    
    'exit the excel program
    'objExcel.Quit
    

    以下行是如何更改 excel 单元格的颜色 其中单元格(1,1)= 单元格 A1

    objExcel.Cells(1,1).Interior.ColorIndex = 3
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    相关资源
    最近更新 更多