Help in making a simle column/line chart in excel using vb6

Hi,

I want to make a simple column or line chart in excel 
using vb6.
I have created instance of excel and in the sheet1, I am 
able to populate some data. Now using that data, say the 
range is A2-B6, I want to use that range and make a chart.

Pls help by giving me code, step by step.

Thx
Sandeep

0
anonymous (74722)
8/11/2004 11:18:31 AM
excel.charting 18370 articles. 0 followers. Follow

1 Replies
705 Views

Similar Articles

[PageSpeed] 59

Sandeep -

Step 1. In Excel, turn on the macro recorder while making the kind of 
chart you want.

Step 2. Refine the recorded code.

Here is some recorded code, which creates an embedded column chart, adds 
axis and chart titles, and does a little formatting:

'' START RECORDED CODE
Sub SimpleChartForVB()
'
' SimpleChartForVB Macro
' Macro recorded 8/11/2004 by Jon Peltier
'
     Charts.Add
     ActiveChart.ChartType = xlColumnClustered
     ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B6")
     ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
     ActiveChart.PlotArea.Select
     With Selection.Border
         .ColorIndex = 1
         .Weight = xlThin
         .LineStyle = xlContinuous
     End With
     With Selection.Interior
         .ColorIndex = 2
         .PatternColorIndex = 1
         .Pattern = xlSolid
     End With
     ActiveChart.Axes(xlValue).MajorGridlines.Select
     With Selection.Border
         .ColorIndex = 15
         .Weight = xlThin
         .LineStyle = xlContinuous
     End With
     ActiveChart.PlotArea.Select
     With ActiveChart
         .HasTitle = True
         .ChartTitle.Characters.Text = "My Chart Title"
         .Axes(xlCategory, xlPrimary).HasTitle = True
         .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = _
             "Categories"
         .Axes(xlValue, xlPrimary).HasTitle = True
         .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Values"
     End With
     ActiveChart.SeriesCollection(1).Select
     With Selection.Border
         .Weight = xlThin
         .LineStyle = xlAutomatic
     End With
     Selection.Shadow = False
     Selection.InvertIfNegative = False
     With Selection.Interior
         .ColorIndex = 3
         .Pattern = xlSolid
     End With
     ActiveChart.ChartArea.Select
End Sub
'' END RECORDED CODE

This procedure is cleaned up a little; I've removed some defaults and 
streamlined the code a bit:

'' START ADJUSTED CODE
Sub SimpleChartForVB()
     Charts.Add
     With ActiveChart
         .ChartType = xlColumnClustered
         .SetSourceData Source:=Sheets("Sheet1").Range("A1:B6")
         .Location Where:=xlLocationAsObject, Name:="Sheet1"
     End With
     With ActiveChart
         With PlotArea.Border
             '' black border
             .ColorIndex = 1
         End With
         With PlotArea.Interior
             '' white fill
             .ColorIndex = 2
             .PatternColorIndex = 1
         End With
         With .Axes(xlValue).MajorGridlines.Border
             '' light gray lines
             .ColorIndex = 15
             .Weight = xlThin
             .LineStyle = xlContinuous
         End With
         .HasTitle = True
         .ChartTitle.Characters.Text = "My Chart Title"
         With .Axes(xlCategory, xlPrimary)
             .HasTitle = True
             .AxisTitle.Characters.Text = "Categories"
         End With
         With .Axes(xlValue, xlPrimary)
             .HasTitle = True
             .AxisTitle.Characters.Text = "Values"
         End With
         With .SeriesCollection(1).Interior
             '' red fill
             .ColorIndex = 3
             .Pattern = xlSolid
         End With
     End With
End Sub
'' END ADJUSTED CODE

For more about using VBA with charts, check out this web page:

   http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html

You also have to keep in mind that you have to reference everything for 
VB. This is easier if you use object variables. For example:

dim xlApp as Excel.Application
dim xlWB as Excel.Workbook
dim xlWS as Excel.Worksheet
dim xlCht as Excel.Chart
dim xlChOb as Excel.ChartObject

Set xlApp = <whatever>
set xlWB = <whatever>
set xlWS = <whatever>

