Excel VBA Snap Chart to Range - Resize a Chart to Match the Exactly Match Cells

Опубликовано: 06 Октябрь 2024
на канале: EverydayVBA
805
11

Get the Excel file here
https://chrisjterrell.com/blog/215483...

Grab the Free VBA Quick Reference Guide
https://www.chrisjterrell.com/excel-v...

Are you annoyed with Charts that won't fit within the cells? This little bit of code can be a lifesaver. I highly recommend you add this code to your Personal workbook.

In this video, we provide code that will allow you to select a chart and then select a range from an Application InputBox and the code will resize the chart to exactly match the selected cell range.

The Code:

Sub SnapChart()
On Error Resume Next
Dim chrt As Chart
Dim rng As Range

Set chrt = ActiveChart
'An Application inputbox can return ranges
Set rng = Application.InputBox("Select Range", "Selection", Type:=8)

chrt.Parent.Top = rng.Top
chrt.Parent.Left = rng.Left
chrt.Parent.Height = rng.Height
chrt.Parent.Width = rng.Width

End Sub