Home » excel » Excel VBA Userform – Setting a chart the same size as userform image

Excel VBA Userform – Setting a chart the same size as userform image

Posted by: admin April 23, 2020 Leave a comment


I have an image in a userform which displays a pre-existing chart, according the properties menu the height = 246 and width = 462 (it does not specify units). I want my chart to be sized so that when a .GIF of the chart is saved and loaded to the userform image is matches the dimensions. In the chart properties I can change the dimensions but only has the option for inches. How can I get these dimensions to match?

How to&Answers:

If you are using an Image control to display the chart, then size the image control to fit the userform as per your requirements and then set the “PictureSizeMode” property of the image control to “fmPictureSizeModeStretch”.


The conversion factor you’re looking for is 72. Resize your chart to (Image ctl height / 72) inches by (Image ctl width / 72) inches. In your case that would give you 3.42 x 6.42 inches. When you load this into the Image ctl in Clip Mode it will fit perfectly. If you’ve selected to Center the picture in the Image with PictureAlignment, then switching back and forth between Clip and Stretch will barely produce a wiggle.

Here are 3 ways you can get the chart into the Image:

You can CopyPicture the resized Chart on the sheet to get it on the Clipboard, and then in the Form Editor at Design time select your Image ctl, then select the Picture Property for your Image ctl in the Properties Window, and Paste.

You can save the resized chart as a jpg, (it will also work to save as bmp but the file sizes get huge) and load the chart into the Image ctl from a file using:

myImageCtl.picture = loadPicture("C:\whatever")

If you want to keep a Picture of the resized chart on the sheet and insert it into your Image ctl from there instead of saving it to a file first, you can CopyPicture the Chart and paste it back onto the sheet. If you want the Picture to be a live updating copy of the original Chart (which could be on some other sheet), then use PastePicture to put it back on the sheet. Select the newly pasted Picture, type in a Name for it (myResizedChart) in the Name Box (upper left corner) and hit Return.

Download modPastePicture from here http://www.oaltd.co.uk and install it, (and set a reference to OLE Automation).

Then in your code you can say: (assuming your original CopyPicture was “Format Picture”)

myImage.picture = pastePicture

Finally, unless you need the Transparent Property of an Image ctl, there are a lot of advantages to using Frames instead of Images to show your Pictures on a Form.


Do it all at runtime, using the approach that saves the chart as a GIF and loads the gif into the image control.

The userform is named F_DisplayChart. It contains an Image control named imgChart and a button named btnClose. Here is the code in the UserForm’s code module:

Private Sub btnClose_Click()
  Unload Me
End Sub

Public Property Set Chart(cht As Chart)
  ' pass chart from calling code to userform
  Dim dHeight As Double, dWidth As Double
  Dim sPath As String

  dHeight = cht.Parent.Height
  dWidth = cht.Parent.Width

  cht.Parent.Height = Me.imgChart.Height
  cht.Parent.Width = Me.imgChart.Width

  sPath = ThisWorkbook.Path & "\temp1.gif"
  cht.Export Filename:=sPath, FilterName:="gif"

  cht.Export sPath

  Me.imgChart.Picture = LoadPicture(sPath)

  cht.Parent.Height = dHeight
  cht.Parent.Width = dWidth

End Property

In the property code, the chart is sized to fit the image control, exported, loaded into the image control, then the chart’s size is reset to the original size.

Here is the code that calls the UserForm, passes the active chart into the UserForm, then shows the form:

Sub ShowFormWithChart()
  Dim chrt As Chart

  If ActiveChart Is Nothing Then Exit Sub

  Set chrt = ActiveChart

  With F_DisplayChart
    Set .Chart = chrt
  End With

End Sub

You could also use bmp in place of gif, and get a larger file size. jpg also works, but it usually does not render the chart as nicely.