set xlChOb = xlWS.ChartObjects.Add _
     (<left, top, width, height in points>)
set xlCht = xlChOb.Chart

with xlCht
     .SetSourceData Source:=xlWS.Range("A1:B6")
     .ChartType = xlColumnClustered

     ' you don't need .Location
     ' .ChartObjects.Add puts it into xlWS

     ' do whatever formatting you need
end with

etc.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

Sandeep wrote:

> Hi,
> 
> I want to make a simple column or line chart in excel 
> using vb6.
> I have created instance of excel and in the sheet1, I am 
> able to populate some data. Now using that data, say the 
> range is A2-B6, I want to use that range and make a chart.
> 
> Pls help by giving me code, step by step.
> 
> Thx
> Sandeep
> 

0
8/11/2004 12:21:45 PM
Reply:

Similar Artilces:

Converting QuatroPro to Excel
How can convert a quatropro spreadsheet to an excel spreadsheet. Any help? Thanks, Bob Open the file in Quattro Pro and save it in one of the supplied Excel formats. -- Regards Andy Wiggins www.BygSoftware.com Home of "Save and BackUp", "The Excel Auditor" and "Byg Tools for VBA" "Bob" <anonymous@discussions.microsoft.com> wrote in message news:36c601c40128$9e26a970$a601280a@phx.gbl... > How can convert a quatropro spreadsheet to an excel > spreadsheet. Any help? > Thanks, > Bob ...

How do I use a "subtract" function in Excel?
Hi Kelly if you want to subtract the value in B1 from the value in A1 and have the answer display in C1 then in C1 type =A1-B1 Hope this helps Cheers JulieD "Kelly" <Kelly@discussions.microsoft.com> wrote in message news:5DF4C83F-6BF1-40A8-BC8A-692058A5C70B@microsoft.com... > ...

how do I automatically update a text box in a chart?
Hi, You can link the textbox to a cell. When you update the cell the textbox should reflect this. Select the textbox border and then in the formula bar enter the complete cell reference, for example, =Sheet1!A1 Cheers Andy Kath wrote: -- Andy Pope, Microsoft MVP - Excel http://www.andypope.info ...

Using part of a cell in a chart title
I have a chart which should get a title. However, this should be partly be used from a cell e.g. "counted with 5%" 5% should be taken from the cell and used in the title. Is this possible? Hi, Yes it's possible but all of the chart title needs to be in the cell. So you may need to use a helper cell and concatenate text and value. http://www.andypope.info/tips/tip001.htm Cheers Andy -- Andy Pope, Microsoft MVP - Excel http://www.andypope.info "Nicole" <Nicole@discussions.microsoft.com> wrote in message news:5CB7A971-AA7F-4C34-BB42-7DC283AA2958@micro...

Input Excel 'Password to Open' through control in access form
Hi All, We know,Excel has prompt password to open it files. Is it possible to create a code that can supplies the excel prompt password?.So that when we open the excel file through our access control in a form, the excel files can be opened automatically.But when the excel files opened from its default icon,it will prompt a password first. ...

Excel 2007
When I select cells to copy as a picture in Excel 2007, the resolution is terrible. Text and objects with shadow's are very blotchy when pasting the picture. How do you change the resolution of a 'Copy Picture'? ...

Sort ascending, make changes, restore previous order
I've got an AutoFilter in a spreadsheet. I want to sort ascending, mak some changes to some cells, then restore the previous order. Can thi be done easily or will this require some programming?? Thanks in advane! Matt -- BVHi ----------------------------------------------------------------------- BVHis's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=859 View this thread: http://www.excelforum.com/showthread.php?threadid=47508 I'd use a helper column. Put =row() and drag down. Convert it to values (edit|copy, edit|paste special|values) Do all y...

Excel not Access
I have designed an Access database that holds records relating to my stores audit results going back for about 5 years plus a load more information relating to these stores. This was used to produe a pack once a month, however a change in senior management means that I have got to shelve this and prodce a similar pack in Excel. The idea would be that the user could select a month or a 12 mnth date range that would produce data that could then be used to populate a number of excel templates that have been designed. Having not used excel for years I would be grateful for any suggestion...

