如何获得Excel以搜索和更改特定列中的单元格信息?

时间:2016-07-18 18:07:12

标签: excel excel-vba macros vba

我需要将特定列中的所有数字更改为文本和我目前使用的宏,将整个数据库中的所有数字更改为字母,而不是仅更改指定的列H. 这是我目前正在使用的宏。我应该在它的开头添加什么来指定仅在H列中搜索?如果可能的话,我想保留我的大部分宏。 谢谢!

    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

3 个答案:

答案 0 :(得分:1)

您需要更好地定义范围。尽可能避免使用SelectActivate。如果您不需要,请不要搜索超过100万行。确定列中所需的最后一行,并限制要搜索的行数。

例如:

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

答案 1 :(得分:1)

您可以指定要替换的范围而不选择它:

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

答案 2 :(得分:0)

这是我上面创建的用户Slai 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

感谢大家的帮助!