If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > excel macro problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-04, 17:27
grogport grogport is offline
Registered User
 
Join Date: Jan 2004
Posts: 2
excel macro problem

I have created the following macro to draw graphs from selected data. I want to convert the macro so that it will draw a graph FROM WHICHEVER data is selected in the spreadsheet. How do I do it?

' Keyboard Shortcut: Ctrl+w
'
Charts.Add
ActiveChart.ApplyCustomType ChartType:=xlUserDefined, TypeName:="Default"
ActiveChart.SetSourceData Source:=Sheets("AF objs % responses").Range("B5:C8" _
)
ActiveChart.Location Where:=xlLocationAsObject, Name:="AF objs % responses"
ActiveChart.ChartTitle.Select
Selection.Characters.Text = "P"
Selection.AutoScaleFont = False
With Selection.Characters(Start:=1, Length:=1).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
ActiveChart.Axes(xlValue).Select
End Sub

Many thanks
Reply With Quote
  #2 (permalink)  
Old 01-30-04, 22:50
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
One way to do it is to define a range at the beginning, then use the InputBox to get the range desired:
Code:
Dim myRng as Range
Prompt = "Select cells for work"
Title = "Select a Range"

Set myRng = Application.InputBox(Prompt:=Prompt, Title:=Title)

ActiveChart.SetSourceData Source:=Sheets("AF objs % responses").Range("myRng")

Adjust as needed.

HTH
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
  #3 (permalink)  
Old 01-31-04, 06:40
grogport grogport is offline
Registered User
 
Join Date: Jan 2004
Posts: 2
My code now as follows.

It doesn't like:-
"ActiveChart.SetSourceData Source:=Sheets("AF objs % responses").Range("myRng")" and crashes at that point.

What am I doing wrong?


Dim myRng As Range
Prompt = "Select cells for work"
Title = "Select a Range"
SetmyRng = Application.InputBox(Prompt:=Prompt, Title:=Title)
Charts.Add

ActiveChart.ApplyCustomType ChartType:=xlUserDefined, TypeName:="Default"
ActiveChart.SetSourceData Source:=Sheets("AF objs % responses").Range("myRng")
ActiveChart.Location Where:=xlLocationAsObject, Name:="AF objs % responses"
ActiveChart.ChartTitle.Select
Selection.Characters.Text = "P"
Selection.AutoScaleFont = False
With Selection.Characters(Start:=1, Length:=1).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
ActiveChart.Axes(xlValue).Select
End Sub
Reply With Quote
  #4 (permalink)  
Old 02-01-04, 10:41
redma_d redma_d is offline
Registered User
 
Join Date: Jan 2004
Location: Newcastle UK
Posts: 10
Just new here so hope i dont tread on toes ... but I just noticed

----------------------------------------------------------------------
SetmyRng = Application.InputBox(Prompt:=Prompt, Title:=Title)
Charts.Add

ActiveChart.ApplyCustomType ChartType:=xlUserDefined, TypeName:="Default"
ActiveChart.SetSourceData Source:=Sheets("AF objs % responses").Range("myRng")
------------------------------------------------------------------------

You need a space between 'Set' and 'myrng' and then remove quotes from .Range("myRng")

D
Reply With Quote
  #5 (permalink)  
Old 02-01-04, 12:11
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Quote:
Originally posted by redma_d
Just new here so hope i dont tread on toes ... but I just noticed

----------------------------------------------------------------------
SetmyRng = Application.InputBox(Prompt:=Prompt, Title:=Title)
Charts.Add

ActiveChart.ApplyCustomType ChartType:=xlUserDefined, TypeName:="Default"
ActiveChart.SetSourceData Source:=Sheets("AF objs % responses").Range("myRng")
------------------------------------------------------------------------

You need a space between 'Set' and 'myrng' and then remove quotes from .Range("myRng")

D
Welcome to the board. You're not stepping on any toes. Any help is appreciated. I didn't get back to the removing of the quotes, so good correction.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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