+1 vote
ago in VBA by Beginner (115 points)
On a worksheet, there is a range which I would like to display on the userform as an image.

How can I achieve that?

1 Answer

+2 votes
ago by Skilled (306 points)
selected ago by
 
Best answer

Insert a userform >> Create CommandButton1 (Name = cmdClose) >> Create Image1 (resize to fit your range)

then in Standard Module put this code

Sub ShowForm()
    UserForm1.Show
End Sub

Function ExportRangeAsPictureFile(rExport As Range, sExport As String) As Boolean
    Dim dh As Double, dw As Double

    dh = 1: dw = 1

    On Error Resume Next
        Kill sExport
    On Error GoTo 0

    If rExport Is Nothing Then GoTo Exit_Func
    rExport.CopyPicture appearance:=xlScreen, Format:=xlBitmap

    With ActiveSheet.ChartObjects.Add(Left:=rExport.Left, Top:=rExport.Top, Width:=rExport.Width + dw, Height:=rExport.Height + dh)
        DoEvents
        With .Chart
            Do Until .Pictures.Count = 1
                DoEvents: .Paste
            Loop
            .ChartArea.Format.Line.Visible = msoFalse
            .Export sExport
            ExportRangeAsPictureFile = True
        End With
        .Delete
    End With

Exit_Func:
End Function

And in UserForm Module put this code

Private Sub UserForm_Initialize()
    Dim oSelection      As Object
    Dim rSelection      As Range
    Dim bExport         As Boolean
    Dim sPathName       As String
    Dim sFileName       As String
    Dim sExportName     As String

    sPathName = Environ("tmp")
    sFileName = "Temp.gif"
    sExportName = sPathName & Application.PathSeparator & sFileName
    Set oSelection = Sheet1.Range("A1:B6")

    If TypeName(oSelection) = "Range" Then
        Set rSelection = oSelection
        bExport = ExportRangeAsPictureFile(rSelection, sExportName)
        If bExport Then Me.Image1.Picture = LoadPicture(sExportName)
    Else
        Me.Hide: Exit Sub
    End If
End Sub

Private Sub cmdClose_Click()
    Unload Me
End Sub

 

ago by Beginner (115 points)
+1
Works like charm. Thank you very much.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

What makes us different?
Our points system rewards you with a chance for free gifts as your contributions grow. The more points you earn, the better the prizes.

Getting Started
Register
Prizes

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

Top Users Mar 2019
  1. YasserKhalil

    306 Points

  2. ParserMonster

    204 Points

  3. Alberto Semat

    186 Points

  4. danmcg

    127 Points

  5. Abdan

    124 Points


Prizes for March 2019
Terms and Conditions
1st place (1) 1-year MyExcelOnline Academy Membership
(2) My First Add-in Dev Pack & Course
(3) AutoMacro: VBA Code Generator (Developer)
(4) VBA Cheat Sheet Bundle
(5) $35 Amazon Gift Card
2nd place (1) My First Add-in Dev Pack & Course
(2) 101 Most Popular Excel Formulas E-Book
(3) VBA Cheat Sheet Bundle
(4) $25 Amazon Gift Card
3rd place (1) Mouse to Macro
(2) VBA Cheat Sheet Bundle
(3) $15 Amazon Gift Card
...