+ Reply to Thread
Results 1 to 17 of 17

Macro based on cell value

  1. #1
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Macro based on cell value

    I'm struggling with the following problem:

    In the attached file, I would need a macro that would expand the formula in D4 to all cells in column D if the cell on the same row in column B is not empty. Of course, this should only apply to the cells D4:D35, D43:D74 and D82:D113. In between, the value in the cell in column D should not be changed.

    It should also work in case a row is inserted or deleted (and that's the problem I'm struggling with; I managed to get Macro 1 work, but it fails if I remove or insert a row).

    If someone could help me out, I'd be very grateful :-)
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro based on cell value

    Macros generally don't respond as well as formulae when it comes to inserting/deleting rows. Why not use a formula? Wouldn't putting the formula below in D4 and filling down to D35 (then pasting and filling down through your other ranges) get you the desired result, macro-free?

    =IF(ISBLANK($B4),"",VLOOKUP($B4,Table!$A$2:$B$78,2,FALSE))

  3. #3
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Macro based on cell value

    I know, but my intention is not to need any formula fixing when inserting a row (other people have to work with the file too, and I don't want them to screw with the formulas) :-)

  4. #4
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro based on cell value

    Where will users be inserting rows?

  5. #5
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Macro based on cell value

    Between rows 4 and 35, between 43 and 74 and between 82 and 113.

  6. #6
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro based on cell value

    If I understand correctly, then if all they're doing is adding data to previously empty rows, they don't need to touch the formula at all. Fill it all the way from D4:D35. When the row is blank, the cell in D will be blank. When they add an entry to B, D will make the VLOOKUP automatically. Try experimenting with the attachment, let me know how it does/doesn't work.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Macro based on cell value

    Well, if all lines are used, and a line has to be added for instance between row 10 and 11, the cel in column D in the new row will be empty. And I'd like a macro so I can make a button that someone just has to press if they add a row instead of copying the formula.

  8. #8
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro based on cell value

    I see now. In the real database, will there always be a date in column A if there's an entry in column B?

  9. #9
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Macro based on cell value

    Unfortunately, no. A date will only be in the database once, and the rest of the actions on that date are added below, without a date.

  10. #10
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro based on cell value

    Ok, try this one - run from your data page, not the table.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Macro based on cell value

    Thank you so much!

    Unfortunately I don't really understand it, but it works

  12. #12
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro based on cell value

    Glad I could help. It basically loops through all of your rows and, where B is not empty but C and D are, it stops and plants your Vlookup formula in D. Will all values in column B have a corresponding value in the "table" worksheet? If not, we'll need to add an error handler based on what you want to happen for those cells.

  13. #13
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Macro based on cell value

    Well, if there is no corresponding value, I'd like a certain text (like 'missing' or 'error') in that cell.

    Thanks again :-)

  14. #14
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro based on cell value

    Try this one. You can replace "Missing" if you want something else entered when there are failed matches.

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Macro based on cell value

    Again, thank you so much

    I just noticed that if a certain value is added to column D, the macro cannot overwrite that later? It's not major problem (not at all), but if the description in column B is changed, we have to erase the value in D too in order for the macro (if run again) to assign the new corresponding value?
    Last edited by _Nebur_; 04-04-2016 at 02:23 PM.

  16. #16
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro based on cell value

    You could go with this version, which puts the actual formula in column D, so it will respond to any changes in "Table":

    Please Login or Register  to view this content.
    The procedure looks for rows where C is blank but B is not. The goal is to avoid your subtotal rows, so keep the conditions in mind if you make any changes.

  17. #17
    Registered User
    Join Date
    01-30-2016
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Macro based on cell value

    Did I already mention you're awesome? :-)

+ 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. Using Macro to change cell color based on reference cell value in different sheet
    By Emma Ford in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-09-2015, 03:51 AM
  2. Replies: 3
    Last Post: 08-16-2014, 09:55 AM
  3. Macro to change colour of cell based on finding a specific word in the cell
    By Shelby761 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2014, 09:46 AM
  4. Create Auto-Calculating Cell and Assign Cell Values Based on a Macro
    By Titanium Fox in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-26-2013, 12:00 AM
  5. [SOLVED] Event Macro to insert text in cell based on input of another cell
    By nplouffe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-08-2012, 11:54 AM
  6. 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
  7. Macro to auto populate the destination cell based on the values of the previous cell
    By Avinash Kumar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2009, 02:06 AM

Tags for this Thread

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