简体   繁体   English

如何查找特定单元格并替换为文本

[英]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.

相关问题 如何使用格式化文本查找和替换单元格的一部分 - How to find and replace part of a cell with formatted text 查找单元格中的特定文本 - Find specific text in a cell 如何查找和替换完全匹配的文本但不匹配整个单元格内容 - How to find and replace exact match text but not match entire cell contents 查找替换vbscript excel特定单元格 - find replace vbscript excel specific cell 查找特定的文本,但不设置特定的单元格引用 - Find specific text but not set a specific cell reference VBA - 替换为在特定单元格中查找一个词有效,我如何找到一个词或另一个词? - VBA - Replace with find one word in a specific cell works, how do I find one word or another? 如果相邻单元格包含特定文本,则查找并突出显示包含文本的单元格 - Find and Highlight Cell Containing Text IF Adjacent Cell Contains Specific Text 查找和替换替代方案? 而不是“REPLACE”,只需将文本添加到现有单元格? - Find and Replace alternative? Instead of “REPLACE”, simply add text to existing cell? 在 PowerPoint 中查找文本并替换为 Excel 中单元格中的文本 - Find text in PowerPoint and Replace with text from a cell in Excel 查找特定文本并返回单元格以更改单元格的颜色 - Find specific text and return cell changing the cell's color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM