0

I am relatively new to VBA. I am attempting to automate the generation of a graph containing Beta values using the following steps:

Step 1 - Generate a list of random numbers in Column A
Step 2 - Based on values in Column A, apply the Beta.Dist formula in Column B
Step 3 - Generate a scatterplot based on values in Column B, the scatterplot should look like a U-shaped distribution curve

I've tried using the following code but I am only seeing 1 value in the scatter plot (i.e., the scatterplot only has one dot).

Here's the code:

Option Explicit
Option Base 1
Sub Macro1()
    Application.ScreenUpdating = False
    Dim i%, j%
    Range("A1").Select
    Selection = "0.99"
    Selection.Offset(0, 1) = "Beta(" & Selection & ", " & Selection & ") Density"
    For i = 1 To 301
        Selection.Offset(i, 0).Formula = "= rand()"
        Selection.Offset(i, 1).Formula = "=BETA.DIST(A2:A302,0.99,0.99,False)"
    Next i
    j = ActiveSheet.ChartObjects.Count
    ActiveSheet.Shapes.AddChart2(-1, xlXYScatter, 98 + j * 10, 16.5 + j * 10, 319, 296.5).Select
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("B2:B302")
    ActiveSheet.ChartObjects(j + 1).Activate
    ActiveChart.SetElement (msoElementChartTitleAboveChart)
    Selection.Caption = "="
    ActiveChart.Axes(xlCategory).MaximumScale = 1
    ActiveChart.Axes(xlCategory).Format.Line.Visible = msoFalse
    ActiveChart.Axes(xlCategory).MajorGridlines.Delete
    ActiveChart.Axes(xlValue).MaximumScale = 1.1
    ActiveChart.Axes(xlValue).TickLabels.NumberFormat = "0.000#"
    ActiveChart.Axes(xlValue).Format.Line.Visible = msoFalse
    ActiveChart.Axes(xlValue).MajorGridlines.Delete
    Range("E1") = ""
    Range("I1") = ""
    Range("B1").Select
    Application.ScreenUpdating = True
End Sub

I'm hoping to get a U-shaped beta distribution curve in the scatter plot. Appreciate any form of help and thank you in advance!

1 Answer 1

1

You're not setting the chart data correctly:

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A2:B302")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.