|
Create a Line Chart with a Macro in Excel
Create a line chart in Excel with this macro. This allows you to quickly and easily create line charts and graphs from a selection of data on the worksheet. As this macro is currently set, the range from which the line chart will be created is a static range that is hard-coded into the macro.
This is a bare-bones version of this macro and is in this fashion so that it is easy to integrate into any other macros that you may be using in Excel.
To use this macro simple replace 'Sheet1'!$A$1:$B$67 with the range cell references of the source data that you would like to use in your chart.
Where to install the macro: Module
Excel Macro to Create a Line Chart
Sub Create_Line_Chart()
'create a line chart in excel with this macro
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$1:$B$67")
ActiveChart.ChartType = xlLine
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
PLEASE HELP ME!!!!! How can I create a line chart with two X-axes in Excel?
I would like to create a Today Line that will start from the to column date right down to the last entry of the activities..
How do i create this line. Do i again use conditional formatting?
thanks
I've done a handful of macros in excel before, but it's been awhile and I'm kind of blanking. I'm essentially looking to have a macro that will insert a blank row between each other row (a list of names with information, and I want there to be spaces between). Then I want it to stop when it gets to the last row with text in it. I know this is something simple to do, but I'm forgetting how to do it. Thanks in advance.
I am writing a VBA macro to create a table. Excel 2007.
The table has two columns, Column A: date, Column B: amount.
The column has headers. Cell(1,1) = "Date" Cell(1,2) = "Amount"
I do not know ahead of time how many rows will be in the table.
Once the table is created, I want to use VBA to create a simple line chart....
Column A (date) will be the XValues.
Column B (amount) will be the data series
I want to dynamically specify the range of the data series & XValues within the VBA code. I have tried the code below, but it doesn't work...
Bottom = Range("A1").End(xlDown).Row
ActiveChart.SetSourceData Source:=Range(cells(2,1),cells(2,Bottom))
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SeriesCollection(1).XValues = Range(cells(1,1),cells(1,Bottom))
Can some one help?
i have a data with its target..how do i plot a graph with a horizontal line as its target?
Bookie.xls
I would like to create a Pivot Table Chart (unless you have a better idea). I want a line chart for customer comments/ratings. I need to be able to separate the information by using O2 (Code) as my primary key.
Row 2 = Titles (Cells M3 thru X36 = Data)
M2 = Model (data example: ABC)
N2 = Serial # (data example: 12-12345)
O2 = Code (data example: BQ,CQ,A2,PJ,FD)
P2 = Records (data example: 2 [rating option of 1 thru 5])
Q2 = Metal (data example: 5 [rating option of 1 thru 5])
R2 = Electric (data example: 5 [rating option of 1 thru 5])
S2 = Hyd (data example: 5 [rating option of 1 thru 5])
T2 = XMSN (data example: 1 [rating option of 1 thru 5])
U2 = Eng (data example: 3 [rating option of 1 thru 5])
V2 = Appearance (data example: 4 [rating option of 1 thru 5])
W2 = Other (data example: 4 [rating option of 1 thru 5])
X2 = OverAll (data example: 4 [rating option of 1 thru 5])
On the chart the value Y axis would be 0 thru 6. I want the data to show below the chart to act as the legend on the X axis. On the X axis, I would like for the Serial # to show to the left of the chart, then Records thru OverAll to show under the chart with the rating number (1-5) to be in the line chart for each serial number.
In advance, thank you for you help!
I have created charts for data involving the number of parts that is bring bought every month. I have 'months' along the x-axis and 'number of parts' along the y-axis.
I want to create an average line to show if for a particular month, the number of parts is above the average number of parts per month.
Is this possible?
This would seem like a simple thing, but I haven't found a solution. I am creating a line chart based on two columns of data. First column is the year / work week in the format of 201001, 201002, 201003 ... 201052. Second column is the values I wish to plot. When I create a line chart, Excel makes a chart series out of both column A and column B instead of using column A as the x-axis values. It seems that as long as the values in column A look like numbers to Excel, Excel puts them into a series. I could force all col A to be text ('201001, '201002,...) but this is cumbersome and more work than just fixing the chart. I tried to just format the cells as text and then create the chart, that didn't work either. I also tried to create a template after fixing the chart to use column A as the x-axis values and applying it when creating a new chart, this didn't work either. Is there no way to get Excel to recognize that I want column A as my axis instead of a series?
Rick
|
|