Excel/formula vba

Advertisement


Question
QUESTION: Hi Tom

Is there a formula or a small piece of VBA that will replace only cell that contains a "0" with an underscore.?  If so would it be possible to localise the function.? E.g. replace any cell in  the range w1:w20 that contains a "0" with an underscore?

ANSWER: Christopher Mitchell,


Sub abc()
Dim cell as Range
for each cell in Worksheets("Sheet2").Range("W1:W20")
  if Trim(cell.Text) = "0" then
    cell.Value = "_"
  end if
Next
End Sub

Change W1:W20  to the range you want to process

or if you want to select a range and then run the macro it would be


Sub abc()
Dim cell as Range
for each cell in Selection
  if Trim(cell.Text) = "0" then
    cell.Value = "_"
  end if
Next
End Sub


--
Regards,
Tom Ogilvy



---------- FOLLOW-UP ----------

QUESTION: Hi Tom

Again many thanks for your patience and knowledge.  One last question, for now: can the line in the code

“for each cell in Worksheets("Sheet2").Range("W1:W20")”

Be changed to:

“for each cell in Worksheets("Sheet2").Range("W1:W20, X1:X20, Y1:Y10")”

To accommodate different ranges?

Thanks is advance

Chris Mitchell

ANSWER: you ranges are contiguous so you could do

for each cell in Worksheets("Sheet2").Range("W1:Y20")

but

For each cell in Worksheets("Sheet2").Range("W1:W20,X1:X20,Y1:Y20")

would also work.


The ranges don't have to be contigous   

For each cell in Worksheets("Sheet2").Range("W1:W20,Y1:Y20")

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Hi Tom

I am trying to rewrite some of the code you created for me.  The code you wrote replace a "0" with and "_".  I am trying to write the code so that either a, "0" or a blank space "" is replaced by an underscore. This is what I have come up with:

Sub replacezero()
Dim cell As Range
For Each cell In Worksheets("Sheet2").Range("C1:C20")
 If Trim(cell.Text) = "0" Or "" Then
   cell.Value = "_"
 End If
Next
End Sub

Thanks in advance

Chris Mitchell

Answer
Chris Mitchell

this will do  0, "", " "

Sub replacezero()
Dim cell As Range
For Each cell In Worksheets("Sheet2").Range("C1:C20")
If Trim(cell.Text) = "0" Or len(trim(cell.text)) = 0 Then
  cell.Value = "_"
End If
Next
End Sub

or

Sub replacezero()
Dim cell As Range
For Each cell In Worksheets("Sheet2").Range("C1:C20")
If Trim(cell.Text) = "0" Or cell.text = "" or cell.text = " " Then
  cell.Value = "_"
End If
Next
End Sub






--
regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.