5

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?

3 Answers 3

4

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

Range("AS22:AU" & LastRow).Replace "", "Greens", xlWhole
Sign up to request clarification or add additional context in comments.

4 Comments

@Iturner I did try that, but got the same results - Nothing. the cells stayed blanks. this is what I had originally LastRow = Cells(Rows.Count, 2).End(xlUp).Row With Range("AS22:AU" & LastRow) .Replace what:="", Replacement:="On Track" End With
Are there formulas within the cells which are causing the cells to show as blank? Have you debugged to ensure that the LastRow variable is returning the expected integer?
@Iturner I will just run LastRow and check that it is returning a value
@Iturner Thanks I checked this and wasn't getting the correct cell number, I changed the column I was running LastRow on and I now get the results I wanted. - Thanks
2

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

5 Comments

this does not work either, the cells stay blank - Any clues as to what this is
@user3088476 I suspect the 2 in the equation for LastRow ............are you sure you want column B to control the end of the loop ??
The column B is a column I know I will contain a value in the last row, the column AS - AU which I am copying in may not contain a value within the last row, this is why I am searching on column B.
@user3088476 Perhaps the cells in AS:AU only appear empty.................. perhaps they contain a single space ??
@ Gary's I have it working now - thanks for you help and input
0

Use IsEmpty() to test for blanks.

    If IsEmpty(r.Value2) Or Trim(r.Value2) = "" Then
        r.Value2 = "Greens"
    End If

Also watch out for cells with a zero length string or a space. They appear empty but in fact are not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.