在VBA中进行匹配操作后 - 我得到一个范围变量
Set rngSel = ActiveSheet.Range(Cells(3, 28).Offset(0, startIdx - 1), Cells(3, 28).Offset(0, endIdx - 1))
它告诉我,我想在条形图中显示的数据在A3和Z3之间(总是在一行中),实例。我也可以打印这个
MsgBox ("rngSel=" & rngSel.Cells.Address)
但是,现在我想使用rngSel为我的diagramm提供输入。我试过了
ActiveChart.SetSourceData Source:=Sheets("Diagramm 1").rngSel
然而,这不起作用。 rngSel只提供x坐标范围(例如A3到Z3。y值在A4到Z4。我怎么能 - 基于rngSel对象告诉我的Diagramm 1只从中获取数据 A3:Z4?没有手动指定但动态使用rngSel?
整个宏
Sub Aku()
Dim startIdx As Integer, endIdx As Integer
Set valRng = ActiveSheet.Range("AB3:LS3")
startIdx = Application.Match(CLng(Cells(2, 18).Value), valRng, 0)
endIdx = Application.Match(CLng(Cells(2, 22).Value), valRng, 0)
Set rngSel = ActiveSheet.Range(Cells(3, 28).Offset(0, startIdx - 1), Cells(3, 28).Offset(0, endIdx - 1))
MsgBox ("rngSel=" & rngSel.Cells.Address)
ActiveChart.SetSourceData Source:=rngSel.Resize(2), PlotBy:=xlRows
End Sub
答案 0 :(得分:0)
编辑
试试这个:
Sub Aku()
Dim startIdx As Integer, endIdx As Integer
Dim rngSel As Range, valRng As Range
With ActiveSheet
Set valRng = .Range("AB3:LS3")
startIdx = Application.Match(CLng(Cells(2, 18).Value), valRng, 0)
endIdx = Application.Match(CLng(Cells(2, 22).Value), valRng, 0)
Set rngSel = .Range(Cells(3, 28).Offset(0, startIdx - 1), Cells(3, 28).Offset(0, endIdx - 1))
End With
Worksheets("Sheet 1").ChartObjects("Diagramm 1").Chart.SetSourceData Source:=rngSel.Resize(2), PlotBy:=xlRows
End Sub
它假定您在 Active 表是从
读取值的表时运行此宏