我正在使用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度。
提前谢谢你。
答案 0 :(得分:0)
订单很重要。在您访问系列之前,必须先添加它。其他属性也是如此,例如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
如果你已经定义了符号常量,它应该可以工作。
答案 1 :(得分:0)
有两件事:
参见示例
'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
mind .ForeColor.ObjectThemeColor = msoThemeColorBackground1
将图表的一般区域设置为某种颜色(我指的是图表的整个区域,绘图区域除外)
'将图表区域的颜色设置为灰色 使用ActiveSheet.Shapes(“NameChart”)。填充 .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"
mind .ForeColor.ObjectThemeColor = msoThemeColorBackground1
如果我的回答有任何帮助,请评价。感谢。
答案 2 :(得分:0)
我知道的唯一方法是使用VBScript或Python。下面是一个可以帮助您的VBScript。您将需要一个if语句来使日期变为红色。
但是,我将使用公式来执行此操作,并将其复制到用于日期的行中的所有单元格中。
注释中有指向颜色索引的链接。
另存为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单元格的颜色的 其中cell(1,1)=单元格A1
objExcel.Cells(1,1).Interior.ColorIndex = 3