•1 min read•from Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community
Snap chart to a range
Aligning a chart to a range of cells is needlessly complicated, bordering on impossible, so I wrote this code to do it. Save to your Personal Macros or as an add-in (what I do) and let it rip.
Sub Snap_Chart_To_Range() Dim cChart As ChartObject Dim rng As Range ' Validate selection is a chart If ActiveChart Is Nothing Then MsgBox "Please select a chart first.", vbExclamation Exit Sub Else Set cChart = ActiveChart.Parent End If 'Select range RangeSelect: On Error Resume Next 'Allows code to proceed if user cancels or closes via X Set rng = Application.InputBox( _ Prompt:="Select the cell range to align the chart to:", _ Title:="Select Target Range", _ Type:=8) On Error GoTo 0 If rng Is Nothing Then MsgBox "No range selected, operation cancelled.", vbExclamation Exit Sub ElseIf rng.Areas.Count > 1 Then MsgBox "Select one contiguous range", vbExclamation Set rng = Nothing GoTo RangeSelect End If 'Align and size the chart With cChart .Left = rng.Left .Top = rng.Top .Width = rng.Width .Height = rng.Height End With End Sub [link] [comments]
Want to read more?
Check out the full article on the original site
Tagged with
#no-code spreadsheet solutions
#financial modeling with spreadsheets
#rows.com
#Excel compatibility
#Excel alternatives for data analysis
#Excel alternatives
#Snap chart
#range
#macro
#align
#chart object
#contiguous range
#ActiveChart
#cells
#InputBox
#MsgBox
#validate selection
#error handling
#set rng
#add-in