Set Source Data for Chart On Save

laxstar785

New Member
Joined
Jul 27, 2011
Messages
23
Hello,

I am having an issue when trying to run a macro on save, I am trying to set the source data for a chart on a specific worksheet. I do not want to use activate because I do not want excel changing to that sheet to update the chart and taking me away from the current sheet I am on. here is the snippet of my code:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)​

Dim LR As Integer​

'Find last cell in column A with a date​
LR = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row​
If Sheets("Sheet1").Cells(LR, 1).Value = Date Then​
Sheets("Sheet1").Cells(LR, 2) = Application.WorksheetFunction.CountIf(Sheets("Micro 3in").Range("$E$3:$E$1998"), "Open")​
Sheets("Sheet1").Cells(LR, 3) = Application.WorksheetFunction.CountIf(Sheets("Micro 3in").Range("$E$3:$E$1998"), "Pending")​
Sheets("Sheet1").Cells(LR, 4) = Application.WorksheetFunction.CountIf(Sheets("Micro 3in").Range("$E$3:$E$1998"), "Complete Needs ECO")​
Sheets("Sheet1").Cells(LR, 5) = Application.WorksheetFunction.CountIf(Sheets("Micro 3in").Range("$E$3:$E$1998"), "Waiting For Samples")​
Else​
'Insert Date and count for conidtions in next row​
Sheets("Sheet1").Cells(LR + 1, 1).Value = Date​
Sheets("Sheet1").Cells(LR + 1, 2) = Application.WorksheetFunction.CountIf(Sheets("Micro 3in").Range("$E$3:$E$1998"), "Open")​
Sheets("Sheet1").Cells(LR + 1, 3) = Application.WorksheetFunction.CountIf(Sheets("Micro 3in").Range("$E$3:$E$1998"), "Pending")​
Sheets("Sheet1").Cells(LR + 1, 4) = Application.WorksheetFunction.CountIf(Sheets("Micro 3in").Range("$E$3:$E$1998"), "Complete Needs ECO")​
Sheets("Sheet1").Cells(LR + 1, 5) = Application.WorksheetFunction.CountIf(Sheets("Micro 3in").Range("$E$3:$E$1998"), "Waiting For Samples")​

Sheets("Sheet1").ChartObjects(1).Chart.SetSourceData Source:=Sheets("Sheet1").Range(Cells(1, 1), Cells(LR + 1, 5))​
End If​

End Sub​



This is the line that is giving me an error:


Sheets("Sheet1").ChartObjects(1).Chart.SetSourceData Source:=Sheets("Sheet1").Range(Cells(1, 1), Cells(LR + 1, 5))​


It doesn't give me an error if I am on Sheet1, it works correctly, but as soon as I am on another sheet it spits the error. If I change it to this:


Sheets("Sheet1").ChartObjects(1).Chart.SetSourceData Source:=Range(Cells(1, 1), Cells(LR + 1, 5))

this works but since I am not defining the work sheet it changes the source data to whatever sheet I am currently on which isn't good.

I am looking for some clarification on how to properly call out a worksheet in the setsourcedata command.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
As an update, I have been testing it more and the code appears to run correctly, it selects the chart and reselects the source data. But I still get runtime 1004 error every time I run the code while not on Sheet 1 using this:

Sheets("Sheet1").ChartObjects(1).Chart.SetSourceData Source:=Sheets("Sheet1").Range(Cells(1, 1), Cells(LR + 1, 5))


 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,827
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top