VBA event activate worksheet compile error

bmaas93

New Member
Joined
Jan 6, 2016
Messages
3
Hi,

I'am a beginner to VBA and trying to get a code running I got from a tutorial. The only thing I wanted to change is the trigger event. Change it from change cell to active worksheet.

For some reason I keep getting the compile error: a variable is not defined (or something like that, I have my excel in Dutch).

This is the code:
Code:
Option Explicit

Private Sub Worksheet_Activate()
  With ActiveWorkbook.ChartObjects("Grafiek 14").Chart
    Select Case Target.Address
      Case "$B$14"
        .Axes(xlCategory).MaximumScale = Target.Value
      Case "$B$15"
        .Axes(xlCategory).MinimumScale = Target.Value
      Case "$B$16"
        .Axes(xlCategory).MajorUnit = Target.Value
      Case "$T$3"
        .Axes(xlValue).MaximumScale = Target.Value
      Case "$T$4"
        .Axes(xlValue).MinimumScale = Target.Value
      Case "$T$16"
        .Axes(xlValue).MajorUnit = Target.Value
    End Select
  End With
End Sub

I hope someone can help me out.

Thanks.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
There're no variables in your presented code. Hence your missing variable declaration is out there.
Always include Option Explicit directive on top of all your modules. You can even ask VBA to do it automatically - Tools=>Options=>Require Variable Declaration.
Put it there and execute Debug=>Compile VBAProject. If there're some missing declarations, VBE will highlight the row.
 
Last edited:
Upvote 0
Hi Sektor,

Thank you for your response. Option Explicit is included.

Where should the missing variable be inserted? I really have no clue..
 
Upvote 0
When error message appears, press Debug button - it will lead you to the row with error.
 
Upvote 0
Oops, didn't notice.
You refer to ChartObjects of Workbook, but it's in Worksheet object. You have missing sheet reference. Should be something like this:
Code:
With ActiveWorkbook[B].Sheets("NAME_OF_SHEET")[/B].ChartObjects("Grafiek 14").Chart
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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