Consulting

Results 1 to 3 of 3

Thread: Unable to Control Cursor at End of Script

  1. #1

    Unable to Control Cursor at End of Script

    I have a script for keeping passwords. The script works with a few anomalies that I would like to address. The first issue is that the cursor does not return to the "user data entry cell" that I have denoted in the script. At the end of the script, it moves down one cell as if I had used the <enter> key. I have tried a few things (e.g. Selecting the cell [c3] that I want the cursor to go to and offsetting the cursor as the last step of the script) that have not worked. The cursor always goes to [c4]. Here is the code:
    Sub FindPSWD()
    '  This sub finds the password associated with the code entered by the user
    On Error GoTo MyErrorHandler:
    Dim PSWD_Code As String
    '  Identify the cell that the Password Code is to be entered
    PSWD_Code = "c3"
    If Len(PSWD_Code) > 0 Then
    pswd = Application.WorksheetFunction.VLookup(PSWD_Code, Sheet1.Range("F2:G10000"), 2, False)
    ' Data is  stored in columns F and GElse
    MsgBox ("Enter the password code")
    End If
    Range("d3").Select
    
    ActiveCell.Offset(-1, 0).Select
    ActiveCell.Value = pswd
    ' Range("c3").Select
    End Sub
    I would appreciate any suggestions. I am using Excel 2016 with Windows 10 Pro
    Last edited by Paul_Hossler; 10-17-2018 at 12:51 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    1. I added CODE tags to your macro - you can use the [#] to insert CODE ... /CODE tags

    2. Putting On Error at the front of the macro hides things; I only bracket statement(s) that could throw an error (e.g. VLookup)

    3. Not 100% sure what you're trying to do, but try this

    Option Explicit
    
    Sub FindPSWD()
    
        ' This sub finds the password associated with the code entered by the user
        Dim PSWD_Code As String, pswd As String
    
        ' Identify the cell that the Password Code is to be entered
        PSWD_Code = "c3"
    
        If Len(Range(PSWD_Code).Value) > 0 Then
            On Error GoTo MyErrorHandler
            pswd = Application.WorksheetFunction.VLookup(Range(PSWD_Code).Value, Sheet1.Range("F2:G10000"), 2, False)
            On Error GoTo 0
        
            MsgBox pswd
        
        Else
            MsgBox ("Enter the password code")
        End If
        Application.Goto Range(PSWD_Code)
        MsgBox ActiveCell.Address
        
        
        Exit Sub
    
    MyErrorHandler:
        MsgBox "Error"
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Thank you for your reply Paul. I am embarrassed to say that I submitted a thread on this over a year ago titled: Trouble Selecting Range that was answered with a solution that worked fine. In that solution and Event was used: "Private Sub Worksheet_Change(ByVal Target As Range)" which made the difference.

    Thank you again. While this was an important project for me, life got in the way and it had to become a "when I have some time" project and it prompted got forgotten. My apologies again for wasting your time.

Tags for this Thread

Posting Permissions

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