查找和替换动态值

时间:2012-08-17 13:19:56

标签: excel vba dynamic replace

我正在尝试在工作表中搜索某个值,在本例中为“ALAE”。找到此实例后,我需要向下并用另一个工作表上找到的参考值替换所有后续字段。

例如,该字段将“ALAE”作为列标题,然后在该字段下方有两个“2”。我需要转到参考表,查找2表示的含义,并用文本版本替换该值。 “ALAE”的位置将始终改变,标题下方的字段数也将改变。我需要在每次运行宏时动态执行此操作。

目前,代码将替换第一个“2”而不是第二个。

这是我到目前为止的代码

Sub Reference()

Dim macroSheet As Worksheet
Dim LastRow As Long
Dim strSearch As String
Dim aCell As Range, bCell As Range
Dim x As Integer

Set macroSheet = Sheets("Treaty Year Preview")
With macroSheet
     LastRow = .Range("A" & Rows.Count).End(xlUp).Row
     strSearch = "ALAE"
     Set aCell = .Range("A1:R" & LastRow).Find(What:=strSearch, LookIn:=xlValues, _
                                                        Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                                        MatchCase:=True, SearchFormat:=False)
     If Not aCell Is Nothing Then
        x = 1
        Set bCell = aCell
         Do
            aCell.Offset(x) = Application.WorksheetFunction.VLookup(aCell.Offset(x), Worksheets("ALAE ULAE").Range("A:B"), 2)
            x = x + 1

        Loop Until aCell.Offset(x) Is Nothing

    End If 'If Not aCell is Nothing Then
End With 'With macroSheet
End Sub

1 个答案:

答案 0 :(得分:0)

试试这段代码。我认为它以更简单的方式提供您想要的东西。我根据你在帖子中看到的内容做了最好的,但如果有什么遗漏或错误,只需评论,如果需要我会调整。

代码被大量评论,以解释我的更改。 :)

Sub Reference() 'given your post explanation and what I see, there is no need to pass a variable into the sub

    Dim macroSheet As Worksheet
    Dim lastRow As Long
    Dim strSearch As String
    Dim aCell As Range, bCell as Range
    ' the rest of the variables I removed because they were superfolous (yuck - bad spelling, i know!)


    Set macroSheet = Sheets("Treaty Year Preview")

    With macroSheet 'now we are working with the macroSheet, no need to reference it any more

        lastRow = .Range("A" & Rows.Count).End(xlUp).Row

        strSearch = "ALAE"

        Set aCell = .Range("A1:A" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _
                                                            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                                            MatchCase:=True, SearchFormat:=False)

        If Not aCell Is Nothing Then

            Set bCell = aCell

            Do

                'basically find the value 1 below the found cell and replace it with the value in the lookup table
                'I wasn't sure how your ALAE lookup table is organized, but you may need to change the ALAE!B:C reference to fit your needs.
                'i changed to vlookup because I am more familiar with that than lookup function
                aCell.Offset(1) = .Application.WorksheetFunction.VLookup(aCell.Offset(1), "ALAE!B:C", 2, False)

                'reset aCell to find the next one
                Set aCell = .Range("A1:A" & lastRow).Find(What:=strSearch, After:=aCell, LookIn:=xlValues, _
                                                            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                                            MatchCase:=True, SearchFormat:=False)

            Loop Until aCell Is Nothing or aCell.Address = bCell.Address 'it will keep looping unless aCell returns nothing

        End If 'If Not aCell is Nothing Then

    End With 'With macroSheet

End Sub