Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
Ed Ed is offline
external usenet poster
 
Posts: 279
Default Create a Graph with a Macro

Hello I have the following code:

Sub Gráfica_Curva_S()
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Charts.Add
ActiveChart.ChartType = xlLineMarkersStacked
ActiveChart.SetSourceData Source:=Sheets("Curva-S").Range("A4:P4"), PlotBy _
:=xlRows
ActiveChart.SeriesCollection(1).Name = "=""Curva-S"""
ActiveChart.Location Whe=xlLocationAsNewSheet, Name:="Gráfica Curva-S"
With ActiveChart.Axes(xlCategory)
..HasMajorGridlines = True
..HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
..HasMajorGridlines = True
..HasMinorGridlines = False
End With
ActiveChart.HasLegend = False
ActiveChart.HasDataTable = False
End Sub


I have 3 questions, help on any is greatly appreciated:

1) On the 6th line of the code, how do I manage to make the range more
dynamic? Instead of having ("A4:P4") to have from A4 until the last column
containing data? (By the way, it is maybe usefull to mention that Row 1
contains the number of that (week) so from A:P I have on Row 1 from 1:15, so
maybe I can have a MAX formula and the code takes that result to specify the
columns for the range or something)

2) On line 10, I specified the name for the Sheet that will be created when
I run the Macro. But when I already have that sheet created and I run the
macro it gives a Run-time Error because that sheet already exists and it
creates a sheet called Sheet # but what is after line 10 of the code is not
created. How do I manage, again, to make a more flexible name, maybe Curva-S
& "current date" (dd-mm-yy) or something like that?

3) I would like to also automatically generate Value Data Labels, aligned
above, rotated 90°, size 6 and Arial font.

Thanks a lot!
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default Create a Graph with a Macro

Putting this code ahead of the first line of your Sub should solve problems
#1 and #2 -

As for #3 - once you've created one of your graphs, use Tools | Macro |
Record New Macro to record the keystrokes you use to set up things the way
that you want regarding Value Data Labels, their alignment, etc. You can use
the recorded macro code as a model for adding to your own Sub to finish it
off.

Sub TestCreatingSheet()
Dim NewSheetName As String
Dim DataRange As String

'make sure sheet name < 31 characters
NewSheetName = "Gráfica Curva_S_" & _
Format(Now(), "m_d_yy_hh_mm")
On Error Resume Next
'go to worksheet to place chart on
Worksheets(NewSheetName).Select
If Err < 0 Then
'sheet DID NOT exist
'add it
Sheets.Add
' new sheet is now the ActiveSheet
'give it the name you want
ActiveSheet.Name = NewSheetName
Err.Clear
Else
'sheet already exists
'may want to exit or
'warn user?
End If
On Error GoTo 0
'You can now use NewSheetName
'to create your chart on
'now back to your sheet with data
Worksheets("Gráfica Curva_S").Select
Range("A4").Select
'now to get dynamic source data
'must have empty cell to mark
'end of data in column P at row 4
DataRange = "A4:" & Range("A4").End(xlToRight).Address
'now where you used .Range("A4:P4") you can use
' .Range(DataRange)
'just so you can see results
MsgBox "DataRange is " & DataRange
End Sub

Hope this helps you out at least a little.

"Ed" wrote:

Hello I have the following code:

Sub Gráfica_Curva_S()
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Charts.Add
ActiveChart.ChartType = xlLineMarkersStacked
ActiveChart.SetSourceData Source:=Sheets("Curva-S").Range("A4:P4"), PlotBy _
:=xlRows
ActiveChart.SeriesCollection(1).Name = "=""Curva-S"""
ActiveChart.Location Whe=xlLocationAsNewSheet, Name:="Gráfica Curva-S"
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = False
ActiveChart.HasDataTable = False
End Sub


I have 3 questions, help on any is greatly appreciated:

