Dash if Zero VBA

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Good evening

Looking to insert a dash if cell value is a zero

using

With range("E2:E" & lastrow)

.numberformat = ?????
.Value = .Value

End With
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Good evening

Looking to insert a dash if cell value is a zero

using

With range("E2:E" & lastrow)

.numberformat = ?????
.Value = .Value

End With
If you want to use Cell Formatting to display a dash when the cell value is 0, you would use this...

.NumberFormat = "[=0]-"

However, the .Value=.Value line of code makes it seem like you are trying to physically change the formatted dash to a real dash. If so, you cannot do it that way; rather, remove the two dotted code lines inside the With..EndWith block to this...

.Replace 0, "-", xlWhole, , , , False, False
 
Upvote 0
Maybe this

Code:
Sub MM1()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each c In Range("E2:E" & lastrow)
        If c.Value = 0 Then c.Value = "-"
        c.HorizontalAlignment = xlRight
    Next c
End Sub
 
Upvote 0
Mr Rothstein

Thank You for the explanation. I did not realize this. the cell formatting is what I was looking to attain. I'll keep the other line of code in the repository.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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