+ Reply to Thread
Results 1 to 5 of 5

Can't Replicate Success of Nested IF with VBA Code

  1. #1
    Registered User
    Join Date
    06-01-2015
    Location
    USA
    MS-Off Ver
    2010
    Posts
    3

    Can't Replicate Success of Nested IF with VBA Code

    Hello everyone!

    I have a nested IF statement that works perfectly if put directly into the cell on the spreadsheet side, but cannot get it to work if I try to use it as VBA Code instead.

    The set up I am using is based on a series of "Yes" "No" selections:

    <= 8 "Yes" = Pink Elephants "on Parade"

    > 8 And < 13 "Yes" = Pink Elephants "on Vacay"

    Else (anything greater than 13) = Pink Elephants "sound asleep"


    Here is the nested IF statement that is directly put into the cell:

    =IF(C21<=8,"on Parade",IF(AND(C21>8,C21<13),"on Vacay","sound asleep"))


    As mentioned before, it works as should if entered directly into the cell. But when coding the same in VBA:

    Sub PinkElephants()
    If Cells(21, 2) <= 8 Then
    Cells(23, 2) = "on Parade"
    ElseIf Cells(21, 2) > 8 And Cells(21, 2) < 13 Then
    Cells(23, 2) = "on Vacay"
    Else
    Cells(23, 2) = "sound asleep"
    End If
    End Sub

    The cell will only display Pink Elephants "on Parade", and does not change even once the number of "Yes" selections are 9 or greater. Any idea why it is not working?

    I have attached two spread sheets to detail both scenarios (one using only the nested IF statement directly in the cell, and the other spreadsheet only using VBA code).

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Can't Replicate Success of Nested IF with VBA Code

    The formula that counts the results is in C21 - Cells(21,3), not B21 - Cells(21,2) as you have in the code.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    06-01-2015
    Location
    USA
    MS-Off Ver
    2010
    Posts
    3

    Re: Can't Replicate Success of Nested IF with VBA Code

    Quote Originally Posted by Norie View Post
    The formula that counts the results is in C21 - Cells(21,3), not B21 - Cells(21,2) as you have in the code.
    Thanks Norie! I completely missed that.

    Now that the code executes, is there any way to have Cell(23, 2) auto-update itself without having to run the VB Macro (F5) each time? For instance, with 9 "Yes" selections, Cell(23, 2) shows the default "on Parade", but once I hit F5 to run the macro, it updates to "on Vacay". I would like it to auto-update once the "Yes" selections breach the thresholds I've mandated.

    Is this possible?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Can't Replicate Success of Nested IF with VBA Code

    Put this in the sheet 'SaaS Scorecard' module.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-01-2015
    Location
    USA
    MS-Off Ver
    2010
    Posts
    3

    Re: Can't Replicate Success of Nested IF with VBA Code

    Quote Originally Posted by Norie View Post
    Put this in the sheet 'SaaS Scorecard' module.
    Please Login or Register  to view this content.
    Solved!

    Thanks again! Would love to research this particular macro execution further. Any suggestions on where to start (subject)? I have the Excel 2013 Power Programming with VBA book.

+ 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] slow code to replicate a sheet
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-18-2013, 11:44 AM
  2. [SOLVED] Looking for a way to replicate code in Excel
    By tebrow77 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2013, 03:31 PM
  3. VBA Code to replicate Pivot table data and apply a % to a value field
    By kenadams378 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2012, 05:39 AM
  4. VBA code to replicate ShowDetails function
    By bwwhite in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2009, 02:10 PM
  5. VB Code to replicate data ?!
    By Dhimit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-24-2007, 09:54 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