1) On the 6th line of the code, how do I manage to make the range more
dynamic? Instead of having ("A4:P4") to have from A4 until the last column
containing data? (By the way, it is maybe usefull to mention that Row 1
contains the number of that (week) so from A:P I have on Row 1 from 1:15, so
maybe I can have a MAX formula and the code takes that result to specify the
columns for the range or something)

2) On line 10, I specified the name for the Sheet that will be created when
I run the Macro. But when I already have that sheet created and I run the
macro it gives a Run-time Error because that sheet already exists and it
creates a sheet called Sheet # but what is after line 10 of the code is not
created. How do I manage, again, to make a more flexible name, maybe Curva-S
& "current date" (dd-mm-yy) or something like that?

3) I would like to also automatically generate Value Data Labels, aligned
above, rotated 90°, size 6 and Arial font.

Thanks a lot!

  #3   Report Post  
Posted to microsoft.public.excel.misc
Ed Ed is offline
external usenet poster
 
Posts: 279
Default Create a Graph with a Macro

Hello JLatham! hey, thanks again for your help! Yeah the code you sent me
definetely helped me make some usefull changes, I seem to understand now a
bit more about codes.
thanks!,
Ed



"JLatham" wrote:

Putting this code ahead of the first line of your Sub should solve problems
#1 and #2 -

As for #3 - once you've created one of your graphs, use Tools | Macro |
Record New Macro to record the keystrokes you use to set up things the way
that you want regarding Value Data Labels, their alignment, etc. You can use
the recorded macro code as a model for adding to your own Sub to finish it
off.

Sub TestCreatingSheet()
Dim NewSheetName As String
Dim DataRange As String

'make sure sheet name < 31 characters
NewSheetName = "Gráfica Curva_S_" & _
Format(Now(), "m_d_yy_hh_mm")
On Error Resume Next
'go to worksheet to place chart on
Worksheets(NewSheetName).Select
If Err < 0 Then
'sheet DID NOT exist
'add it
Sheets.Add
' new sheet is now the ActiveSheet
'give it the name you want
ActiveSheet.Name = NewSheetName
Err.Clear
Else
'sheet already exists
'may want to exit or
'warn user?
End If
On Error GoTo 0
'You can now use NewSheetName
'to create your chart on
'now back to your sheet with data
Worksheets("Gráfica Curva_S").Select
Range("A4").Select
'now to get dynamic source data
'must have empty cell to mark
'end of data in column P at row 4
DataRange = "A4:" & Range("A4").End(xlToRight).Address
'now where you used .Range("A4:P4") you can use
' .Range(DataRange)
'just so you can see results
MsgBox "DataRange is " & DataRange
End Sub

Hope this helps you out at least a little.

"Ed" wrote:

Hello I have the following code:

Sub Gráfica_Curva_S()
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Charts.Add
ActiveChart.ChartType = xlLineMarkersStacked
ActiveChart.SetSourceData Source:=Sheets("Curva-S").Range("A4:P4"), PlotBy _
:=xlRows
ActiveChart.SeriesCollection(1).Name = "=""Curva-S"""
ActiveChart.Location Whe=xlLocationAsNewSheet, Name:="Gráfica Curva-S"
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = False
ActiveChart.HasDataTable = False
End Sub


I have 3 questions, help on any is greatly appreciated:

1) On the 6th line of the code, how do I manage to make the range more
dynamic? Instead of having ("A4:P4") to have from A4 until the last column
containing data? (By the way, it is maybe usefull to mention that Row 1
contains the number of that (week) so from A:P I have on Row 1 from 1:15, so
maybe I can have a MAX formula and the code takes that result to specify the
columns for the range or something)

2) On line 10, I specified the name for the Sheet that will be created when
I run the Macro. But when I already have that sheet created and I run the
macro it gives a Run-time Error because that sheet already exists and it
creates a sheet called Sheet # but what is after line 10 of the code is not
created. How do I manage, again, to make a more flexible name, maybe Curva-S
& "current date" (dd-mm-yy) or something like that?

3) I would like to also automatically generate Value Data Labels, aligned
above, rotated 90°, size 6 and Arial font.

