+ Reply to Thread
Results 1 to 5 of 5

Macro inside a formula

  1. #1
    Registered User
    Join Date
    05-04-2004
    Posts
    6

    Macro inside a formula

    Is it possible to run a macro from within a formula?

    eg =If(A1="yes", run macro, " ")

    I hope it is! please help me here

    Thanks

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,448
    From some brief tests I did, I think it depends on what kind of procedure you're using. If your "macro" does things that allow it to be defined as a "function" procedure, then you can use it inside of an IF function like that.

    If your macro does things that force you to define it as a sub procedure, I don't believe you can call a sub procedure from within an if function like that. In that case, I think it would be better to define a calculate event sub procedure (or whatever event you need the sub to run on). The first line of this sub procedure would then check the value of A1 and respond accordingly. I think this is about how it would look:

    Sub Worksheet.Calculate()
    If sheets(1).cells(1,1)<>"yes" then
    sheets(1).cells(2,1)=""
    exit sub
    end if
    macro code if cells(1,1)="yes"
    end sub

  3. #3
    Registered User
    Join Date
    05-04-2004
    Posts
    6
    thanks - appreciate it

    my macro is simple - to hide colums is cell = x

    but what do i put in the formula to invoke the macro?

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,448
    hiding a column isn't something you can do from a function.
    I'm not sure if conditional formatting will allow hiding a column as one of the formats it can do.
    If conditional formatting won't work, then you'll need a macro like I suggested above. In my above example, the macro is supposed to be automatically called everytime the worksheet calculates. You could also associate the macro with other events (open, close, etc.). Or you could link to macro to a toolbar button or keyboard shortcut and run the macro manually whenever you want to run the macro.

  5. #5
    Registered User
    Join Date
    05-04-2004
    Posts
    6

    Smile

    thanks for your help - I am playing with your macro now

    appreciated as ever

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1