Search code examples
excelvba

Find Text - scroll the cell to the top left of the sheet excel


I want to find specific text and then scroll the cell that contains that text to the top left of the sheet.

Sub test2()
    Columns("A:A").Select
    Selection.Find(What:="User - User Full Name", After:=ActiveCell, LookIn:= _
        xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
' Name First Cell in Header

    Dim Tab2 As String
    Tab2 = ActiveSheet.Name & "Home"
    ActiveWorkbook.Names.Add Name:=Tab2, RefersToR1C1:="=Home!R22C1"
    Application.Goto Reference:=ActiveSheet.Range(Tab2), Scroll:=True
End Sub

It finds the text ok It hangs up on the line that tries to name the cell

Thanks in advance


Solution

  • Use this line:

    Application.Goto Reference:=ActiveCell, Scroll:=True
    

    instead of:

    Dim Tab2 As String
    Tab2 = ActiveSheet.Name & "Home"
    ActiveWorkbook.Names.Add Name:=Tab2, RefersToR1C1:="=Home!R22C1"
    Application.Goto Reference:=ActiveSheet.Range(Tab2), Scroll:=True  
    

    If you need to give a name to a cell, give an example, because this code is difficult to understand.