+ Reply to Thread
Results 1 to 7 of 7

VBA IF AND Statement

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA IF AND Statement

    Hi, I wonder if someone may be able to help me please.

    I'm trying to put together the following formula in VBA: =IF(AND($C3<>$D3,$B3="B&C"),$D3-$C3,0), but I'm having a little difficulty in doing so.

    I've put together the following code snippet which will add the calculation to each row within a given range to the last used row.

    Please Login or Register  to view this content.
    But Debug shows a 'Compile' error.

    I just wondered whether someone could possibly look at this please and let me know where I'm going wrong.

    Many thanks and kind regards

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA IF AND Statement

    It's failing because of single quotation marks. Use:

    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: VBA IF AND Statement

    you need to double up on the quotes inside quotes.

    "=IF(RC3<>RC4 AND RC2=""B&C""),RC4-RC3,0)"
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA IF AND Statement

    Code corrected for relative ranges in formula, and to apply to multiple cell range:

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA IF AND Statement

    Hi @OllyXLS, thank you very much for taking the time to reply to my post and for the solution, it works perfectly.

    All the best and kind regards

  6. #6
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA IF AND Statement

    Hi Andy, thank you very much for taking the time to reply to my post and for the formula.

    I did try the code you kindly provided but the line caused a 'Type Mismatch' error.

    Many thanks and kind regards

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: VBA IF AND Statement

    I assume you also corrected the invalid formula syntax in your orginal code? I was just pointing out the lack of double quotes.

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA IF AND Statement

    Hi Andy, sorry my mistake.

    All the best and kind regards

+ 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. VBA Compile Error : line number or label or statement or end of statement
    By excellearner121 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2013, 06:41 PM
  2. [SOLVED] Combining If Statement, Is Blank Statement and HLookUp
    By kimberlyre2000 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-29-2012, 01:27 PM
  3. Replies: 4
    Last Post: 06-01-2012, 10:05 AM
  4. Replies: 4
    Last Post: 05-16-2012, 05:33 PM
  5. [SOLVED] Utilize a Select Case Statement in Target Intersect Statement
    By max57 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-29-2009, 08:55 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