How to keep format when importing Excel into SQL.
Can anybody help me please? When I import Excel file into SQL, a field that formated as 0000000000 (custom), loosing leading zeros. I tried to change data type in SQL after importing. No luck. I appretiate any help. See if using IMEX=1 helps: http://pratchev.blogspot.com/2007/10/importing-excel-data-into-sql-server.html -- Plamen Ratchev http://www.SQLStudio.com Plamen, I tried this, but got an error: "The OLE DB provider "Microsoft.Jet.OLEDB.4.0" has not been registered." Thank you very much for looking into this. "Plamen Ratchev" wr...

Using expression builder object
Hi, I'm developing a wizzard in Access which builds import templates for various data sources to a fixed set of tables. In step 3 the users must be able to build an expression ; for instance Left([Fieldx],20) . Now I would like to have a command button on my form which calls the Access expression builder to allow the users to use this to build the expression. This expression will then be stored in a text box linked to the templates table. Anybody know how to call and use this object from VBA code? -- Kind regards Noëlla DoCmd.RunCommand acCmdInvokeBuilder I th...

Counting number of X's in a column
I need to sum up the number of "Xs" in a column. I would be extremely grateful if someone could help me with the formula. I cannot seem to get one to work. We are using Excel 2003. Thank you. -- Susie Harris Operations Manager Environmental Education Association of Washington =COUNTIF(A:A,"X") -- HTH RP (remove nothere from the email address if mailing direct) "Susie Harris" <SusieHarris@discussions.microsoft.com> wrote in message news:FFA0FB17-C922-48A8-B3C3-0D487C1E6F6A@microsoft.com... > I need to sum up the number of "Xs" in a col...

Lookup based on criteria in 2 columns
Hi, I am trying to use a vlookup or other function to return the value in the amount column based on the location and date. Here is a sample of my data: Location Date Amount 101 9/15/8 10 101 9/16/8 20 101 9/17/8 15 102 9/15/8 50 102 9/16/8 75 102 9/17/8 67 For example if I wanted to return the amount for location 102 on 9/15/8, what formula would I use? I tried using variations of vlookups but had no luck. Thanks, =SUMPRODUCT(--(A2:A50=102),--(B2:B50=DATE(2008,9,15)),C2:C50) -- Regards, Peo Sjo...

Auto-format in Microsoft Excel.
Each time i try to enter a number range, for example, 8- 10, in Excel, it constantly re-formats it to a date. If I change the formatting to "General" it turns it into a random number, usually 38209. I would like to turn off ALL auto-formatting, but that's probably asking too much. How do I disable this frustrating feature? thanks. Hi Paul When you enter "8-10" in a cell, that's not strictly a number but Excel thinks you mean a date so converts it as such. A date is a number and in your example the number 38209 represents August 10 2004 which is not a ra...

Chart template?
I am working with a chart, created from a pivot table. The chart has the various [drop down] fields available for selecting different values for the chart to show. I have formatted the chart with specific settings, fonts, colors, etc., but everytime I use one of the dropdowns, the formatting goes back to the default. I have looked for a place to save as a template, or defaults without luck. Thanks for any help you can provide! Alan ...

Radar chart in Access 2007 report
Can you add a Radar chart to an access 2207 report? ...

How to change font size on formula bar in Excell 2007
I don't find Tools>Options>General (as suggested in other post answers) in Excel 2007. On the Office button there is an Excel Options but it doesn't provide a method of changing font size on the office but. My font is so small I can barely see it. Office button>ExcelOptions>Popular tab>in the "When creating new workbooks" section, choose font and font size -- Kind regards, Niek Otten Microsoft MVP - Excel "jimwillie" <jimwillie@discussions.microsoft.com> wrote in message news:588AAC05-0F52-404E-AA01-128E70E02D0B@microso...

Opening Excel Workbooks
I'm running into an issue where if I click on an Excel file through My Documents, it doesn't automatically bring it up. I get the toolbar but the actual spreadsheet doesn't appear on the screen. I have to click on the taskbar to get it to pop up. If I already have Excel active and I open a file through Excel, this doesn't happen. Any ideas? Here is a similar thread: http://www.excelforum.com/showthread.php?s=&threadid=237195 Rolli -- Message posted from http://www.ExcelForum.com Hi, Take a look at Tools-Options-General tab- uncheck ignore other application...

