+ Reply to Thread
Results 1 to 26 of 26

Executing Macro based on cell contents

Hybrid View

  1. #1
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Executing Macro based on cell contents

    I'm trying to have it so when a specific cell is filled out with specific text it runs a specific macro. This macro populates a specific range of cells relative to the text cell (example cell A1)


    so:

    IF
    A1="text1"
    THEN
    Execute 'Macro1' (which fills range C1:D5)

    I also want to be able to set other possible text values to run other macros, example: text2 runs macro2, text3 runs macro3, etc.

    In a nutshell:

    If trigger text in A1 is typed, Macro will run and paste specific range of text in C1:D5

    Now I need this to be relative, so if this same text is typed in A10, for example, the macro will now populate C10:D15

    Or perhaps there's a way to do this without macros, as each of the desired "paste" items have named ranges.

    Thanks!
    Last edited by m_roussakis; 09-27-2018 at 03:50 PM.

  2. #2
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Executing Macro based on cell contents

    WorkSheet_Change event should be used
    Teach me Excel VBA

  3. #3
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    Can you elaborate? I'm not experienced with VBA at all.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    This will give you an idea...
    In Sheet Module...
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("A:A")) Is Nothing Then
            Application.EnableEvents = False
            If Target = "text1" Then
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 2) = "Text1" 'Call macro1
            ElseIf Target = "text2" Then
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 2) = "Text2" 'Call macro2
            ElseIf Target = "text3" Then
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 2) = "Text3" 'Call macro3
            End If
        End If
        Application.EnableEvents = True
    End Sub
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  5. #5
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    I played around with this...

    Problem is that I can't see where the macro is called?

    Cells(Target.Row, 1).Offset(, 2).Resize(6, 2) = "Macro1" 'Call macro1

    If I type "Macro1" where you say to call the macro, it just fills in the word "macro1" in the offset range....

  6. #6
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    perhaps there's a way to call a named range rather than a macro?

    so in other words, when column A finds specific text string, offset 2 columns to the right, and paste "named range XXX"

    is this possible?

  7. #7
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    This was just an example...
    I have no idea what your macro must do...The Call macro indicates to the code to run Macro1 which will be found in a std Module
    i.e

    This line normally will be changed to Call macro1
    Cells(Target.Row, 1).Offset(, 2).Resize(6, 2) = "Text1"
    and in a std Module

    Sub Macro1()
       'Do something
    End sub
    Perhaps you should upload a sample file depicting your requirement...

  8. #8
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    I attached a sample.
    see sheet called "named ranges" also.
    Attached Files Attached Files

  9. #9
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    Have not set your named ranges...
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("A:A")) Is Nothing Then
            Application.EnableEvents = False
            If Target = "text1" Then
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 12) = Sheet2.Range("A5:L10").Value
            ElseIf Target = "text2" Then
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 12) = Sheet2.Range("A13:L18").Value
            ElseIf Target = "text3" Then
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 12) = Sheet2.Range("A21:L26").Value
            End If
        End If
        Application.EnableEvents = True
    End Sub

  10. #10
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    Pasted this code into Sheet1

    I renamed my sheet containing the ranges to match what you gave me (Sheet2).

    It's not working. When I type 'text1' in column A on Sheet1, nothing appears at the offset you indicated.

  11. #11
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    I've added your code, and as you can see from attached, doesn't seem to be working. any ideas? thx . where did i mess up? :p
    Attached Files Attached Files

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    Works fine for me...
    Untitled.png

  13. #13
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    I reloaded the example sheet and it worked. However when I applied this to my actual document, I get a script error.

    Here's the details of my actual document:

    - I have many named ranges, all the same size of 8 rows x 13 columns
    - If it matters (which I don't think it does) the named ranges are all on sheet "Components"
    - The trigger string would be anywhere in column B within sheet "Sheet2"
    - I need the named range to be inserted at offset of (-1, 1) from the trigger string cell.
    - I need the inserted named range to retain all properties (colors, dynamic dropdowns, font, etc)

    I have a feeling that a cut and paste macro may be additionally needed here.

    Here's my code right now. Obviously it has issues....


    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("B:B")) Is Nothing Then
            Application.EnableEvents = False
            If Target = "Crew_Key_Non_Prompt" Then
                Cells(Target.Row, 1).Offset(-1, 1).Resize(8, 13) = Components.Range(***HOW CAN I SPECIFY A NAMED RANGE HERE***).Value
            ElseIf Target = "Crew_Key_Prompt" Then
                Cells(Target.Row, 1).Offset(-1, 1).Resize(8, 13) = Components.Range(***HOW CAN I SPECIFY A NAMED RANGE HERE***).Value
            ElseIf Target = "Crew_Key_Target" Then
                Cells(Target.Row, 1).Offset(-1, 1).Resize(8, 13) = Components.Range(***HOW CAN I SPECIFY A NAMED RANGE HERE***).Value
            End If
        End If
        Application.EnableEvents = True
    End Sub

    Thanks
    Last edited by m_roussakis; 09-27-2018 at 10:41 AM.

  14. #14
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    In future please view the forum rules before posting. Your post does not comply with rule #2.
    2. Programming code must be enclosed in code tags to improve readability. (A, Z)
     Your Code
    So, do the following:
    Edit your post, highlight your code and click the [#] button at the top of the post window.

  15. #15
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    done. thx for letting me know.

  16. #16
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    Look at the attached...I created the named ranges as Range1, Range2, Range3
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("A:A")) Is Nothing Then
            Application.EnableEvents = False
            If Target = "text1" Then
                Sheet2.Range("Range1").Copy
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 12).PasteSpecial xlPasteAll
            ElseIf Target = "text2" Then
                Sheet2.Range("Range2").Copy
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 12).PasteSpecial xlPasteAll
            ElseIf Target = "text3" Then
                Sheet2.Range("Range3").Copy
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 12).PasteSpecial xlPasteAll
            End If
        End If
        Application.EnableEvents = True
        Application.CutCopyMode = False
    End Sub
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    Still having difficulties

    Pls tell me if I understand your code:

    Where I have written "Components" in bold, does this mean the sheet where the named ranges reside? If so, I am getting an error on this . See below for screenshot. I have only edited the code for one text string/named range combo: Crew_Key_Target, so pls ignore the other 2 text strings.

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("B:B")) Is Nothing Then
            Application.EnableEvents = False
            If Target = "Crew_Key_Target" Then
                Components.Range("Crew_Key_Target").Copy
                Cells(Target.Row, 1).Offset(-1, 1).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "text2" Then
                Sheet2.Range("Range2").Copy
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 12).PasteSpecial xlPasteAll
            ElseIf Target = "text3" Then
                Sheet2.Range("Range3").Copy
                Cells(Target.Row, 1).Offset(, 2).Resize(6, 12).PasteSpecial xlPasteAll
            End If
        End If
        Application.EnableEvents = True
        Application.CutCopyMode = False
    End Sub
    See image:
    Capture.PNG

  18. #18
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347
    Sheets("Conponents")...
    Not in front of pc.. Upload your fule or we going to go back and forth forever...

  19. #19
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    wow i realized that the proper names I give sheets are not what is to be referred to in code. I should have used Sheet1.... in any case ignore last comment. I'll test it out now.

  20. #20
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    no worries. I got it working perfectly! thanks!!

  21. #21
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    Please mark thread as solved ...
    Thank you for adding rep +

  22. #22
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    ....solved however I have a new issue.

    If I try to add new rows anywhere I get a run-time error. I tried it on the example you sent me, and it's happens there too. Any ideas why?

    Screenshot from your example:
    Capture.PNG

    Here's my code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("B:B")) Is Nothing Then
            Application.EnableEvents = True
            If Target = "Crew_Key_Non_Prompt" Then
                Sheet1.Range("Crew_Key_Non_Prompt").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Crew_Key_Prompt" Then
                Sheet1.Range("Crew_Key_Prompt").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Crew_Key_Target" Then
                Sheet1.Range("Crew_Key_Target").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Crew_Speed" Then
                Sheet1.Range("Crew_Speed").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Crew_Speed_Overspeed" Then
                Sheet1.Range("Crew_Speed_Overspeed").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Crew_Train_Orientation" Then
                Sheet1.Range("Crew_Train_Orientation").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Crew_Verbal_Confirmation" Then
                Sheet1.Range("Crew_Verbal_Confirmation").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Dispatcher_Action" Then
                Sheet1.Range("Dispatcher_Action_button").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Fence_Validation" Then
                Sheet1.Range("Fence_Validation").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Fence_Validation" Then
                Sheet1.Range("Fence_Validation").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Set_Device" Then
                Sheet1.Range("Set_Device").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Train_Switch_Navigation" Then
                Sheet1.Range("Train_Switch_Navigation").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Train_Target_Approach" Then
                Sheet1.Range("Train_Target_Approach").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Train_Target_Interaction" Then
                Sheet1.Range("Train_Target_Interaction").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            ElseIf Target = "Train_Timed_Movement" Then
                Sheet1.Range("Train_Timed_Movement").Copy
                Cells(Target.Row, 1).Offset(-1, 2).Resize(8, 13).PasteSpecial xlPasteAll
            End If
        End If
        Application.EnableEvents = True
        Application.CutCopyMode = False
    End Sub
    Thx for your patience.

  23. #23
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    Remove this part in all the code...it ids not necessary when pasting...Forgot to remove it when I changed code earlier...
    .Resize(8, 13)

  24. #24
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    I removed those lines in my code. Still getting error if adding new rows.

  25. #25
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Executing Macro based on cell contents

    can't say without your workbook upload...Are you adding new rows to the named ranges or what

  26. #26
    Registered User
    Join Date
    09-13-2018
    Location
    canada
    MS-Off Ver
    office 2013
    Posts
    27

    Re: Executing Macro based on cell contents

    no i'm adding rows withing the sheet. I'm not touching the ranges.

    i've attached a version of my sheet.

    thx.
    Attached Files Attached Files

+ 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. Excel Macro to find cell in worksheet based on contents of another cell
    By MeBeMel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-13-2017, 11:37 AM
  2. Need a macro to add 1 to active cell based on previous cell contents
    By wtom in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-20-2014, 12:16 AM
  3. Macro to move row based on cell contents
    By alpha608 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-17-2013, 04:08 AM
  4. [SOLVED] Need help writing macro to replace one cell based on contents of adjacent cell
    By TheRaptMuse in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2012, 12:54 PM
  5. Macro that will clear contents of cell based on format of text in adjacent cell
    By judasdac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-12-2012, 01:56 AM
  6. Run macro based on cell contents
    By Billip in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-16-2011, 06:55 PM
  7. Executing a macro based on worksheet change
    By Rynofasho in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-07-2010, 08:44 PM

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