Search code examples
excelvbainputbox

Selecting a variable range and replacing blank fields with value "NA"


I would like to have a button in my Excel sheet that:
1) Asks me to select the range I want to use
2) Changes the blank cells found in this range to a fixed value ("NA")

I could find how to get a box asking me to select a range, but not a solution on changing the values in combination with this box.


Solution

  • You can use SpecialCells() to do this in only two lines:

    Sub t()
    Dim rng As Range
    Set rng = Application.InputBox("Select a range", Type:=8).SpecialCells(xlCellTypeBlanks)
    rng.Value = "NA"
    End Sub