+ Reply to Thread
Results 1 to 6 of 6

Macro name to run defined by cell

Hybrid View

  1. #1
    Registered User
    Join Date
    05-24-2013
    Location
    Hampshire
    MS-Off Ver
    Office 2013
    Posts
    49

    Macro name to run defined by cell

    Hey Guys

    Done a search but not found exactly what I need.

    Basically what I am trying to achieve is, having cell A1 have the word "red" or "green" inputted, and basically a vba code such as

    If cell A1 had the word RED, I would want the following:

    Private sub colourchange()
    'run macro name based off cell A1
    run red
    end sub

    But say cell A1 had the word GREEN, I would want the following to happen:

    Private sub colourchange()
    'run macro name based off cell A1
    run green
    end sub

    Hope you understand what I am trying to achieve.

    Many thanks in advance,

    Ash

  2. #2
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Macro name to run defined by cell

    Would this be of any help?

    Sub RunColour()
    Dim sVal As String
        sVal = Range("A1").Value
        Select Case sVal
            Case "Red": Call Red
            Case "Green": Call Green
        End Select
    End Sub
    
    Sub Red()
        MsgBox "Red running.", vbInformation
    End Sub
    
    Sub Green()
        MsgBox "Green running.", vbInformation
    End Sub
    Regards,
    Rudi

  3. #3
    Forum Contributor
    Join Date
    12-27-2012
    Location
    cebu, Philippines
    MS-Off Ver
    Excel 2016
    Posts
    210

    Re: Macro name to run defined by cell

    you can do an if condition like below

    
    sub colourchange()
    if Range("A1") = "RED" then
       call Red_Sub
    elseif Range("A1") = "GREEN" then
       call Green_Sub
    end if
    end sub
    
    Sub Red_Sub()
    'Your code here for the red...
    end sub
    
    Sub Green_Sub()
    'Your Code here for the green...
    end sub
    so the code above has 3 subs - 1 main sub (colourchange) and two smaller sub (Red_Sub and Green_Sub). The Main sub is the one you will use so it decides which smaller sub it will run based on the color or text on cell A1...
    don't worry, there's a lot of people that are far more confused than you
    but if you liked what i suggested. Click for me the "Add Reputation" - that way, we'd be both happy.

  4. #4
    Valued Forum Contributor
    Join Date
    09-01-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    324

    Re: Macro name to run defined by cell

    You can paste the code below in the ThisWorkbook vba window of the project. It will automatically run the sub red if you type RED in A1. You can add as many of the if lines as you want. A1 is the key range and you can change or expand it to a larger range.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim KeyCells As Range
    
        Set KeyCells = Range("A1")
        
        If Not Application.Intersect(KeyCells, Range(Target.Address)) _
               Is Nothing Then
    
             If KeyCells.Value = "RED" Then red 'Macro to run
            'If KeyCells.Value = "GREEN" Then green ' Macro to run
           
        End If
    End Sub
    Last edited by playaller; 05-25-2014 at 04:11 PM.


    Shelton A.
    If Helpful, Add Reputaion!

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Macro name to run defined by cell

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        If Target.Address(,,,True) = "Sheet1!$A$1" Then
            Application.Run CStr(Target.Value)
        End If
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  6. #6
    Registered User
    Join Date
    05-24-2013
    Location
    Hampshire
    MS-Off Ver
    Office 2013
    Posts
    49

    Re: Macro name to run defined by cell

    Works like a dream, thanks awfully!

    Regards

    Ash

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to copy selected cell and paste into another (defined) cell.
    By Barking_Mad in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2014, 11:36 AM
  2. Macro to Insert defined integer into range defined by variable criteria
    By stereofeedback in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-06-2013, 12:33 PM
  3. [SOLVED] Macro to add defined text to the end of multiple defined columns of data
    By spikedog in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-28-2012, 05:10 AM
  4. Application-defined or object-defined error with unshare/share macro
    By rlw in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-23-2007, 07:58 AM
  5. Need help with cell defined in Macro
    By Maria in forum Excel General
    Replies: 0
    Last Post: 08-16-2005, 11:05 AM

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