[英]How to find a specific cell and replace by a text
On an active range selection, how to find only the cells that contains "0" and "#N/A" - and replace it by text "NA" and change the font color to "red".在活动范围选择中,如何仅查找包含“0”和“#N/A”的单元格 - 并将其替换为文本“NA”并将字体颜色更改为“红色”。
Here is the macro I am using to "convert formulas to absolute values " and "to find empty cells to put text "NA".这是我用来“将公式转换为绝对值”和“查找空单元格以放置文本“NA”的宏。
sub XConvertToValues()
Dim MyRange As Range
Dim MyCell As Range
Set MyRange = Selection
For Each MyCell In MyRange
If MyCell.HasFormula Then
MyCell.Formula = MyCell.Value
End If
If IsEmpty(MyCell.Value) = True Then
MyCell.Value = "NA"
End If
Next MyCell
End Sub
edited after OP's clarification about data format在 OP 澄清数据格式后进行编辑
use Replace()
and AutoFilter()
method of Range
object使用Range
对象的Replace()
和AutoFilter()
方法
Sub XConvertToValues()
With Selection
.Value = .Value '<--| convert all formulas to their values
.Replace What:="#N/A", replacement:="NA", LookAt:=xlWhole
.Replace What:="0", replacement:="NA", LookAt:=xlWhole
If WorksheetFunction.CountIf(.Cells, "NA") > 0 Then
.AutoFilter field:=1, Criteria1:="NA"
.Resize(IIf(.Cells(1) = "NA", .Rows.count, .Rows.count - 1)).Offset(IIf(.Cells(1) = "NA", 0, 1)).SpecialCells(xlCellTypeVisible).Font.ColorIndex = 3
.Parent.AutoFilterMode = False
End If
End With
End Sub
edit编辑
Now as you have provided more info, this can be done like:现在,由于您提供了更多信息,可以这样做:
Try this尝试这个
Sub ConvertToValues()
Dim R As Long
k = Sheet1.Range("A1048576").End(xlUp).Row '-> total rows in column A
For R = 1 To k
If IsEmpty(Sheet1.Cells(R, 2)) = True Or Sheet1.Cells(R, 2) = "#NA" Or Sheet1.Cells(R, 2) = "0" Then
Sheet1.Cells(R, 2).Value = "NA"
Sheet1.Cells(R, 2).Font.Color = RGB(255, 0, 0)
End If
Next R
End Sub
i'm beginner too , this what can i make and maybe it will help you , you can just put number of cells you want to change or rewrite the code with FOR EACH我也是初学者,这我能做什么,也许它会帮助你,你可以输入你想要更改的单元格数量或使用 FOR EACH 重写代码
Dim i As Integer
On Error Resume Next
For i = 1 To 20
cells.Find(What:="0", MatchCase:=False_, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "NA"
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
cells.Find(What:="#N/A", MatchCase:=False_, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "NA"
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
Next i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.