การใช้ VBScript เพื่อเปลี่ยนสีพื้นหลังในแผนภูมิ Excel

ฉันใช้ 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" ฉันเดาว่าฉันไม่ควรเรียก .SeriesCollection ใต้ .Activechart มีข้อเสนอแนะอะไรบ้าง? ณ จุดนี้ ฉันยินดีเป็นอย่างยิ่งที่สามารถเปลี่ยนสีพื้นหลังของแผนภูมิเป็นสีขาว และหมุนป้ายกำกับแกน x 45 องศา

ขอบคุณล่วงหน้า.


person vicjun    schedule 17.07.2013    source แหล่งที่มา
comment
VBA นั้นดูค่อนข้างใกล้เคียงกับ VBScript... คุณได้ลองแล้วหรือยัง : ด้วย objExcel.ActiveChart ‹รหัส vba ทั้งหมดของคุณที่นี่› ลงท้ายด้วย?   -  person Papasmile    schedule 17.07.2013
comment
หากคุณใช้ VBScript คุณจะไม่สามารถใช้ค่าคงที่ใดๆ เช่น xlCategory ได้: Excel VBA รู้ว่าค่าเหล่านั้นคืออะไร แต่ไม่มีใน VBScript คุณสามารถค้นหาค่าตัวเลขได้ใน Object Browser (F2 ใน VBEditor)   -  person Tim Williams    schedule 17.07.2013
comment
... หรือเพียงพิมพ์ ?xlCategory แล้วกด Enter ในหน้าต่างทันที   -  person Jon Peltier    schedule 20.08.2015


คำตอบ (3)


เรื่องการสั่งซื้อ ก่อนที่คุณจะสามารถเข้าถึงซีรีส์ได้ คุณต้องเพิ่มซีรี่ส์ก่อน เช่นเดียวกับคุณสมบัติอื่นๆ เช่น 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

และควรจะได้ผล หากคุณได้กำหนดค่าคงที่เชิงสัญลักษณ์แล้ว

person Ansgar Wiechers    schedule 17.07.2013
comment
ขอบคุณมาก ตอนนี้เปลี่ยนสีเส้นได้แล้ว แต่ฉันอยากให้สีพื้นหลังในแผนภูมิเป็นสีขาว ไม่ใช่เส้น ฉันอยากทำมุมป้ายกำกับแกน x 45 องศาด้วย - person vicjun; 18.07.2013
comment
สีพื้นหลังของแผนภูมิสามารถเปลี่ยนแปลงได้ทาง .ChartArea.Color, สีพื้นหลังของกราฟสามารถเปลี่ยนแปลงได้ทาง .PlotArea.Color คุณยังสามารถใช้ .ColorIndex แทน .Color เพื่อกำหนดสีที่จัดทำดัชนีไว้ได้ (เช่น 2 สำหรับสีขาว) - person Ansgar Wiechers; 18.07.2013
comment
ฉันได้รับข้อผิดพลาดว่า: Object ไม่รองรับคุณสมบัตินี้ของหรือเมธอด: 'PlotArea.Color' ฉันจำได้ว่าเคยทดลองใช้วิธีนี้มาก่อน แต่ก็ได้ผลลัพธ์ที่คล้ายกัน... ฉันต้องประกาศอย่างอื่นก่อนหรือไม่? ขอบคุณ. - person vicjun; 18.07.2013
comment
ความผิดฉันเอง. มันคือ .PlotArea.Interior.Color ไม่ใช่ .PlotArea.Color - person Ansgar Wiechers; 19.07.2013

มีสองสิ่ง:

  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 .Solid End With

โดยที่ชื่อของแผนภูมิได้รับการตั้งค่าเป็น "NameChart" ก่อนหน้านี้ในโค้ด VBA เมื่อสร้างแผนภูมิ

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

ใจ .ForeColor.ObjectThemeColor = msoThemeColorBackground1

หากคำตอบของฉันช่วยคุณในทางใดทางหนึ่ง โปรดให้คะแนน ขอบคุณ.

person George    schedule 07.03.2016

วิธีเดียวที่ฉันรู้คือใช้ VBScript หรือ Python ด้านล่างนี้เป็น VBScript ที่อาจช่วยคุณได้ คุณจะต้องมีคำสั่ง if เพื่อทำให้ Date เป็นสีแดงหรือไม่

ฉันจะใช้สูตรเพื่อทำสิ่งนี้และคัดลอกสิ่งนี้ไปยังเซลล์ทั้งหมดในแถวที่คุณใช้สำหรับวันที่

มีลิงค์ดัชนีสีอยู่ในคอมเม้นท์ครับ

บันทึกเป็น 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
person Michael Mulvey    schedule 26.11.2019
comment
docs.microsoft.com/en-us/office/vba/ api/excel.colorindex - person Michael Mulvey; 26.11.2019
comment
ablebits.com/office-addins-blog/2013/10/18/ - person Michael Mulvey; 26.11.2019