Change Position of Legend
The below can be used to activate each chart in the activesheet and reposition the legend based on values entered by users.
Sub Change_Position_Of_Legend()
'
' For each chart in the active sheet, activate chart and change position of Legend
'
Dim NumberOfChartsInActiveSheet As Integer ' create a number variable to reflect the number of charts in the active sheet
Dim LegendDistanceFromTop As Double ' create a number variable to reflect the desired distance of the Legend from the TOP of the active chart
Dim LegendDistanceFromleft As Double ' create a number variable to reflect the desired distance of the Legend from the LEFT of the active chart
'
' Get the number of charts in the active sheet
NumberOfChartsInActiveSheet = ActiveSheet.ChartObjects.Count
'
' Ask the user to specify value for the "LegendDistanceFromTop" number
LegendDistanceFromTop = CDbl(InputBox(Prompt:="Enter the 'Distance From Top' for the chart Legends", Title:="Chart Legend Attributes"))
' use the below to use a cell value to specify the distance from Top
' LegendDistanceFromTop = CDbl(range("B2").value)' where cell B2 contains a whole number
'
' Ask the user to specify value for the "LegendDistanceFromleft" number
LegendDistanceFromleft = CDbl(InputBox(Prompt:="Enter the 'Distance From Left' for the chart Legends", Title:="Chart Legend Attributes"))
' use the below to use a cell value to specify the distance from Left
' LegendDistanceFromLeft = CDbl(range("B3").value)' where cell B3 contains a whole number
'
' Turn off screen updating to help the code run faster
Application.ScreenUpdating = False
'
' Create a For/Next loop to cycle through each chart in the active sheet
For ChartLoop = 1 To NumberOfChartsInActiveSheet
'
' Activate the next chart in the loop
ActiveSheet.ChartObjects(ChartLoop).Activate
' Now do something with the active chart
With ActiveChart
' In this case the "do something" is move the chart legend by changing the distance from the TOP and LEFT of the active chart
.Legend.Top = LegendDistanceFromTop
.Legend.Left = LegendDistanceFromleft
End With
'
'move on to next chart in the loop
Next
'
'
Application.ScreenUpdating = True
'
' Simple message to let users know that the repositioning is finished
MsgBox ("The Legend on each chart in the active sheet has been repositioned")
End Sub
Bookmarks