Consulting

Results 1 to 3 of 3

Thread: vba - help removing selection

  1. #1
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location

    vba - help removing selection

    Hi Team,
    The below codes formula work for me, however in each sheet column B is getting selected
    I want range ("a1") to get selected by default.

    I tried application.cutcopymode = false but no luck. plz assist. Thanks.

    Sub Test()
    Dim ws As Worksheet, lr As Long
    For Each ws In Worksheets
        Select Case ws.Name
            Case "Sheet1", "Sheet2", "Sheet5"
                lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
                ws.Range("B2:B" & lr).Formula = "=VLOOKUP(A2,data,2,FALSE)"
        End Select
    Next ws
    
    End Sub
    Last edited by Simon Lloyd; 11-05-2017 at 09:12 PM. Reason: Added code tags

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    The code doesn't "Select" anything! it simply puts a formula in column B for the range you specify, if you put that formula in column a you will have a circular reference.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Hello
    Please put the codes between tags to appear properly
    As for your code, it is working well and there is no relationship between selecting B2 and the code
    But if you need to do that and select A1 you can use the code like that
    Sub Test()
        Dim ws      As Worksheet
        Dim lr      As Long
    
    
        For Each ws In Worksheets
            Select Case ws.Name
                Case "Sheet1", "Sheet2", "Sheet5"
                    ws.Activate
                    lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
                    ws.Range("B2:B" & lr).Formula = "=VLOOKUP(A2,data,2,FALSE)"
                    Application.Goto ws.Range("A1")
            End Select
        Next ws
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •