+ Reply to Thread
Results 1 to 9 of 9

Inserting a function into VBA code

  1. #1
    Registered User
    Join Date
    02-02-2012
    Location
    ILLINOIS
    MS-Off Ver
    Excel 2010
    Posts
    7

    Inserting a function into VBA code

    I have a spreadsheet that I'm trying to get sound output form when a certain value comes up in a cell. I have all the code for the sound output and it works fine when I type in that value, but when the value is created from my excel formula, the sound code doesn't work. I am assuming I need to put my formula into my VBA code, but I's not quite sure how to do it. My formula in the sheet is a nested If/Then statement. How do I write that in VBA code within my module and would I put it before or after the code for the sound output?

    Here is my formula as it appears in the workbook: =IF(B1=C1,"MATCH", IF(B1="",(""),"NO MATCH"))

    My exisitng code is:

    Please Login or Register  to view this content.
    Last edited by cwright; 02-07-2012 at 08:25 AM. Reason: solved

  2. #2
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Inserting a function into VBA code

    Hi

    Worksheet_Change won't pick up changoing values in a cell which contains a formula - you need the Worksheet_Calculate event for that.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: Inserting a function into VBA code

    Sorry, another rule to be aware of:

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    02-02-2012
    Location
    ILLINOIS
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Inserting a function into VBA code

    I'm fairly new to this, so can you elaborate on how to write a Worksheet_Calculate event in my case.

  5. #5
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Inserting a function into VBA code

    Sure:

    Please Login or Register  to view this content.
    would be one way.

  6. #6
    Registered User
    Join Date
    02-02-2012
    Location
    ILLINOIS
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Inserting a function into VBA code

    That works, but the sound is generated whether the cell says "MATCH" or "NO MATCH". I would like to just generate the sound when the cell value is "NO MATCH".

  7. #7
    Registered User
    Join Date
    02-02-2012
    Location
    ILLINOIS
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Inserting a function into VBA code

    With the "MATCH" "NO MATCH" problem,

    Now my code reads as this :

    Please Login or Register  to view this content.
    Last edited by cwright; 02-06-2012 at 03:55 PM. Reason: clarification

  8. #8
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Inserting a function into VBA code

    It is looking in the entire column A - is that what ou want? So if any cell is displaying NO MATCH then it will play the sound. If you are sure that it is playing when it shouldn't be, then can you please post back in a reply the exact code you are using in the Calculate event.

  9. #9
    Registered User
    Join Date
    02-02-2012
    Location
    ILLINOIS
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Inserting a function into VBA code

    Ok it is working exactly how I wanted to. thanks Firefly for all your help.

+ 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