[VBA - excel] chart

Mastrohead

New Member
Joined
May 28, 2011
Messages
3
Hi to all I'm a new user.
I have a trouble, I have to create a polar chart on excel. To do this I create a VBA subroutine to simplify its creation (in my mind..). It is a very stupid operation , but probably due to my poorly experience with VBA, I'm not able tio run it!
Can any one help me?!!

hereafter the code:

Sub graph()

Dim c() As Variant
Dim s() As Variant
Dim vn, bw As Single

vn = 0.1
i = 0

Const st = 30

For bw = 0 To 360 Step st
ReDim Preserve c(0 To i)
ReDim Preserve s(0 To i)


c(i) = vn * Cos(Application.Radians(bw))
s(i) = vn * Sin(Application.Radians(bw))

i = i + 1
Next

Charts.Add
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SeriesCollection.NewSeries

ActiveChart.SeriesCollection(1).XValues = c
ActiveChart.SeriesCollection(1).Values = s
ActiveChart.SeriesCollection(1).Name = "=""VN=01"""

With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "titolo gr"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "xValori"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "yValori"
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
End Sub
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
thx VoG for your quickly response,
but I can't use that. I try to create a plane where I will plot many arrays (results of another subroutine) without use the spreadsheets cells.

Up to now, I find that if I reduce the number of the elements on the array c () - setting st=30. I can plot it. This could be acceptable to create the background of the plot, but not when I will plot the data...

Is in VBA chart some length data restriction?
Is possible to set up it?
 
Upvote 0
Hi
Welcome to the board

Try:

Code:
Sub graph()
Dim c() As Double, s() As Double
Dim cht As Chart
Dim ser As Series
Dim vn As Double, bw As Double
Dim i As Long
 
Const st As Long = 30
ReDim c(0 To 360 \ st)
ReDim s(0 To 360 \ st)
 
vn = 0.1
i = 0
 
For bw = 0 To 360 Step st
    c(i) = vn * Cos(Application.Radians(bw))
    s(i) = vn * Sin(Application.Radians(bw))
    
    i = i + 1
Next
 
Set cht = Charts.Add

With cht
    .ChartType = xlXYScatterSmoothNoMarkers
    Set ser = .SeriesCollection.NewSeries
    
    With ser
        .XValues = c
        .Values = s
        .Name = "VN=01"
    End With
 
    .HasTitle = True
    .ChartTitle.Characters.Text = "titolo gr"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "xValori"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "yValori"
 
    .HasAxis(xlCategory, xlPrimary) = True
    .HasAxis(xlValue, xlPrimary) = True
 
    .Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
    With .Axes(xlCategory)
        .HasMajorGridlines = False
        .HasMinorGridlines = False
    End With
 
    With .Axes(xlValue)
        .HasMajorGridlines = False
        .HasMinorGridlines = False
    End With
End With
End Sub
 
Upvote 0
You've already received several suggestions but *how* does your own code not work?
Hi to all I'm a new user.
I have a trouble, I have to create a polar chart on excel. To do this I create a VBA subroutine to simplify its creation (in my mind..). It is a very stupid operation , but probably due to my poorly experience with VBA, I'm not able tio run it!
Can any one help me?!!

hereafter the code:

Sub graph()

Dim c() As Variant
Dim s() As Variant
Dim vn, bw As Single

vn = 0.1
i = 0

Const st = 30

For bw = 0 To 360 Step st
ReDim Preserve c(0 To i)
ReDim Preserve s(0 To i)


c(i) = vn * Cos(Application.Radians(bw))
s(i) = vn * Sin(Application.Radians(bw))

i = i + 1
Next

Charts.Add
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SeriesCollection.NewSeries

ActiveChart.SeriesCollection(1).XValues = c
ActiveChart.SeriesCollection(1).Values = s
ActiveChart.SeriesCollection(1).Name = "=""VN=01"""

With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "titolo gr"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "xValori"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "yValori"
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,547
Members
452,925
Latest member
duyvmex

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top