简体   繁体   English

Excel VBA-替换值

[英]Excel VBA - Replacing Value

I want to replace the numbers 91, 92, 93, 94, 99 with 91H, 92H, 93H, 94H, 99H. 我想用91H,92H,93H,94H,99H替换数字91、92、93、94、99。 But every time I run the macro a second time, the macro adds another H to the number and it looks like 91H H for example. 但是,每次我第二次运行宏时,宏都会在数字中添加另一个H ,例如,看起来像91HH。 I can't find a solution for this. 我找不到解决方案。 I don't want that the macro adds another H to the number. 我不希望宏在数字中添加另一个H。 I want that the macro only adds a H to the new values, not to the ones that have already been changed. 我希望宏仅将H添加到新值,而不添加到已经更改的值。

Columns("D").Replace What:="92", _
                        Replacement:="92H", _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        MatchCase:=False, _
                        SearchFormat:=False, _
                        ReplaceFormat:=False
Columns("D").Replace What:="93", _
                        Replacement:="93H", _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        MatchCase:=False, _
                        SearchFormat:=False, _
                        ReplaceFormat:=False

Columns("D").Replace What:="91", _
                        Replacement:="91H", _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        MatchCase:=False, _
                        SearchFormat:=False, _
                        ReplaceFormat:=False

Columns("D").Replace What:="94", _
                        Replacement:="94H", _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        MatchCase:=False, _
                        SearchFormat:=False, _
                        ReplaceFormat:=False

Columns("D").Replace What:="99", _
                        Replacement:="99H", _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        MatchCase:=False, _
                        SearchFormat:=False, _
                        ReplaceFormat:=False

I didn't post the whole VBA code because it's a little bit long. 我没有发布整个VBA代码,因为它有点长。

I think of a workaround. 我认为有一种解决方法。 Add codes before the macro to replace 91H, 92H, 93H, 94H, 99H with 91, 92, 93, 94, 99. What the new macro would do is: 在宏之前添加代码,用91、92、93、94、99替换91H,92H,93H,94H,99H。新宏将执行以下操作:
A. "91H": remove "H" and add "H", ie do nothing 答:“ 91H”:删除“ H”并添加“ H”,即不执行任何操作
B. "91": dont fit into first part, "H" will be added in 2nd part B.“ 91”:不适合第一部分,“ H”将添加到第二部分

This should work for any text seen. 这应该适用于所看到的任何文本。

In general, you are repeating lots of code. 通常,您在重复很多代码。 This should be avoided. 应该避免这种情况。 A way to do it is like this: 一种方法是这样的:

Option Explicit

Sub ReplaceSomething(columnName As String, lookFor As String, replaceWith As String)

    Dim rngCol      As Range
    Dim rngCells    As Range

    If WorksheetFunction.CountA(Columns(columnName)) = 0 Then Exit Sub
    Set rngCol = Columns(columnName).SpecialCells(2)
    If rngCol Is Nothing Then Exit Sub

    For Each rngCells In rngCol
        If rngCells = lookFor Then
            rngCells = replaceWith
        End If
    Next rngCells

End Sub

Sub TestMe()

    ReplaceSomething "A", "H", "H1"
    ReplaceSomething "B", "H", "H1"
    ReplaceSomething "D", "H", "H1"

End Sub

Thus you would be giving the Column Name as an argument of the function ReplaceSomething and it will run for columns A, B and C. 因此,您可以将列名作为函数ReplaceSomething的参数,它将在列A,B和C中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM