+ Reply to Thread
Results 1 to 3 of 3

Using Target.Address after protecting sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    01-28-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    5

    Using Target.Address after protecting sheet

    I am having issues using either Target.Select or Range(Target.Address) in my code. I have a code that after double clicking a cell, unprotects the sheet, and displays an inputbox for the user to enter an amount for a manual adjustment. This adjustment is pasted into another cell. Then, I protect the sheet, and I want to select the original cell that was double-clicked.

    This works fine if I don't reprotect the sheet at the end, but if I do reprotect (even with .EnableSelection = xlUnlockedCells and .EnableSelection = xlLockedCells) the macro ends with the cell where the adjustment was made selected. This is extremely frustrating to me.

    Just to clarify, what I want this code to to is this: if $I$9 is double-clicked, unprotect sheet, allow user to input amount, then paste that amount into $I$62, then reprotect sheet, and select cell $I$9.

    Here is my code:

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)


    ActiveSheet.Unprotect

    If Not Intersect(Target, Range("$A$3:$ZZ$51")) Is Nothing Then


    Amount = InputBox("Enter amount:", "Amount", Target.Offset(53, 0).Value)

    If StrPtr(Amount) = 0 Then
    Application.SendKeys ("{ESC}")
    ActiveSheet.Protect
    Range(Target.Address).Select
    Exit Sub
    End If

    If Cancel = True Then
    Application.SendKeys ("{ESC}")
    ActiveSheet.Protect
    Range(Target.Address).Select
    Exit Sub
    Else
    Target.Offset(53, 0) = Amount
    End If

    Application.SendKeys ("{ESC}")

    End If

    ActiveSheet.Protect
    Range(Target.Address).Select

    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Using Target.Address after protecting sheet

    Can you please add code tags? Makes it a lot easier to read the code.

    Do you actually need to select the cell?

    If you want to exit edit mode then use this instead of SendKeys.
    Cancel = True
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    01-28-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Using Target.Address after protecting sheet

    I changed the SendKeys to Cancel = True and it seems to be working now. Sorry about the code tags, but I am new, so I didn't know. I will add them to future posts.

    Thanks for the help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1