+ Reply to Thread
Results 1 to 9 of 9

IF AND in -VBA

  1. #1
    Registered User
    Join Date
    09-27-2018
    Location
    london
    MS-Off Ver
    excel
    Posts
    39

    IF AND in -VBA

    Hi Guys,


    I wonder if you can help please

    I am not really ggod with VBA so I was wondering if anyone can help


    I need to have a IF and formla in VBA for a range if cells.

    PLease se sample attached

    Any help would be great

    Thanks

    Vito
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Ipswich, England
    MS-Off Ver
    Excel 2016 (Win 10 - Work) & 365 Subscription (Win 10 - Home)
    Posts
    23,627

    Re: IF AND in -VBA

    Why VBA?

    You can achieve this with a formula and conditional formatting, and it would be automatic - no clicking to validate.

    Your sample data is incorrect, by the way.

    In E2 copied down:

    =IF(AND(B2="Acquavi",A2="Vito"),"BAD","")

    Your existing CF will do the rest.
    Last edited by AliGW; Yesterday at 06:24 AM.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!

    STOP PRESS: Forum Rules Updated September 2018! Please read them here.
    Are you new to Power Query and don't know what to do with the code you've been given? Have a look here.
    Don't forget to say "thank you" to those who have helped you in your thread. If you wish, you can also reward them by clicking on their reputation star bottom left.

  3. #3
    Registered User
    Join Date
    09-27-2018
    Location
    london
    MS-Off Ver
    excel
    Posts
    39

    Re: IF AND in -VBA

    Hey,

    Thanks a million for coming back to me

    The reason for it is because I need the message to appear based on the given conditions and then employee will type over the cells wich have the macro. I have tried with the conditional formatting but there is a limitation. With conditional formatting I can red color the cell but then not sure as to how make the alert text appear within the cell.

    I hoep this is clear

    Thanks

    Vito

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Ipswich, England
    MS-Off Ver
    Excel 2016 (Win 10 - Work) & 365 Subscription (Win 10 - Home)
    Posts
    23,627

    Re: IF AND in -VBA

    I've shown you. However, if you want an employee to be able to overwrite that column, you probably do need VBA. Why would they need to be able to overwrite a validation column, though?

  5. #5
    Registered User
    Join Date
    09-27-2018
    Location
    london
    MS-Off Ver
    excel
    Posts
    39

    Re: IF AND in -VBA

    Hi,
    Might be easier to expalin by showing you a better example (see attached).

    Currently in column "N" I have set the two conditions: 1 via formula and one via condtional formatting. If I could avoid this and have both set cond formats for coulmn "M" via a macro instead

    Can you think of a better solution? I was hoping the conditonal formatt would this can be done but as per attached but in column "M" and then the employee could type over the vat amout

    Thanks

    Vito
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    852

    Re: IF AND in -VBA

    Edit: I will have a look at your new example also.. but I rewriten your first macro

    biggest problem in that macro is that you are trying to fill a value in a whole column, that does not work (as you noticed)
    you need to loop thru the lines and assign bad or ok to each line

    look at this code ( I stayed as close as possible to your code to help understand the differences)

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-27-2018
    Location
    london
    MS-Off Ver
    excel
    Posts
    39

    Re: IF AND in -VBA

    Thank you! It is great.

  8. #8
    Registered User
    Join Date
    09-27-2018
    Location
    london
    MS-Off Ver
    excel
    Posts
    39

    Re: IF AND in -VBA

    Hi,

    Can I ask you, is it possible to make the message appear/change instantly without haveing to click on the validation button?

    Thanks

    Vito

  9. #9
    Valued Forum Contributor
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    852

    Re: IF AND in -VBA

    Sure.. that was a bit of my plan after I saw your new example.

    so that resulted in 1 code which i Put in Module1 this is to validate 1 line in the expense form.
    I also choose not to use the help column and CF to color the cells. In my code the Column M cell will be made RED if there is no amount and a comment is inserted. to warn the user an amount needs filling.

    Please Login or Register  to view this content.
    To trigger this code there are 2 ways.

    first is an event macro that needs to be put in the sheet module called sheet1(EXPENSE CLAIM FORM)

    Please Login or Register  to view this content.
    This event macro will trigger each time a cell in the range H12:M24 is changed (I assumed the expense form would be 12 lines long from 12 to 24 but this ofcourse can be changed to your needs)

    What the event macro does is ask to excecute the line validation macro for the current line based on Target.row as row to validate (Target is comparable to Activecell and is used in eventmacros by default)

    The other way to trigger is by a second piece of code in Module1. The name of that form speaks for itself it will test the whole expense form again for VAT lines. This might be usefull to have for the accounting staff to (Double) check submitted forms.

    Please Login or Register  to view this content.
    This code is nothing more than a simple loop that will call the line validations where r is the rownumber to validate. again the assumption here that expence form has 12 rows from 12 to 24 which you can simply adjust to actual no of last possible row

    I choose not to use conditional formatting because then you would basicly create 2 validations 1 thru vba and 1 in conditional formatting formula. which will hold the risk that one is changed but not the other giving inconsistent results. So I feel it is better to control the message and format of cell in 1 code.

    have a look at the attachment for some testruns...
    Attached Files Attached Files

+ 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