I have an Excel chart like this:
And the following VBA macro which is toggling the HasLegend property and checking the Left position of the first DataLabel several times:
Sub Button1_Click()
Debug.Print CStr(ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(1).DataLabels(1).Left)
ActiveSheet.ChartObjects("Chart 1").Chart.HasLegend = False
Debug.Print CStr(ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(1).DataLabels(1).Left)
DoEvents
Debug.Print CStr(ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(1).DataLabels(1).Left)
DoEvents
Debug.Print CStr(ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(1).DataLabels(1).Left)
DoEvents
Debug.Print CStr(ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(1).DataLabels(1).Left)
DoEvents
Debug.Print CStr(ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(1).DataLabels(1).Left)
DoEvents
Debug.Print CStr(ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(1).DataLabels(1).Left)
DoEvents
Debug.Print CStr(ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(1).DataLabels(1).Left)
DoEvents
ActiveSheet.ChartObjects("Chart 1").Chart.HasLegend = True
End Sub
As you can see in the Immediate window below, after removing the legend there is a delay before the DataLabel Left value is updated.
After the Legend is removed, the original position value is printed another 4 times before the new value is read from the Left property. How can I program this in a reliable way so that the code blocks until the value is updated without adding some random number of DoEvents, or adding a timer loop for some random timespan, and hoping for the best?
Example workbook: https://www.dropbox.com/scl/fi/3b96fim1lkxz1mh5i4pq4/test.xlsm?rlkey=ivdghw3xr87b99ss1avkqgi79&dl=0