Results 1 to 3 of 3

Thread: Global Variables - grrr!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Global Variables - grrr!

    I've got some code in Excel VBA and can't get it to run properly. Here's the situation:

    There are two bits of code, one is part of a worksheet (embedded) and the other is a seperate module. There are two procedures in the module and I've got a macro set up on a button in the worksheet which calls one or the other of the procedures depending on the value of a Global Variable. The value of the global variable is set by choosing between 1 of 2 option buttons on the worksheet.

    The problem I have is that I can't get the computer to run either of the two procedures. The code in the module basically goes like this:

    Code:
    If Global_Variable = 1 Then
    run this piece of code 
    
    ElseIf Global Variable = 2 Then
    run this piece of code 
    
    End If
    End If
    I know the problem is not with the run this piece of code and I've tracked it down to a problem with the global variable. I know that the correct values are been passed to it from the worksheet (depending on which option button is pressed) but the computer seems to completely ignore this.

    I've tried declaring the variable as a Public variable all over: in the worksheet code, in the module declarations, in the ThisWorkbook section. But it still doesn't seem to want to recognise the variable when I try to make a decision based on it. However, it must know the value of the variable because I can write it to a cell!

    What I am doing wrong?!?!

    Cheers
    -Rob

    *Edit*

    One thing I should mention is that I got it to work in a rather unelegant way. I made a cell on the worksheet equal the global variable, so if one option button is ticked it equals 1, if the other is ticked it equals 2, then set the code up to look like this:

    Code:
    If Range("A1") = 1 Then
    run this piece of code 
    
    ElseIf Range("A1") = 2 Then
    run this piece of code 
    
    End If
    End If
    That's how I know the run this piece of code works as I can make it do what I want by referencing a cell, but when I try to reference a global variable which the cell also depends upon it just doesn't work.
    Last edited by TheRobster; Nov 28th, 2004 at 09:02 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Here is a small demo of two option buttons triggering a public
    function in a standard module. The function evaluates which
    option button is selected and displays a msgbox of the selected
    option.

    Note: set your macro security level to medium so it can run.
    Nothing else will run until an option is selected.

    HTH

    VB Code:
    1. 'This is all thats in there.
    2. Public Function SomePublicFunction()
    3.     If ActiveWorkbook.Sheets(ActiveWorkbook.ActiveSheet.Name).OptionButton1.Value = -1 Then
    4.         MsgBox "Opt 1"
    5.     ElseIf ActiveWorkbook.Sheets(ActiveWorkbook.ActiveSheet.Name).OptionButton2.Value = -1 Then
    6.         MsgBox "Opt 2"
    7.     Else
    8.         'Etc
    9.     End If
    10. End Function
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Global Variables - grrr!

    Excel debugging:
    Put a break point in front of code in question
    If break point is hit, step through (hope excel doesn't crash)
    Use watch and locals windows to make sure variable has correct value
    Use debug.print and msgbox to MAKE SURE variable has correct value

    If variable has correct value on line before code, but not during line THEN:
    Excel is playing games with you. See section 2.

    Section 2:
    -Comment and Uncomment the line. This has LITERALLY WORKED for me. Who knows why, some kind of twisted excel logic.
    -Restart your computer to give you time to calm down and maybe fix some random problem. Consider buying a new computer w/o excel
    -Move the function around, rename the variable, try random things
    -Explode in anger
    Don't pay attention to this signature, it's contradictory.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width