For each cell in a specific range if blank enter "N/A" - VBA

KNKN9

Board Regular
Joined
Mar 27, 2017
Messages
92
Hi,

I have a code
Code:
 For Each cel In rng            If IsEmpty(cel.Value) Or cel.Value = vbNullString Then
                cel.Value = "N/A"
            End If
        Next cel

However, this code does not seem to work when I enter it in a different module. I can trying to amend this code so it is able to work. I need the code to go through each cell in the specific range and if a cell is blank then enter the string "N/A". If you are able to help that'll be much appreciated.

Thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
If you want to check the range: "A1:A25"

Try this:

Modify to your needs:

Code:
Sub Check_Range()
Dim c As Range
    For Each c In Range("A1:A25")
        If c.Value = vbNullString Then c.Value = "N/A"
    Next
End Sub
 
Upvote 0
If the range is fixed and never changing (I'll assume A1:A25 for example purposes), then you could so this...
Code:
Sub CheckRange()
  [A1:A25] = [If(A1:A25="","N/A",A1:A25)]
End Sub
If the range is not fixed, then you could do this (where you can use whatever method you want to set the address range)...
Code:
Sub Check_Range()
  Dim Addr As String
  Addr = "A1:A25"
  Range(Addr) = Evaluate(Replace("If(@="""",""N/A"",@)", "@", Addr))
End Sub
 
Upvote 0
Hi Rick,

I think the problem I have is that my range is based on selection

Code:
  Dim colLast As LongcolLast = Cells(12, Columns.Count).End(xlToLeft).Column
Cells(12, colLast).Select
Range(Selection, Selection.Offset(44, 0)).select
Dim rng As Range
Set rng= Range(Selection, Selection.Offset(44, 0))

My code at the moments finds the last cell in the data set that has data and from this cell to the last cell in the data set with data.

your code below wouldn't work for me
Code:
    Dim Addr As String
  Addr = "A1:A25"
  Range(Addr) = Evaluate(Replace("If(@="""",""N/A"",@)", "@", Addr))
 
Last edited:
Upvote 0
You can combine your code & Rick's code
Code:
Sub test()

    Dim colLast As Long
    Dim Rng As Range

    colLast = Cells(12, Columns.Count).End(xlToLeft).Column
    Set Rng = Range(Cells(12, colLast), Cells(12, colLast).Offset(44))
    Range(Rng.Address) = Evaluate(Replace("If(@="""",""N/A"",@)", "@", Rng.Address))
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top