Consulting

Results 1 to 3 of 3

Thread: How to return to the original active cell?

  1. #1
    VBAX Newbie
    Joined
    Mar 2022
    Location
    United States
    Posts
    1
    Location

    Question How to return to the original active cell?

    Say I have cell B5 selected. I'm creating VBA code that copies data from a cell above B5 and then also copies data from a cell to the right of B5. The issue I'm having is telling the code to go back to B5 after I've copied the cell above it so that I can then copy the cell to the right.

    I'm not sure if I'm using the wrong syntax or if I'm setting up my variables incorrectly, but the last line of code below is what gives me an error, and I can't figure out how to fix it:

    Sub PQ_Resolver()
    '
    ' PQ_Resolver Macro
    '
    ' Keyboard Shortcut: Ctrl+e
    '
        Dim R As Integer
        Dim C As Integer
        
        R = ActiveCell.Row
        C = ActiveCell.Column
        
        ActiveCell.End(xlUp).Select
        ActiveCell.Offset(1).Select
        Selection.Copy
        Range("W1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Cells(R & C).Select
    My thought is to take the row and column of the active cell (B5) and then use those to reselect that cell later on before moving on in the code, but as stated above it's not working.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Quote Originally Posted by Saunick View Post
    How to return to the original active cell?
    The best way is not to have to! Don't change what cell is the active cell and what's selected (they aren't always the same thing).
    This does the same thing:
    Sub PQ_Resolver()
    '
    ' PQ_Resolver Macro
    '
    ' Keyboard Shortcut: Ctrl+e
    '
    Range("W1").Value = ActiveCell.End(xlUp).Offset(1).Value
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    p45cal is correct.

    Also don't use Integer Types unless you are working on an olde DOS machine with very little RAM. Integers will fail on numbers > 32K.

    In your code, Cells(R&C) = Range("AZ1"), if it works at all: You need Cells(R, C)

    On the very rare occasions I must use "ActiveCell" The very first thing do is set a Variable to that Range.. I would refactor your code like
    Dim myRange as Range
    
    Sub BlahBlah()
    Set myRange = Activecell
    
    Range("W1") = myRange.End(xlUp).Offset(1, 0).Value
    End Sub
    
    Sub BlehBleh()
    With myRange ...
    More code here
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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