Thanks a lot!

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default Create a Graph with a Macro

Glad it did you some good.

"Ed" wrote:

Hello JLatham! hey, thanks again for your help! Yeah the code you sent me
definetely helped me make some usefull changes, I seem to understand now a
bit more about codes.
thanks!,
Ed



"JLatham" wrote:

Putting this code ahead of the first line of your Sub should solve problems
#1 and #2 -

As for #3 - once you've created one of your graphs, use Tools | Macro |
Record New Macro to record the keystrokes you use to set up things the way
that you want regarding Value Data Labels, their alignment, etc. You can use
the recorded macro code as a model for adding to your own Sub to finish it
off.

Sub TestCreatingSheet()
Dim NewSheetName As String
Dim DataRange As String

'make sure sheet name < 31 characters
NewSheetName = "Gráfica Curva_S_" & _
Format(Now(), "m_d_yy_hh_mm")
On Error Resume Next
'go to worksheet to place chart on
Worksheets(NewSheetName).Select
If Err < 0 Then
'sheet DID NOT exist
'add it
Sheets.Add
' new sheet is now the ActiveSheet
'give it the name you want
ActiveSheet.Name = NewSheetName
Err.Clear
Else
'sheet already exists
'may want to exit or
'warn user?
End If
On Error GoTo 0
'You can now use NewSheetName
'to create your chart on
'now back to your sheet with data
Worksheets("Gráfica Curva_S").Select
Range("A4").Select
'now to get dynamic source data
'must have empty cell to mark
'end of data in column P at row 4
DataRange = "A4:" & Range("A4").End(xlToRight).Address
'now where you used .Range("A4:P4") you can use
' .Range(DataRange)
'just so you can see results
MsgBox "DataRange is " & DataRange
End Sub

Hope this helps you out at least a little.

"Ed" wrote:

Hello I have the following code:

Sub Gráfica_Curva_S()
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Charts.Add
ActiveChart.ChartType = xlLineMarkersStacked
ActiveChart.SetSourceData Source:=Sheets("Curva-S").Range("A4:P4"), PlotBy _
:=xlRows
ActiveChart.SeriesCollection(1).Name = "=""Curva-S"""
ActiveChart.Location Whe=xlLocationAsNewSheet, Name:="Gráfica Curva-S"
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = False
ActiveChart.HasDataTable = False
End Sub


I have 3 questions, help on any is greatly appreciated:

1) On the 6th line of the code, how do I manage to make the range more
dynamic? Instead of having ("A4:P4") to have from A4 until the last column
containing data? (By the way, it is maybe usefull to mention that Row 1
contains the number of that (week) so from A:P I have on Row 1 from 1:15, so
maybe I can have a MAX formula and the code takes that result to specify the
columns for the range or something)

2) On line 10, I specified the name for the Sheet that will be created when
I run the Macro. But when I already have that sheet created and I run the
macro it gives a Run-time Error because that sheet already exists and it
creates a sheet called Sheet # but what is after line 10 of the code is not
created. How do I manage, again, to make a more flexible name, maybe Curva-S
& "current date" (dd-mm-yy) or something like that?

3) I would like to also automatically generate Value Data Labels, aligned
above, rotated 90°, size 6 and Arial font.

Thanks a lot!

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a macro to create a macro in another workbook Gizmo63 Excel Worksheet Functions 2 May 15th 06 09:48 AM
How do I create a macro that will compare columns and place data CompuCat Excel Worksheet Functions 0 March 20th 06 06:21 PM
Vb macro stops when I try to create more than 89 charts Tiberius Charts and Charting in Excel 0 January 19th 06 05:52 PM
how to create automatic macro with if statement or similar betatryck_se Excel Discussion (Misc queries) 2 December 14th 05 11:32 AM
How do I create an excel macro to append to a cell with existing i zola_tiara Excel Discussion (Misc queries) 4 September 14th 05 08:22 PM


All times are GMT +1. The time now is 09:45 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"