简体   繁体   中英

Excel VBA: sum chart range

Macro below works fine, but i want for range F23 sum number from range D2.

Sub Macro1()
ActiveSheet.ChartObjects("Graph").Activate
ActiveChart.SetSourceData Source:=Range("Sheet1!$E$13:$F$23")
End Sub

So if in cell D2 is number 87, then instead $F$23 will be $F$110. Always sum 23 with value in D2.

尝试这个:

Source:=Range("Sheet1!$E$13:$F$" & (23 + Range("D2").Value))

You just need to modify the Range String "Sheet1!$E$13:$F$23". You may try:

Sub Macro1()
  ActiveSheet.ChartObjects("Graph").Activate

  Dim valD2 As Integer
  valD2 = CInt(Sheets("Sheet1").Range("D2").Value)
  Dim rangeStr As String
  rangeStr = "Sheet1!$E$13:$F$" & CStr(valD2 + 23)

  ActiveChart.SetSourceData Source:=Range(rangeStr)
End Sub

您可以使用:

Source:=Range("Sheet1!$E$13:$F$13").Resize(10 + Range("D2").Value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM