Vba 将此子模块转换为一个函数,这样我就可以更新Sheet2上的所有其他图表,而无需重复代码
我相信一定有比重复此代码更好的方法,但我不确定如何将其转换为可重复的函数,因为每个图表的newRange都是不同的-有什么想法吗?您可以将newRange和工作表引用作为参数传递Vba 将此子模块转换为一个函数,这样我就可以更新Sheet2上的所有其他图表,而无需重复代码,vba,excel,Vba,Excel,我相信一定有比重复此代码更好的方法,但我不确定如何将其转换为可重复的函数,因为每个图表的newRange都是不同的-有什么想法吗?您可以将newRange和工作表引用作为参数传递 Private Sub updateChart1() Dim month Dim year Dim stringToFind Dim newRange Dim foundString Dim newRange1 Dim newrange2 'Assign a valu
Private Sub updateChart1()
Dim month
Dim year
Dim stringToFind
Dim newRange
Dim foundString
Dim newRange1
Dim newrange2
'Assign a value to the month and the year
month = VBA.DateTime.month(Date)
year = VBA.DateTime.year(Date)
'Make the search for string equal to the month + year variables
stringToFind = month & "/" & year
'Declare a range where you will look for the month and year
Set newRange = Worksheets("Sheet1").Range("C16:Z16")
'Search for the month and year
Set foundString = newRange.Find(stringToFind)
'Once the correct date has been found, resize the range to be the new data source
Set newRange1 = foundString.Offset(0, -12)
Set newrange2 = newRange1.Resize(2, 12)
'Update the chart with the new data source
Worksheets("Sheet2").ChartObjects("Chart1").Chart.SetSourceData Source:=newrange2
End Sub
拆除管线
Private Sub updateChart1(newRange As Range)
'I decided that only Range in arguement list is enough. You may refer to the worksheet by newRange.Parent.
Dim month
Dim year
Dim stringToFind
Dim foundString
Dim newRange1
Dim newrange2
然后,您可以在代码中使用此子对象,让它为您想要的任何工作表和范围执行任务 多谢各位。如果无法获得正确的语法,如何准确引用newRange所在的工作表和范围?另外,在新的sub中,我还需要告诉它要更新哪个图表?updateChart1工作表Sheets1.RangeC16:Z16并查看我的编辑。但是如果我在另一个sub中调用updateChart1,我如何让它更新不同的图表,sub仍在查看图表1?谢谢你可以通过图表参考作为论据,也可以让潜艇自己寻找。但这似乎是另一个问题。
Set newRange = Worksheets("Sheet1").Range("C16:Z16")