我正在尝试使用VBA创建一个图形。首先,我有一个仪表板,我想要一个主表,总结一些我想要搜索的名字,所以我的所有数据都在其他表格中。
我正在尝试创建一个VBA IF条件,用于搜索特定的名称,数据表中的名称如下所示:
考虑到这一点,我在数据表中用“我”来查找我想要的名字。当某人想要搜索name1或name 2.时,此人将在主表中选择名称,其单元格为:"C3“。如果主表中的C3单元格等于数据表B2或Q2.然后创建一个图表。
If Worksheets("data").Cells(2, i) = Worksheets("main").Cells(3, 3) Then
问题是,这个条件只适用于名字,我不知道为什么。还有一个错误说:
运行时错误"13":类型不匹配
我拥有的完整的vba代码:
Sub graph()
Dim chrt As ChartObject
Dim i As Long
Dim LastRow As Long
Dim LastColumn As Long
'Find the last used row
LastRow = Sheets("vol").Range("B65536").End(xlUp).Row
'Find the last used column
LastColumn = 500
'Looping from second row till last row which has the data
For i = 2 To LastColumn
If Worksheets("vol").Cells(2, i) = Worksheets("main").Cells(3, 3) Then
'Sheet main is selected bcoz charts will be inserted here
Sheets("main").Select
ActiveSheet.Shapes.AddChart.Select
'this adds the chart and selects it in the same statement
ActiveChart.ChartType = xlLine
'now the line chart is added...setting its data source here
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "25%"
ActiveChart.SeriesCollection(1).XValues = Worksheets("data").Range(Worksheets("data").Cells(6, i), Worksheets("data").Cells(LastRow, i))
ActiveChart.SeriesCollection(1).Values = Worksheets("data").Range(Worksheets("data").Cells(6, i + 1), Worksheets("data").Cells(LastRow, i + 1))
'now the line chart is added...setting its data source here
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = "50%"
ActiveChart.SeriesCollection(2).XValues = Worksheets("data").Range(Worksheets("data").Cells(6, i), Worksheets("data").Cells(LastRow, i))
ActiveChart.SeriesCollection(2).Values = Worksheets("data").Range(Worksheets("data").Cells(6, i + 6), Worksheets("data").Cells(LastRow, i + 6))
'now the line chart is added...setting its data source here
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(3).Name = "25%"
ActiveChart.SeriesCollection(3).XValues = Worksheets("data").Range(Worksheets("data").Cells(6, i), Worksheets("data").Cells(LastRow, i))
ActiveChart.SeriesCollection(3).Values = Worksheets("data").Range(Worksheets("data").Cells(6, i + 11), Worksheets("data").Cells(LastRow, i + 11))
With Worksheets("main").ChartObjects(1).Chart
.HasTitle = True
.ChartTitle.Text = "1 month"
End With
End If
i = i + 15
Next i
End Sub
有人能照我一下吗!
非常感谢
发布于 2022-07-18 20:05:26
一些建议的更改,包括Step 15
Sub graph()
Dim cht As Chart, co As Object
Dim i As Long
Dim LastRow As Long, rngX As Range, rngY As Range
Dim LastColumn As Long, wsMain As Worksheet, wsVol As Worksheet, wsData As Worksheet
Set wsVol = ThisWorkbook.Worksheets("vol")
Set wsData = ThisWorkbook.Worksheets("data")
Set wsMain = ThisWorkbook.Worksheets("Main")
For i = 2 To 500 Step 15 'loop in increments of 15
If wsVol.Cells(2, i) = wsMain.Cells(3, 3) Then
'define data ranges
Set rngX = wsData.Range(wsData.Cells(6, i), wsData.Cells(Rows.Count, i).End(xlUp))
Set rngY = rngX.Offset(0, 1)
ClearWorksheetCharts wsMain 'remove any existing chart(s)
With wsMain.Range("B26:F37")
'add chartobject, setting position and size
Set co = .Worksheet.Shapes.AddChart(xlLine, .Left, .Top, _
.Width, .Height)
End With
Set cht = co.Chart
ClearChartSeries cht 'remove any "auto-added" series (if data was selected when chart was added)
AddSeries cht, "25%", rngX, rngY
AddSeries cht, "50%", rngX, rngY.Offset(0, 5)
AddSeries cht, "25%", rngX, rngY.Offset(0, 10)
cht.HasTitle = True
cht.ChartTitle.Text = "1 month"
With cht.Axes(xlCategory, xlPrimary)'X axis name
.HasTitle = True
.AxisTitle.Characters.Text = "Time"
End With
With cht.Axes(xlValue, xlPrimary)
.HasTitle = True
.AxisTitle.Characters.Text = "Value"
End With
End If
Next i
End Sub
'add a series and name it (factored out from main sub)
Sub AddSeries(cht As Chart, serName As String, serX, serY)
With cht.SeriesCollection.NewSeries
.Name = serName
.XValues = serX
.Values = serY
End With
End Sub
'remove any existing series from a chart
Sub ClearChartSeries(cht As Chart)
Do While cht.SeriesCollection.Count > 0
cht.SeriesCollection(1).Delete
Loop
End Sub
'Remove any chart objects from `ws`
Sub ClearWorksheetCharts(ws As Worksheet)
Do While ws.ChartObjects.Count > 0
ws.ChartObjects(1).Delete
Loop
End Sub
https://stackoverflow.com/questions/73027098
复制相似问题