Consulting

Results 1 to 5 of 5

Thread: Solved: Cell reference in global declaration

  1. #1
    VBAX Newbie
    Joined
    Feb 2007
    Posts
    3
    Location

    Question Solved: Cell reference in global declaration

    Hi

    I have written some vba using absolute cell addresses
    eg - If ['Sheet1'!A1] > 25 Then ...
    Is it possible to declare a global const at the top of the module instead
    eg - Const AgeCell = Worksheets("Sheet1").cells(1, 1)
    and then use this code in the Subroutine
    eg - If AgeCell > 25 Then ...
    However, I've tried declaring with Const, Set & Public but cannot get it to work.

    Any ideas?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not a constant, but you can create a variable and the SET the varaibale to that range.

  3. #3
    VBAX Newbie
    Joined
    Feb 2007
    Posts
    3
    Location
    Thanks


    What would the code be to declare a cell variable? Would it be
    Dim AgeCell As Variant (Is Variant the right data type?)
    Set AgeCell = Worksheets("Sheet1").cells(1, 1)
    And can I put both lines in the Global Declaration area of the module so AgeCell can be used by different subroutines?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No

    Dim AgeCell As Range

    Variant will work, as it will create a Range subtype, but Range is more efficient.

    You can declare the variable in the declaratives, and it will be accessible throughout the module. Wherever it gets changed, that change will be picked up.

  5. #5
    VBAX Newbie
    Joined
    Feb 2007
    Posts
    3
    Location
    Many thanks

    Thats now works perfectly.


Posting Permissions

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