Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Blank/empty cell with a string value

Hope someone can help. I need to populate any blank/empty cells within a range of cells with a specific string. I also don't know that the last row would be in that range so I am looking for that first, I know that line of code works as I have used it for another function within the script. Below is the code I am using: -

    LastRow = Cells(Rows.Count, 2).End(xlUp).Row

    For Each r In Range("AS22:AU" & LastRow)
        If r.Value2 = vbNullString Then
            r.Value2 = "Greens"
        End If
    Next

I don't seem to get any compile errors when I run the code, it just does not work, I still have the same blank/empty cells.

Can anyone shed any light onto what I am doing wrong?

like image 602
user3088476 Avatar asked Mar 02 '16 12:03

user3088476


People also ask

How to replace blank cells with text in Excel?

In the popping up Find and Replace dialog box, under the Replace tab, keep the Find what box blank, enter the certain text you want to replace the blank cells into the Replace with box, and finally click the Replace All button.

How to fill a cell with a blank cell in Excel?

In this method, we will use the REPLACE function to fill in the specific text with a blank cell. The REPLACE function in Excel replaces characters in a text string indicated by location with characters from another text string. In our dataset, we will create a duplicate of the Food Type column.

How to replace certain cells with certain content in Excel?

1. Select the range with blank cells you need to replace with certain content, then press Ctrl + H keys simultaneously to open the Find and Replace dialog box. 2. In the popping up Find and Replace dialog box, under the Replace tab, keep the Find what box blank,...

How to replace empty string or blank values to Nan in Python?

Using replace () method you can also replace empty string or blank values to a NaN on a single selected column. df2 = df. Courses. replace ('', np. nan, regex = True) print( df2)


2 Answers

You could just use the .Replace Method with the range. It would be much quicker.

Range("AS22:AU" & LastRow).Replace "", "Greens", xlWhole
like image 81
luke_t Avatar answered Sep 24 '22 05:09

luke_t


How about:

Sub dural()
    Dim r As Range, LastRow As Long

    LastRow = Cells(Rows.Count, 2).End(xlUp).Row
    For Each r In Range("AS22:AU" & LastRow)
        If r.Text = "" Then r.Value = "Greens"
    Next r
End Sub

This will change:

  • truly empty cells
  • cells containing formulas returning Null
  • cells containing Null character as a constant
like image 40
Gary's Student Avatar answered Sep 24 '22 05:09

Gary's Student