我在一个工作簿中有两张工作表。第一个工作表名称是“摘要”,另一个是“目标”。我在摘要选项卡中有图表。我要将该图表的源数据设置为包含日期的目标选项卡。例如(11/01/2013- 11/30/2013)。每天我都想更改相应日期的图表日期。因此,我在excel vba中进行了如下尝试:
sheets("Summary ").Select
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.PlotArea.Select
Sheets("Target").Select
a = InputBox("enter the date - format(mm/dd/yyyy)")
Set findrow = Range("a:a").Find(what:=a, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Rows
findrownumber = findrow.Row
ActiveChart.SeriesCollection(2).Values = "='Target Chart'!R4C78:R" & findrownumber & "C78"
End sub当我尝试在公式中输入源数据值时,它显示错误。
请帮帮我。
发布于 2013-11-26 17:24:41
这是我尝试过的代码,它可以工作。我可能在创建此示例时更改了工作表的名称,但您可以在代码中进行更改:)
假设Summary选项卡如下所示

此时,您在Target工作表中的图表如下所示。源数据设置为=Summary!$A$1:$A$6

现在试一下这段代码
Option Explicit
Sub Sample()
Dim wsSum As Worksheet, wsTgt As Worksheet
Dim objChart As ChartObject, chrt As Chart
Dim sDate
Dim findrow As Long
Dim aCell As Range
'~~> Accept the date
sDate = InputBox("enter the date - format(mm/dd/yyyy)")
'~~> Check if user entered something
If sDate <> "" Then
'~~> Set your respective worksheets
Set wsSum = ThisWorkbook.Sheets("Summary")
Set wsTgt = ThisWorkbook.Sheets("Target")
'~~> Find the date in the cell
With wsSum
For Each aCell In .Columns(1).Cells
If Format(aCell.Value, "mm/dd/yyyy") = sDate Then
'~~> Get the row number
findrow = aCell.Row
Exit For
End If
Next aCell
End With
'~~> Update the chart
With wsTgt
Set objChart = .ChartObjects("Chart 1")
Set chrt = objChart.Chart
chrt.SeriesCollection(1).Values = "='Summary'!R4C1:R" & findrow & "C1"
End With
End If
End Sub运行代码时,在输入框中,将日期设置为"01/11/2013“

这是将源数据设置为=Summary!$A$4:$A$11时将获得的输出

重要提示:我不推荐使用Inputbox来捕获日期。您可能想要使用THIS
https://stackoverflow.com/questions/20212506
复制相似问题