确定单元格值以在另一个工作表中查找相同的值

时间:2016-11-12 15:44:44

标签: vba excel-vba excel

我有一张包含所有商店位置和电子邮件模板的工作表。在电子邮件模板表中,vlookup结果将显示在G25中。我需要G25中的值(例如New Barrie)在另一个表(All Locations)中找到相同的值" New Barrie"。

我不知道如何在G25中寻找价值,而不是硬编码的新巴里。

Sub Email()

' Email Macro

Range("G25").Select

Selection.Copy

Sheets("All Locations").Select

Cells.Find(What:="New Barrie", After:=ActiveCell, LookIn:= _

    xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _

    xlNext, MatchCase:=False, SearchFormat:=False).Activate

Range("A37").Select

Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True

Sheets("Email Template").Select

End Sub

1 个答案:

答案 0 :(得分:0)

我相信你会追随以下事情:

Option Explicit

Sub Email()
    Dim val As String
    Dim myCell As Range

    val = Worksheets("template").Range("G25").Value '<--| store worksheet "template" cell "G25" value into a string variable
    Set myCell = Worksheets("All Locations").Cells.Find(What:=val, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False) '<--| set myCell to the one in "All Locations" worksheet whose content matches the stored value

    If Not myCell Is Nothing Then myCell.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub

只需审核LookIn方法的LookAtMatchCaseFind()参数,以确保它们确实符合您的需求