help with simple maths in excel
I want to do a simple arithmatic excercise in excel for my grand daughter. It is stuff like 2+2 =4 Smart me has hit a problem at the first hurdle... I need to put 2 in one cell the + in another cell and then 2 in another = in another and then she puts the answer in the next one. So the above would have 4 cells completed and she would put the answer in the 5th one. When I use the + or = sign in a cell of its own it (excel) thinks I am doing an equation, is there a way around this? I will work on the answer like if she gets it correct or wrong how I will do that...a sound or som...

Help with income/expenses
Running 2003 in home page I setup income and expenses I prefer to see one whole month. Money some how shows it as 1/22/2006 through 2/20/2006 any way to fix this? Thanks Jeff ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =---- ...

Unexpected error message on closing an Excel file
Suddenly I am getting the following message when I try to close a workbook: "Your formula contains an invalid external reference to a worksheet. Verify that the path, workbook, and range name or cell reference are correct, and try again" The mysterious thing is that it does not happen consistently and that, after I click OK after the above message, I can still save the file. What might be the cause of this error message and can the "invalid reference" be tracked down using one of the utility add-ins such as J. Walkenbach's PUP? If it only happens when you close ...

HELP! Need to export hourly sales data on POS (NOT RMS)
How can I export hourly sales data across a date range? For instance, I want to show hourly sales for the month of October so I can graph it and post it in our break room. If I can't export hourly data, can I export daily sales? The built-in reports don't address this data format. This is a multi-part message in MIME format. ------=_NextPart_000_008E_01C826DC.CBC512D0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit Mark, This should work for you. Keep in mind it takes up to 5-10 minutes to load...

How do I use traffic lights in excel
I am wanting to use traffic lights in excel that change colour based on the result of a variance cell, ie if the result of the cell is 10 make the traffic light green, if it is 20 make the traffic light amber, if the result is 30 make the traffic light red. How do I do this? Shorty Format>Conditional Formatting>Cell Value is: Note: you can add up to 3 conditions(4 if you count default) Gord Dibben Excel MVP On Wed, 22 Dec 2004 16:35:03 -0800, Shorty <Shorty@discussions.microsoft.com> wrote: >I am wanting to use traffic lights in excel that change colour based on the &g...

Need help with Auto Filter
I have a spreedsheet that is filtered in multiple columns. I am running a "Subtotal" function to count certian rows when I filter the column. My question is this. Is there a way to save or freeze the data that the subtotal function counts in a different cell. In other words I want subtotal to count everything in a particular column but I want to be able to save that number somewhere so that when I filter again with another variable I am able to still view the first subtotal to compare the two. I hope this makes sense and thanks in advance for any assistance you can provi...

How To Copy MS Word mailing labels into Excel
I have a word doc that I want to put into Excel. I want to add some more fields to the names and addresses. Is this simple or do I have to learn how to program? Michael Rodriguez City of Grand Prairie Michael, have you tried to copy and paste the data into excel? -- Paul B Always backup your data before trying something new Please post any response to the newsgroups so others can benefit from it Feedback on answers is always appreciated! Using Excel 2000 & 97 ** remove news from my email address to reply by email ** "Michael Rodriguez" <mrodrigu@gptx.org> wrote in messa...

make subreport2 visible based on condition
Help, I am running Access 2003 and have trouble getting this work right. I have an unbound subreport that contains 3 additional subreports. I want only one of the 3 to be visible based on the value in a combo box on a selection form. I can't seem to get the reference right and have tried several variations. If Forms!frmWeeklyGLrpt!cboCC = 1 Then Me!subrptGLWeekly.Report!subrptGLWeeklyCMRDA.Visible = True Me!subrptGLWeekly.Report!subrptGLWeeklyCMRDB.Visible = False Me!subrptGLWeekly.Report!subrptGLWeeklyCMRDC.Visible = False End If If Forms!frmWeeklyGL...