0

I am trying to search a sheet for a certain value, in this case "ALAE". After finding this instance, I then need to go down and replace all the subsequent fields with a reference value found on another worksheet.

For example, the field has "ALAE" as a column heading, then below that there are two "2"s. I need to go to the reference sheet, look up what 2 means, and replace the value with the text version. The location of "ALAE" will always change, as well the number of fields beneath the heading. I need to dynamically do this each time the macro is run.

Currently, the code will replace the first "2" but not the second.

here is the code i have so far

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
4

1 回答 1

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
于 2012-08-17T13:54:37.797 回答