Link to home
Start Free TrialLog in
Avatar of Kiwi-123
Kiwi-123

asked on

Vb help

Please can you help with an error message I get in my spreadsheet, the weird thing is that I have the same code in another and do not encounter this error.

When I click the command button "j = " comes back in error (Compile error variable not defined)

My best guess is that the code works in up to date versions of excel, however it does not seem to work in excel 2003.

Please can you help.
help-12-09-2012.xls
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Is j defined in the sheet where you are having the problem? If not and you want both sheets to share the same j you should move the definition to a module and define it as Public, like

Public j As Integer ' or whatever type it currently is
Avatar of Kiwi-123
Kiwi-123

ASKER

Hi Martin, many thanks for your advice, I don't think j is defined anyplace else. Could this be done at the top of the VB pane, something like 'Option Explicit'?

If not would you be able to show me an example of the module?

Thanks,

Gaz
What I'k talking about is what is called the scope of a variable. Briefly...

If a variable is defined in a Sub or Function it's scope is only that Sub or Function which means that it's only available in that Sub or Function.

If a variable is defined at the top of a Sheet with Dim or Private it's scope is only that sheet.

If a variable is defined as Public in a module it's scope is the whole workbook.


I'm assuming that you have it defined in one sheet and you're trying to use it in another which is why you are getting the error, so as I said, move the definition to a module.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just add Dim j As Long at the top of the sub.
Don't do that if you want to use the same j as in your other sheet.
Martin

There is no other code.:)

Also, it's probably not a good idea to have a variable like this as public.
I didn't notice his attachment and of course I know that it's not a good idea.