+ Reply to Thread
Results 1 to 10 of 10

Create an if statement in VB to display value or leave blank.

  1. #1
    Registered User
    Join Date
    01-12-2015
    Location
    Seattle, WA, USA
    MS-Off Ver
    2013
    Posts
    19

    Question Create an if statement in VB to display value or leave blank.

    I've exhausted the amount of IF statements i can use in the formula bar in excel 2003, and now must rely on VB editor to create the appropriate function.

    But need a little guidance. Here is what my if statement should look like in the formula bar if it was supported:

    =IF(SPECS!B170="P",SPECS!I170,IF(SPECS!I170="NA","",IF(SPECS!B170="K",SPECS!I170,IF(SPECS!I170="NA","",IF(SPECS!B170="C",SPECS!I170,IF(SPECS!I170="NA","",IF(SPECS!B170="F",SPECS!I170,IF(SPECS!I170="NA",""))))))))

    There are four letters that could be displayed in cell SPECSB170 depending on choice of vendor (P = "pacific", K = "keller", etc.). Based on that vendor choice, the appropriate information represented in the cell SPECS!I170 ought to display.

    If the information comes up as "NA," then the cell should be left blank.

    I understand how this would be created in the formula bar with a basic if/else statement, but when transitioning over to Visual Basic editor, the formatting need to be a little different, i understand.

    If possible, could you please help me convert the above formula to a readable format for VB editor. Thanks!

    Here's what i attempted:

    Please Login or Register  to view this content.
    It doesn't work.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Create an if statement in VB to display value or leave blank.

    Hi Talk_4,

    When I see these "too many If statements" I wonder if you know about VLookup() where you can simply look down a list to find the "If" part of your formula and return other columns in the same row.

    Read about VLookup() at: https://exceljet.net/tips/how-to-use...-of-nested-ifs

    If the above link doesn't make sense then attach a sample workbook so we can show you exactly how to do a VLookup with a table.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    01-12-2015
    Location
    Seattle, WA, USA
    MS-Off Ver
    2013
    Posts
    19

    Re: Create an if statement in VB to display value or leave blank.

    VLookup() will display the content within the cell itself, but, when that content itself happens to be"NA," then the cell should be set to blank.

    The cell that i'm referencing, already is referencing from another source. And that original source may have content as "NA" to signify that this product is unavailable. Forgive me if i didn't point this out in my first posting.

    I'm familiar with Vlookup(), but instead i'm using index, here's why: http://www.mbaexcel.com/excel/why-in...-than-vlookup/

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Create an if statement in VB to display value or leave blank.

    You can use Index Match as well as VLookup but the question was to eliminate all the repeat IF statements. Build a Table and put "NA" as one of the things looked up.

    Attach a sample workbook and I can build the table.. Click on "Go Advanced" and then the Paper Clip Icon above the message area.

  5. #5
    Registered User
    Join Date
    01-12-2015
    Location
    Seattle, WA, USA
    MS-Off Ver
    2013
    Posts
    19

    Post Re: Create an if statement in VB to display value or leave blank.

    here you go.

    TestWBforNAblank.xls

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Create an if statement in VB to display value or leave blank.

    What is the difference between the Invoice Sheet and the Bid sheet. They look the same to me.

    What formula do you want to simplify and on which sheet? I don't understand the question yet.

  7. #7
    Registered User
    Join Date
    01-12-2015
    Location
    Seattle, WA, USA
    MS-Off Ver
    2013
    Posts
    19

    Re: Create an if statement in VB to display value or leave blank.

    The invoice sheet is the result of the bid sheet, which pulls that information from the inventory. Basically, the invoice allows for a printer-friendly list of items that are put in the bid. So they must reference one another, leading all the way from the inventory sheet.

    The challenge at this point, is to display the inventory items off the bid sheet onto the invoice sheet, but should an item be considered unavailable ("NA"), the cell ought to display as blank so as to not convolute the invoice sheet with a list of NA's, but rather, blankness.

    The formula in the formula bar won't recognize if("specified cell"="NA", "") towards the end of the formula, because prior to that statement, the previous statements already stated, supersedes the latter. Ergo, when i say If('specified cell'="P",'cell content',...), so the cell content has seniority over the latter statement [if("specified cell"="NA", "")] and will display the "NA" irrespectively, which should be blank.

    If this doesn't make sense, and if your willing, i'd be willing to do a screen share with you.

    Hope this makes sense,

    Cris

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Create an if statement in VB to display value or leave blank.

    Hi Cris,

    I'll look at this harder tomorrow but it seems you need to know about IfError() function in the newer version of Excel. This would make the NA go to "".
    Bump this with a larger set of data in your example if I'm not even close to what you need as an answer.

    http://www.myonlinetraininghub.com/e...sy-workarounds

  9. #9
    Registered User
    Join Date
    01-12-2015
    Location
    Seattle, WA, USA
    MS-Off Ver
    2013
    Posts
    19

    Re: Create an if statement in VB to display value or leave blank.

    Good evening Marvin,

    First I would like to say that I appreciate your assistance with this formula.

    The NA you see in the inventory sheet in the excel file I attached, is a physical NA and not a result from any errors. In fact, it's done by intention for the bid, to show that this item must not be added to the invoice. When the bid items get sent over to the invoicing sheet, they all get sent together. I, therefore, must hide the any items that have been NA'd, so i go ahead and hide them. Now, there will always be a several items left intentionally blank towards the bottom; when i say blank, i mean, NA'd. Now instead of having those line items say NA, i would prefer, or rather, my boss would prefer them to be blank. This results in a better looking and professional invoice, while maintaining its dynamic functionality. So, you see, there will always be content to be had; whether the description of an item or simply 'NA.' But the trick I would like to overcome is: How can I write this formula so that, if the value reads 'NA' to return a blank cell, while maintaining readability if not NA.

    Again, if you have any confusions about my requests, I'd be glad to do a screen share with you.

    Cris

  10. #10
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Create an if statement in VB to display value or leave blank.

    If I am following this correctly this is a slightly modified VLOOKUP. It has an error handler, but as MarvinP stated the IFERROR function in the newer versions is much simpler to apply and easier to edit / debug.

    I left your original Invoice Sheet table in place and moved mine down to row 11.

    Here is a 2003 compatible formula:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    that includes qualifying the duplicate "P"s with a nested COUNTIF. The COLUMNS($A:A) device allows for the one formula to be filled down and across without having to hard code the columns argument.

    The formula for newer versions of Excel would look like this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    which is much shorter.

    File is attached.

    Does this do what you want?
    Attached Files Attached Files
    Last edited by FlameRetired; 03-03-2015 at 03:06 AM.

+ 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. Summing Cells that contain an IF Statement to leave the cell blank if zero
    By sweeteri in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-14-2014, 12:58 PM
  2. IF Statement to leave cell blank if multiple cells are all blank
    By sweeteri in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 03-14-2014, 12:02 PM
  3. Replies: 2
    Last Post: 09-20-2013, 01:43 PM
  4. MS 2010 - IF Statement to reutrn date or leave cell blank
    By persais in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 03-14-2013, 02:48 PM
  5. IF Statement to leave cell completely blank
    By david1987 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-28-2012, 10:27 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