如何获得excel来search和更改特定列中的单元格信息?

我需要将特定列中的所有数字更改为文本以及此刻正在使用的macros,将整个数据库中的所有数字更改为字母,而不仅仅是指定的列H.以下是我正在使用的macros。 我应该在它的开头添加什么来指定仅在H列中进行search? 如果可能,我想保留大部分macros。 谢谢!

Sub EOD_Process_Part_2() [![From and To][1]][1] '----Changes all Numbers to initials---- Columns("H3:H").Select Application.ReplaceFormat.Clear Selection.Replace what:="1", Replacement:="AM", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _ ReplaceFormat:=False Selection.Replace what:="2", Replacement:="B", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _ ReplaceFormat:=False Selection.Replace what:="3", Replacement:="BM", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _ ReplaceFormat:=False Selection.Replace what:="4", Replacement:="BS", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _ ReplaceFormat:=False '--------------- adds missing and no initials------------- Columns("H:H").Select Range("H2").Activate With Application.ReplaceFormat.Interior .PatternColorIndex = xlAutomatic .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With Selection.Replace what:="", Replacement:="MISSING", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _ ReplaceFormat:=True With Application.ReplaceFormat.Interior .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 .PatternTintAndShade = 0 End With With Application.ReplaceFormat.Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.399945066682943 .PatternTintAndShade = 0 End With Selection.Replace what:="-", Replacement:="?", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _ ReplaceFormat:=True Range("H2").Select Selection.AutoFilter Columns("H:H").ColumnWidth = 11.29 Range("L7").Select 

您可以指定要replace的范围而不select它:

 Dim columnH As Range ' to specify that the variable columnH is of type Range Set columnH = ActiveCell.Worksheet.UsedRange.Columns("H") Application.ReplaceFormat.Clear columnH.Replace "1", "AM", LookAt:=xlWhole, SearchFormat:=False, ReplaceFormat:=False columnH.Replace "2", "B" columnH.Replace "3", "BM" columnH.Replace "4", "BS" Application.ReplaceFormat.Interior.Color = XlRgbColor.rgbYellow columnH.Replace "" , "MISSING", ReplaceFormat:=True Application.ReplaceFormat.Interior.Color = XlRgbColor.rgbLightBlue columnH.Replace "-", "?" 

另一种方法可以是逐个检查单元格

 Dim cell As Range, columnH As Range Set columnH = ActiveCell.Worksheet.UsedRange.Columns("H") For Each cell in columnH.Cells Select CStr(cell.Value) Case "1" : cell.Value = "AM" Case "2" : cell.Value = "B" Case "3" : cell.Value = "BM" Case "4" : cell.Value = "BS" Case "" : cell.Value = "MISSING" cell.Interior.Color = rgbYellow Case "-" : cell.Value = "?" cell.Interior.ThemeColor = xlThemeColorLight2 cell.Interior.TintAndShade = 0.399945066682943 End Select Next 

你需要更好地定义你的范围。 避免Select并尽可能Activate 。 如果不需要,不要search超过100万行。 确定列中所需的最后一行,并限制要search的行数。

例如:

 Dim ws1 as Worksheet Set ws1 = ThisWorkbook.Sheets("Sheet 1") LastRow = ws1.Cells(Rows.Count, 8).End(Xlup).Row Set SRng = ws1.Range(ws1.Cells(3,8),ws1.Cells(LastRow, 8)) With SRng 'Add in all your search and format stuff End With 

这是我上面创build的用户Slai the Macros master的最终结果。 感谢Slai!

 Sub EOD_Process_Part_2() '-------Changes assigned numbers to right initials------- Application.ReplaceFormat.Clear With ActiveCell.Worksheet.UsedRange.Range("H:H") .Replace "1", "AM", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False .Replace "2", "B" .Replace "3", "BM" .Replace "4", "BS" '-------Changes Blanks to MISSING with yellow background------- With Application.ReplaceFormat.Interior .PatternColorIndex = xlAutomatic .Color = XlRgbColor.rgbYellow .TintAndShade = 0 .PatternTintAndShade = 0 End With .Replace "", "MISSING", ReplaceFormat:=True '-------Changes NO INITIAL to NO INITIAL with green background------- With Application.ReplaceFormat.Interior .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 .PatternTintAndShade = 0 End With .Replace "NO INITIAL", "NO INITIAL", ReplaceFormat:=True '-------Changes - to ? with light blue background------- With Application.ReplaceFormat.Interior .PatternColorIndex = xlAutomatic .Color = 14260581 .TintAndShade = 0 .PatternTintAndShade = 0 End With .Replace "-", "?", ReplaceFormat:=True '-------Centers Column H and adjusts the column's width------- Range("H2").Select Selection.AutoFilter Columns("H:H").ColumnWidth = 11.29 Range("L7").Select End With End Sub 

感谢大家的帮助!