Closed Thread
Results 1 to 10 of 10

Chart on a Userform

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Chart on a Userform

    Hi All

    I want to add a chart to a userform, and i have found a few codes and sites that offer help, but..... im having issues i just cant get it to work, currently i have this

    Option Explicit
     Dim chartnum As Integer
     Private Sub UserForm_Initialize()
         chartnum = 1
         updatechart
     End Sub
     Private Sub updatechart()
         Dim CurrentChart As Chart
         Dim Fname As String
     
        
        Set CurrentChart = Sheets("Charts").ChartObjects(chartnum).Chart
         CurrentChart.Parent.Width = 390
         CurrentChart.Parent.Height = 190
         Fname = ThisWorkbook.Path & "\temp.gif"
         CurrentChart.Export Filename:=Fname, FilterName:="GIF"
         Image1.Picture = LoadPicture(Fname)
     End Sub
    Which wont work, then i have looked at this site.... http://www.j-walk.com/ss/excel/tips/tip66.htm

    And cant get that to work either, could somone please explain where i should be putting these codes? in a module, or directly into the userform code

    Thanks

    Dan

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Chart on a Userform

    The code should be in the userform module.

    The code posted, and for certain John's example, should work. Do you get errors, if so we need details.
    I assume you have a sheet called Charts with charts on it.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Chart on a Userform

    Yes i have the tab called "Charts" where the chart is located, and the image control in the userform is Image1, my current code for the whole userform is very simple

    Private Sub CommandButton1_Click()
    UserForm1.PrintForm
    End Sub
    
    
    
    Private Sub UserForm_Initialize()
    
    
    TextBox1 = "Sales have " & IIf([n22>0], "in", "de") & "creased by " & Format([n22], "£#,###,##0") & ", Which is " & Format([O22], "##0.0%") & " " & IIf([n22>0], "higher than the MYR.", "lower than the MYR.")
    TextBox2 = "Actual sold quantity has " & IIf([n13>0], "in", "de") & "creased by " & Format([n13], "#,###,##0") & ", Which is " & Format([O13], "##0.0%") & " " & IIf([n13>0], "higher than the MYR.", "lower than the MYR.")
    TextBox3 = "Gross margin has " & IIf([n31>0], "in", "de") & "creased by " & Format([n31], "£#,###,##0") & ", Which is " & Format([O31], "##0.0%") & " " & IIf([n31>0], "higher than the MYR.", "lower than the MYR.")
    TextBox4 = "The overall GM% is " & IIf([n40>0], "high", "low") & "er than the budget by " & Format([n40], "##0.0%.")
    TextBox6 = "Sales have " & IIf([q22>0], "in", "de") & "creased by " & Format([q22], "£#,###,##0") & ", Which is " & Format([r22], "##0.0%") & " " & IIf([q22>0], "higher than last year.", "lower than last year.")
    TextBox7 = "Actual sold quantity has " & IIf([q13>0], "in", "de") & "creased by " & Format([q13], "#,###,##0") & ", Which is " & Format([r13], "##0.0%") & " " & IIf([q13>0], "higher than last year.", "lower than last year.")
    TextBox8 = "Gross margin has " & IIf([q31>0], "in", "de") & "creased by " & Format([q31], "£#,###,##0") & ", Which is " & Format([r31], "##0.0%") & " " & IIf([q31>0], "higher than last year.", "lower than last year.")
    TextBox5 = "The overall GM% is " & IIf([q40>0], "high", "low") & "er than last year by " & Format([q40], "##0.0%.")
    
    Me.TextBox9.Value = Range("D2").Value
    
    TextBox1.Locked = True
    TextBox2.Locked = True
    TextBox3.Locked = True
    TextBox4.Locked = True
    TextBox5.Locked = True
    TextBox6.Locked = True
    TextBox7.Locked = True
    TextBox8.Locked = True
    TextBox9.Locked = True
    
    End Sub
    Its literally a summary box, but i want to extend this now to include graphs, when i put the previously posted code into the module i get this error....

    1. when all the code is in the module = variable not defined on Image2
    2. when i remove the userform_initialize sequence and put it in with my current code = Sub or function not defined, (which i then add "Run" infront of updatechart) and then it works, but doesnt show the graph nor does it save the GIF in the file location

  4. #4
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Chart on a Userform

    When i change it to this....

    Private Sub UserForm_Initialize()
    
    Dim chartnum As Integer
    
    
    TextBox1 = "Sales have " & IIf([n22>0], "in", "de") & "creased by " & Format([n22], "£#,###,##0") & ", Which is " & Format([O22], "##0.0%") & " " & IIf([n22>0], "higher than the MYR.", "lower than the MYR.")
    TextBox2 = "Actual sold quantity has " & IIf([n13>0], "in", "de") & "creased by " & Format([n13], "#,###,##0") & ", Which is " & Format([O13], "##0.0%") & " " & IIf([n13>0], "higher than the MYR.", "lower than the MYR.")
    TextBox3 = "Gross margin has " & IIf([n31>0], "in", "de") & "creased by " & Format([n31], "£#,###,##0") & ", Which is " & Format([O31], "##0.0%") & " " & IIf([n31>0], "higher than the MYR.", "lower than the MYR.")
    TextBox4 = "The overall GM% is " & IIf([n40>0], "high", "low") & "er than the budget by " & Format([n40], "##0.0%.")
    TextBox6 = "Sales have " & IIf([q22>0], "in", "de") & "creased by " & Format([q22], "£#,###,##0") & ", Which is " & Format([r22], "##0.0%") & " " & IIf([q22>0], "higher than last year.", "lower than last year.")
    TextBox7 = "Actual sold quantity has " & IIf([q13>0], "in", "de") & "creased by " & Format([q13], "#,###,##0") & ", Which is " & Format([r13], "##0.0%") & " " & IIf([q13>0], "higher than last year.", "lower than last year.")
    TextBox8 = "Gross margin has " & IIf([q31>0], "in", "de") & "creased by " & Format([q31], "£#,###,##0") & ", Which is " & Format([r31], "##0.0%") & " " & IIf([q31>0], "higher than last year.", "lower than last year.")
    TextBox5 = "The overall GM% is " & IIf([q40>0], "high", "low") & "er than last year by " & Format([q40], "##0.0%.")
    
    Me.TextBox9.Value = Range("D2").Value
    
    TextBox1.Locked = True
    TextBox2.Locked = True
    TextBox3.Locked = True
    TextBox4.Locked = True
    TextBox5.Locked = True
    TextBox6.Locked = True
    TextBox7.Locked = True
    TextBox8.Locked = True
    TextBox9.Locked = True
    
    chartnum = 1
    Run updatechart
    
    End Sub
    And keep the updatechart code in the module, there are no errors, but it still doesnt give me the chart in the userform, or save the GIF

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Chart on a Userform

    can you post example file.

  6. #6
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Chart on a Userform

    Hi Andy

    Sorry i had to remove alot of sensitive data, and then overwrite the formulas to give some form of example of what i am after, here is the example

    Thank you, hopefully you may be able to help me, yet again
    Attached Files Attached Files

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Chart on a Userform

    I just incorporated the UpdateChart routine into your userform.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    11-27-2023
    Location
    Vienna
    MS-Off Ver
    Office 365
    Posts
    12

    Re: Chart on a Userform

    Quote Originally Posted by Andy Pope View Post
    I just incorporated the UpdateChart routine into your userform.
    Hi Andy,
    I know, it's a quite an old topic, I just found it. I tried to copy your code to mine, but I got runtime error.
    Could you maybe check what the problem could be?

    Tks

  9. #9
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Chart on a Userform

    Brilliant thanks Andy Pope, i can only assume that i had another sheet open with userform1 in, as when i opened your returned sheet it wouldnt let me open the userform, when i closed all other sheets, it worked exactly how i wanted it too

    Thanks again

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Chart on a Userform

    Administrative Note:

    We are happy to help, however whilst you feel your request is similar or even the same as this thread, we have a rule that you open your own thread on the issue and do not piggy back another member's thread.

    Please see Forum Rule #1 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread. You can link back to this thread if you wish.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. combining chart userform and data transfer userform into 1 userform
    By H_Kennedy in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-04-2014, 07:11 AM
  2. combining chart userform and data transfer userform into 1 userform
    By H_Kennedy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-03-2014, 12:28 PM
  3. Chart in Userform
    By BrianAll in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-22-2013, 01:36 PM
  4. [SOLVED] Chart on a Userform
    By Rocky McKinley in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2005, 11:05 AM
  5. [SOLVED] how to insert a chart in a userform?
    By Christoph Strobelt [Bt] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1