【问题标题】:Find and Replace dynamic values查找和替换动态值
【发布时间】:2012-08-13 22:50:11
【问题描述】:

我正在尝试在工作表中搜索某个值,在本例中为“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

【问题讨论】:

  • 我看到的第一件事是Set getRow = ActiveCell.Row 因为整数变量不是对象,所以您不需要设置它。只需写getRow = replaceCell.Row。此外,lookup = Selection.Value 谁知道您当前的选择是什么,因为您从未告诉宏选择任何内容。 Find 可能会以这种方式工作(我忘记了),但你最好写 lookup = aCell.Valuelookup = replaceCell.Value 不确定你想要哪个。除非绝对必要,否则最好避免使用 ActiveCellSelection。他们经常欺骗你!
  • 非常感谢您的快速回复和有用的信息,斯科特!你不知道我当前的选择是什么,这行是否: Set replaceCell = aCell.Offset(1, 0).Select 使它被选中?
  • 我刚刚编辑了我的 cmets。请再读一遍。另外,让我们知道这是否解决了它! (可能不会)
  • 是的。之后我注意到了。我即将回复一个可以清理您的代码并为您提供所需的答案。我会评论它来解释。
  • 我现在在查找函数中遇到语法错误:lookup = macroSheet.Application.WorksheetFunction.Lookup(lookup, ALAE!C[getRow], ALAE!B[getRow]) 并且在子标题中传递的变量不正确:\

标签: excel vba dynamic replace


【解决方案1】:

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

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

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

【讨论】:

  • 非常感谢!出现“无法获取 WorksheetFunction 类的 Vlookup 属性”的错误。参考表设置为没有标题,A 列是所需值(“N/A”),B 是参考编号( “2”)。一开始情况并非如此,因此我将代码移至 Vlookup 行中的 A:B 而不是 B:C。我应该先切换订单以获取参考号吗?另外,假设数字是 3 或 7 或 14。它将如何匹配参考值?我的目标是提取参考号并将该值用作行号(充当指针)
  • 1) 要了解有关 vLookup 工作原理的更多信息,请阅读 Excel 帮助或 MSDN 文章 support.microsoft.com/kb/181213? 中的相关信息。 2) My goal was to extract the reference number and use that value as a row number (acts as a pointer) 我不明白你的意思。我以为你想(来自你的 OP) replace the number with the value found in the reference table,所以找到 ALEA 行下的值(比如 #N/A)并将其替换为 2,这是 #N/A 的匹配项参考表。 3) 如果仍然太复杂,请在您的 OP 中放置一个指向虚拟数据集的链接。
  • 我附上了一个虚拟文件,条约年度预览下的 Q11 是感兴趣的区域,但是这个单元格的位置会改变。对于上面的第 2 个项目符号,您所说的是正确的,但值不必总是 2。在这种情况下可能是 1-5。
  • 抱歉,我无法在工作中访问此文件。如果我记得,会尝试在家里下载并发送给自己。否则,您可以发布数据的屏幕截图。我想这就是我需要看到的。
  • 我对代码做了一些调整,现在它可以无限期地运行了。我只是得到沙漏,直到我击中逃跑。我用新代码更新了 OP,但由于我没有足够的声誉而无法发布图片
猜你喜欢
  • 1970-01-01
  • 2012-02-12
  • 2020-06-24
  • 2020-07-14
  • 2012-09-04
  • 1970-01-01
  • 2016-12-05
  • 2016-10-31
  • 1970-01-01
相关资源
最近更新 更多