+ Reply to Thread
Results 1 to 26 of 26

Executing Macro based on cell contents

  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 Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,522

    Re: Executing Macro based on cell contents

    This will give you an idea...
    In Sheet Module...
    Please Login or Register  to view this content.
    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 [★ Add Reputation] to left of post window...
    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 Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,522

    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
    Please Login or Register  to view this content.
    and in a std Module

    Please Login or Register  to view this content.
    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 Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,522

    Re: Executing Macro based on cell contents

    Have not set your named ranges...
    Please Login or Register  to view this content.

  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 Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,522

    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....


    Please Login or Register  to view this content.

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

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

    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)
    Please Login or Register  to view this content.
    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 Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,522

    Re: Executing Macro based on cell contents

    Look at the attached...I created the named ranges as Range1, Range2, Range3
    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.
    See image:
    Capture.PNG

  18. #18
    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.

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

  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 Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,522

    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:
    Please Login or Register  to view this content.
    Thx for your patience.

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

    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...
    Please Login or Register  to view this content.

  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 Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,522

    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