Applying If Statement in VBA

  • Okay I am quite new to Microsoft VBA and I am trying to apply a code to a personal finance project.

    This is my objective:

    - If multiple cells exists within a range,

    - then fill it with a particular colour and store the numeric values adjacent to it
    - print the total value to say another cell say Cells(9,2)

    At the moment this is I can only do the 1st object for 1 cell.

    When I wanted to do multiple cells like Range("A1, A3, A4") or Range(Cells(x,y), Cells(x,y)), it gave me an error message.

    Edited once, last by royUK: Add Code Tags, please use them in future when posting code to the Forum. Watch this to see how ().

  • Welcome to Ozgrid.

    I have added code tags for you, please read the Forum Rules to understand why.

    Why do you want VBA to colour the cells, that can be done more efficiently using Conditional Formatting

  • Thank you for the response.

    Sincerely apologise for not taking time to read the Forum Rules. Is it possible to edit the title thread?

    Yes, conditional formatting seems to do the work, however what I want to achieve is get the Excel to find cells that are the same as the ones I control-selected within a range, if that makes sense. And subsequently colour them the same. I mean I could do this one by one using the conditional formatting but I'm looking to do this in one go or a simple function. I believe the other way would be utilising the filtering tool.

    If VBA is not required which I understand, where do you think I can utilise it? And I mean for automation and if you have other suggestions that would be great? So the best way to learn VBA?

    And for the remaining objectives, how do I then get Excel to do: if colour of cell is red then store the numeric values subsequent to it (or maybe say two cells next to it) and then add those values?


  • Your Thread Title seems reasonable.

    Can you explain more fully what you want to do. An example workbook would help.

    Learning VBA is greatly helped by Google and this Forum is a great source of help. Learn by looking at examples and understanding what the code is doing.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!