0

I have this working macro button that will "reset all status" in the range B2:B15 to a default prompt for my colleagues to indicate job's status:

screenshot

Private Sub CommandButton1_Click()

    Sheets("Sheet1").Range("B2:B15").Value = "please enter status"

End Sub

I want this macro to stop populating the value "please enter status" in the B column if there are no values in adjacent A column. Looking at screenshot it would leave B6:B15 empty. Column A values are vlookup results. If no result, vlookup returns zeros (currently hidden in white font with the help of conditional formatting)

Appreciate your help!

braX
  • 11,506
  • 5
  • 20
  • 33
  • been a long time since I touched vba but you're gonna need to loop over everything between A2:A15 and determine if the mirroring B cell should get a value or not. – misterManager Nov 15 '19 at 20:04
  • 1
    Would you not rather return `""` instead of 0 to avoid messing with formats? Conditional formatting is volatile. – JvdV Nov 15 '19 at 22:49

1 Answers1

1

Maybe like this:

Private Sub CommandButton1_Click()
    Dim rng as Range
    Set rng = Sheets("Sheet1").Range("A2:A15")

    If Application.CountA(rng) > 0 Then
        rng.SpecialCells(xlCellTypeConstants).Offset(,1).Value = "please enter status"
    End If
End Sub

EDIT:

If the cells in the column are formulas, then change xlCellTypeConstants to xlCellTypeFormulas.

EDIT 2:

This is a simple way to do what you're looking for:

Private Sub CommandButton1_Click()
    Dim rng As Range
    Set rng = Sheets("Sheet1").Range("B2:B15")

    rng.Formula = "=IF(A2<>0,""please enter status"", """")"
    rng.Value = rng.Value
End Sub

Or with Evaluate:

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Set ws = Sheets("Sheet1")

    Dim rng As Range
    Set rng = ws.Range("B2:B15")

    rng.Value = ws.Evaluate("IF(A2:A15<>0,""please enter status"", """")")
End Sub

EDIT 3: (3rd time's the charm?)

Another option would be to have the Vlookup return a blank string "" instead of 0 if there's no job number found.

Then you could leverage the 2nd parameter of Range.SpecialCells, like this (as proposed by @JvdV):

Private Sub CommandButton1_Click()
    Dim rng As Range
    Set rng = Sheets("Sheet1").Range("A2:A15")

    rng.SpecialCells(xlCellTypeFormulas, xlNumbers).Offset(, 1).Value = "please enter status"
End Sub

EDIT 4:

You can also make use of AutoFilter:

Private Sub CommandButton1_Click()
    With Sheets("Sheet1").Range("A1:B15")
        .AutoFilter 1, ">0"
            If .Cells.SpecialCells(12).Count > 2 Then .Offset(1).Resize(14, 2).Columns(2).Value = "Please enter status"
        .AutoFilter
    End With
End Sub
JvdV
  • 70,606
  • 8
  • 39
  • 70
BigBen
  • 46,229
  • 7
  • 24
  • 40
  • actually, it is returning an error if I use this code with cells containing formulas, not values. For the purpose of this question I simplified the example. In fact, my column A contains vlookup results – Pavlo Kobzar Nov 15 '19 at 20:43
  • Change `xlCellTypeConstants` to `xlCellTypeFormulas`. Do you only have formulas in that column? If there is a mix of formulas/text, then you need a slightly different approach, which I am happy to show, just let me know. – BigBen Nov 15 '19 at 20:47
  • is working but ignoring the empty vlookup value in the adjacent cell. I want this macro to stop if adjacent cell equals to zero (no vlookup result). thanks – Pavlo Kobzar Nov 15 '19 at 21:00
  • perhaps add a couple hidden "service" columns that macro will react to, which are connected to the vlookup results. just brainstorming here... – Pavlo Kobzar Nov 15 '19 at 21:28
  • Untested but you could play around with [`XlSpecialCellsValue`](https://learn.microsoft.com/en-us/office/vba/api/excel.xlspecialcellsvalue) to return a range including only numeric values. However, It would only work when op changes the return from 0 to `""` in the lookup. That would make the volatile formatting redundant too. That's the theory, can't test since my wife won't let me near a pc at 12 at night ;) – JvdV Nov 15 '19 at 22:57
  • @BigBen I have updated with another possibility (also including `SpecialCells` but teamed up with `AutoFilter`). Feel free to delete if you so desire. Thought it would be best to complement yours instead of posting a new answer. – JvdV Nov 16 '19 at 09:19
  • @JvdV. It's good. I thought about this approach briefly but then moved on to other ideas. Never thought we'd get to edit 4 :) – BigBen Nov 16 '19 at 10:39
  • If you are interested, I've asked a question on `AutoFilter` [here](https://stackoverflow.com/q/58889516/9758194). I have since recently found that you don't need to use `SpecialCells` on a filtered range. The method does it for you, however there is just one concern, which would be the first header row that gets populated. I'm looking to see if there is a way to avoid using several methods and properties just to cater for that one flaw. Not sure if you ok with me redirecting you to the question? – JvdV Nov 16 '19 at 16:11
  • 1
    @JvdV taking a look now, though I'm not sure I can throw together an answer today. – BigBen Nov 16 '19 at 16:22
  • Even though I changed vlookup result return blank, Edit 3 and Edit 4 didn't work (errors that I don't know how to debug). Edit 2 worked fine with conditional formatting hiding the zero (I know you hate it guys :) but that's the only way this macro works and I'm on tight schedule with my project) I will keep trying to find out why Edit 3 and 4 are not working. – Pavlo Kobzar Nov 18 '19 at 14:31
  • @BigBen, JvdV my bad, I missed a couple syntax error. Both, Edit 3 and 4 are working in this example test sheet. Now my challenge is to transfer/edit this macro to a huge sheet with multiple vlookup results and 20 buttons like this. For some reason, even though I have zeros replaced by "" via this function =iferror(vlookup(value, array, column, false),"") both Edit 3 and Edit 4 are returning nothing, just doing an empty computation without errors. Thanks, anyways, this has been a huge help and breakthrough for me! – Pavlo Kobzar Nov 18 '19 at 16:45