VBA automatically change Min and Max Horizontal X Axis on Chart ? (code included)

SereneSea

New Member
Joined
Feb 2, 2022
Messages
43
Office Version
  1. 2016
Platform
  1. Windows
Hello again VBA Neebie here,
I have another question now. I have a ganntt chart that I would like the x-axis to change with what is in cells H3 and I3. My minimum date start is in H3 and maximum is in I3.
If its possible to have the chart updated as soon as someone enters the dates?
I tried this code but I am getting an automation and unspecified error.


VBA Code:
Sub ScaleXAxisWholeCycle()
    Dim Ch As Chart
    
    On Error Resume Next
    Set Ch = ActiveChart
    On Error GoTo 0
    
    If Not Ch Is Nothing Then
        With ActiveChart.Axes(xlCategory, xlPrimary)
            .MinimumScale = ActiveSheet.Range("H3").Value
            .MaximumScale = ActiveSheet.Range("I3").Value
        End With
    Else
        MsgBox "No Chart Found"
    End If
End Sub
 
apparantly there was a mistake between the X and the Y-axis !
In the first posts it was ".Axes(xlCategory, xlPrimary)", now it's ".Axes(xlValue)

if you now use the change-event in your sheetmodule ( the sheet of H3, I3 and the chart)
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
     If Intersect(Target, Range("H3:I3")) Is Nothing Then Exit Sub
     MsgBox "you changed H3 or I3", vbInformation

     With Me.ChartObjects("Chart 9").Chart                      'or the name of the chart or its' indexnumber
     'With .Axes(xlCategory)                                '---> was the wrong one ??? !!! ??? = IS REMARK NOW, choose 1 of these 2
          With .Axes(xlValue)                                   '--> is the good one
               .MinimumScale = Range("H3").Value
               .MaximumScale = Range("I3").Value
          End With
     End With
End Sub
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
example with chart2.xlsm
BCDEFGHIJKLMNOPQRSTUV
12022DO not delete Columns K to O please
2
3Plan Start3/1/202212/31/2022% Complete 0%
4ConcatDepartmentTestTrainerTraineeTrainee AreaPRIORITYSTARTPLAN ENDACTUAL END% COMPLETEProject Start-(chart start)Days to StartCompleteIncompletePlan Days
5Unicorn (T: whiskers)AlphaUnicornwhiskersBirdDeltaMEDIUM4/5/20227/2/20220%3/1/20223508888
6Frog (T: Pluto)OmegaFrogPlutoWhiskersOmegaHIGH4/5/20226/3/20220%3/1/20223505959
7Cat (T: Saturn)DeltaCatSaturnPlutoAlphaLOW4/5/20226/3/20220%3/1/20223505959
8
9
10
11
12
13
14
15
16
17
18
19
200%
2125%
2250%
2375%
24100%
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Entry
Cell Formulas
RangeFormula
M3M3=AVERAGE(Table1[% COMPLETE])
N5:N7N5=$I$3
O5:O7O5=IF(ISBLANK(J5),0,J5-N5)
P5:P7P5=$M5*$R5
Q5:Q7Q5=R5-P5
R5:R7R5=IF(ISBLANK(K5),0,K5-J5)
C5:C7C5=CONCAT([@Test]," (T: ",[@Trainer],")")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
I5Cell Valuecontains "LOW"textNO
I5Cell Valuecontains "MEDIUM"textNO
I5Cell Valuecontains "HIGH"textNO
M3Other TypeColor scaleNO
M5:M8Other TypeDataBarNO
I4,I6:I7Cell Valuecontains "LOW"textNO
I4,I6:I7Cell Valuecontains "MEDIUM"textNO
I4,I6:I7Cell Valuecontains "HIGH"textNO
Cells with Data Validation
CellAllowCriteria
I5:I7ListHIGH,MEDIUM,LOW
M5:M7List=$F$20:$F$24
 
Upvote 0
nothing ? no msgbox neither ?
An extra msgbox before the intersect

Did you use other macros with a command like "Application.EnableEvents=false" ?

If you save & close your excelfile and reopen it immediately, and then change I3 or J3 (no H3) what happens ?


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

     MsgBox Target.Address

     If Intersect(Target, Range("I3:J3")) Is Nothing Then Exit Sub
     MsgBox "you changed H3 or I3", vbInformation

     With Me.ChartObjects("Chart 9").Chart                      'or the name of the chart or its' indexnumber
     'With .Axes(xlCategory)                                '---> was the wrong one ??? !!! ??? = IS REMARK NOW, choose 1 of these 2
          With .Axes(xlValue)                                   '--> is the good one
               .MinimumScale = Range("i3").Value
               .MaximumScale = Range("j3").Value
          End With
     End With
End Sub
 
Upvote 0
Solution
IT WORKS! I closed and reopened excel and it works now.! Thank you so much for you help :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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