VBA - if cell value true, offset, check value then delete if criteria met

  • Hi all,


    Been a while since I have been on here or even coded in VBA, little rusty so need some help and start building a template toolbox for myself.
    What i need the code to do is Look at a value in a column, then when it finds it, offset below and check to see what the next value is.
    If it is "AVAIL" or "ENROUTE" then delete the entire row, then rinse and repeat.
    Once i have the logic built for this one, i can modify the rest to do LOFF etc.
    Here is the logic to the code I want, but of course its not working, could be as simple as I forget that i need to define something or im using the wrong suffix etc.


  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    Try this:

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    1.Try to avoid using active cells or selecting cells if you can. In your particular case, the code wasn't activating any cells, so the active cell was always the most recent cell selected by you before you ran the macro.
    2. If you use "Or" inside an "If" statement, you need to completely restate the argument.


    Wrong:

    Code
    If ActiveCell.Value = "ENROUTE" Or "AVAIL" Then


    Right

    Code
    If ActiveCell.Value = "ENROUTE" Or ActiveCell.Value = "ENROUTE" = "AVAIL" Then


    Keep in mind that in the actual code I provided in the previous response, I removed all instances of "activecell"


    I hope this helps!


    Sincerely,
    Max

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    *Edit to the last post in the "Right" Example of an Or statement
    1.Try to avoid using active cells or selecting cells if you can. In your particular case, the code wasn't activating any cells, so the active cell was always the most recent cell selected by you before you ran the macro.
    2. If you use "Or" inside an "If" statement, you need to completely restate the argument.


    Wrong:

    Code
    If ActiveCell.Value = "ENROUTE" Or "AVAIL" Then


    Right

    Code
    If ActiveCell.Value = "ENROUTE" Or ActiveCell.Value = "AVAIL" Then


    Keep in mind that in the actual code I provided in the previous response, I removed all instances of "activecell"


    I hope this helps!


    Sincerely,
    Max

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    hey max,
    Thanks for the comment back. I had a look and offset nowadays (i use to use it back in 2010), seems the wrong way to go, so all you are doing is adding a +1 to the cell count and checking the value.
    doesnt seem to work,it deleted one "LON" and that was it, from my understanding this however looks from the bottom up yes? I kinda need it to go from top to bottom.


    So the list will look something like this


    Column F
    LON
    AVAIL
    JOB
    ENROUTE
    LOF
    LON
    ENROUTE
    AVAIL
    JOB
    ENROUTE
    LOF


    I need it to look at the value LON and if below it BEFORE the first JOB it has avil or enroute, to delete it. sometimes could be just LON then AVAIL or LON then ENROUTE or both.

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    Are you trying to delete the "LON" row or the Enroute/Avail row?

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    Can you send me a list of what the following would look like after the macro is ran:
    Column F
    LON
    AVAIL
    JOB
    ENROUTE
    LOF
    LON
    ENROUTE
    AVAIL
    JOB
    ENROUTE
    LOF
    LON
    BLAH

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    hey max, I made some other code but it does the same thing as your code.. it only deletes the LON, what i need is if ENROUTE, AVAIL is between LON and ONJOB then delete the AVAIL/ENROUTE


    [TABLE="width: 77"]

    [tr]


    [td]

    LON

    [/td]


    [/tr]


    [tr]


    [td]

    WRAPUP

    [/td]


    [/tr]


    [tr]


    [td]

    ENROUTE

    [/td]


    [/tr]


    [tr]


    [td]

    ONJOB

    [/td]


    [/tr]


    [tr]


    [td]

    AVAIL

    [/td]


    [/tr]


    [tr]


    [td]

    LOFF

    [/td]


    [/tr]


    [tr]


    [td]

    ENROUTE

    [/td]


    [/tr]


    [tr]


    [td]

    ONJOB

    [/td]


    [/tr]


    [tr]


    [td]

    AVAIL

    [/td]


    [/tr]


    [tr]


    [td]

    ENROUTE

    [/td]


    [/tr]


    [tr]


    [td]

    ONJOB

    [/td]


    [/tr]


    [tr]


    [td]

    WRAPUP

    [/td]


    [/tr]


    [tr]


    [td]

    ENROUTE

    [/td]


    [/tr]


    [tr]


    [td]

    ENROUTE

    [/td]


    [/tr]


    [tr]


    [td]

    ONJOB

    [/td]


    [/tr]


    [tr]


    [td]

    AVAIL

    [/td]


    [/tr]


    [tr]


    [td]

    ENROUTE

    [/td]


    [/tr]


    [tr]


    [td]

    ONJOB

    [/td]


    [/tr]


    [tr]


    [td]

    AVAIL

    [/td]


    [/tr]


    [tr]


    [td]

    ENROUTE

    [/td]


    [/tr]


    [tr]


    [td]

    ONJOB

    [/td]


    [/tr]


    [tr]


    [td]

    AVAIL

    [/td]


    [/tr]


    [/TABLE]



  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    so i need to remove all the enroute/avail (and others) that are between LON and the first instance of ONJOB (don't need to declare first instance i think as they need to LON before they do their first job anyway.

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    Try this:



    If this is wrong, can you send me a list before and after the macro is ran?

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    max I think the above post got it, ill run the script over a few sheets, but from what I can see it did the job.
    Ill add some extra sub routines to remove LON and some other values. and then repeat it for LOF.
    +1

  • Re: VBA - if cell value true, offset, check value then delete if criteria met


    works a treat, was about to use this as a template for the other step of the code. Many thanks, rep added

Participate now!

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