Results 1 to 30 of 30

Thread: Excel- enter/tab choosing locked cell on sheet

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Excel- enter/tab choosing locked cell on sheet

    See that 'case 13 or 9, offset' cell piece?
    Code:
    Private Sub TempCombo_KeyDown(ByVal _
            KeyCode As MSForms.ReturnInteger, _
            ByVal Shift As Integer)
            If KeyCode = vbKeyF And Shift = 2 Then PrintARX1
            If KeyCode = vbKeyR And Shift = 2 Then ClearForm
        Select Case KeyCode
            Case 9
                ActiveCell.Offset(0, 1).Activate
            Case 13
                ActiveCell.Offset(1, 0).Activate
            Case Else
        End Select
    End Sub
    In the sheet this is used, that offset (9 or 13 either one) will select a cell that is "locked" when I use tab or enter if a locked cell is in the way of the direction specified above. It will only select 1, and then continue on to the next appropriate 'unlocked' cell afterwards. But the FIRST time it can go down or over to a 'locked' cell from the unlocked cells, it will.
    Is there a piece of code necessary to force it to always skip 'locked' cells on the sheet, even though those particular cells are set as 'locked' in the format? Something like instead of 'activecell.offset' it should be 'activeUNLOCKEDcell.offset' or something else?
    Thanks.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    there may well be a better method, but try like this
    vb Code:
    1. Select Case KeyCode
    2.         Case 9
    3.             do
    4.                ActiveCell.Offset(0, 1).Activate
    5.             loop until activecell.locked = false
    6.         Case Else
    7.     End Select

    edit i just noticed that each key was moving in different direction, so make similar loop for enterkey
    Last edited by westconn1; Jan 18th, 2011 at 05:46 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    This partially works. It does not select the locked cell like it used to and stay there, so that's good. It does continue in that same column or row forever though if an unlocked cell isn't in it's path. So if I have 2 columns unlocked, and it hit tab on column 2, it loops forever in the same row > column 2 only. Same for rows going down and enter, it goes 'down rows' forever beyond my last row.

    It's more complicated b/c my rows and columns don't match up perfectly, at least in the order I wish to select them with 'enter' or 'tab'.

    Is it possible to do an 'if/then' type of statement where it doesn't loop but checks the cell, and if locked moves down rows until it finds the left-most available unlocked cell in the next row? That's the gist of what I'm looking for in either key function. That sounds complicated to me though...

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    That sounds complicated to me though...
    not really
    vb Code:
    1. case 9
    2. x = 3 ' number of columns to search for unlocked cell
    3. for col = activecell.column to activecolumn.column + x
    4.     for r = activecell.row to rows.count    ' change rows.count to bottom most cell you want to check in grid
    5.         if not cells(r, col).locked then cells(r, col).activate:  fnd = true: exit for
    6.     next
    7.    if fnd then exit for
    8. next
    i did not test this, so allow for typos
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    Ehh... You kinda lost me here. It gives errors and says variables aren't defined.
    It doesn't like x, col, activecolumn (and others?) and says they are undefined variables. I do not know how to set them. Not knowing what it means, if I put "dim x as string" it gets past that one then to the next:
    Code:
    Private Sub TempCombo_KeyDown(ByVal _
            KeyCode As MSForms.ReturnInteger, _
            ByVal Shift As Integer)
            'Dim x As String
            'Dim col As String
            If KeyCode = vbKeyF And Shift = 2 Then PrintARX1
            If KeyCode = vbKeyR And Shift = 2 Then ClearForm
        Select Case KeyCode
            Case 9
            x = 4 ' number of columns to search for unlocked cell
            For col = ActiveCell.Column To activecolumn.Column + x
            For r = ActiveCell.Row To Rows.Count ' change rows.count to bottom most cell you want to check in grid
            If Not Cells(r, col).Locked Then Cells(r, col).Activate:  fnd = True: Exit For
            Next
            If fnd Then Exit For
            Next
                'ActiveCell.Offset(0, 1).Activate
            Case 13
                ActiveCell.Offset(1, 0).Activate
            Case Else
        End Select
    End Sub
    Either way I'm lost on this one. I think some things were assumed I put them in there, but I don't know what...

    and should rows.count be replaced entirely with a cell # or actual #? Like "E25" or "25"?

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    It gives errors and says variables aren't defined.
    you have option explicit at the top of your code pane, so all variables must be defined

    dim x as integer, r as long
    if you do not know what type to declare as leave out the as part and it will be a variant, until you can figure something better
    and should rows.count be replaced entirely with a cell # or actual #? Like "E25" or "25"?
    you are just looking for a row number, rows.count returns 65535 up to excel 2003, later versions have more rows in each sheet

    also you can test all cells in a column (or part of) for locked
    vb Code:
    1. if not range(activecell, cells(somerow, activecell.column)).locked  ' all cells unlocked
    2. if isnull(range(activecell, cells(somerow, activecell.column)).locked)   ' mixed, some cells unlocked
    3. ' can be combined
    4. if isnull(range("b1:b16").Locked) or not range("b1:b16").Locked then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    I could not get your code to work using 'tab' or 'enter' to get to the correct cells and not choose locked cells. It doesn't give an error, but the 'tab' function still selects a 'locked' cell that contains a function at this point:
    Code:
    Private Sub TempCombo_KeyDown(ByVal _
            KeyCode As MSForms.ReturnInteger, _
            ByVal Shift As Integer)
            Dim x As Integer
            Dim r As Long
            Dim col
            Dim fnd
            If KeyCode = vbKeyF And Shift = 2 Then PrintARX1
            If KeyCode = vbKeyR And Shift = 2 Then ClearForm
        Select Case KeyCode
            Case 9
            x = 4 ' number of columns to search for unlocked cell
            For col = ActiveCell.Column To ActiveCell.Column + x
            For r = ActiveCell.Row To Rows.Count ' change rows.count to bottom most cell you want to check in grid
            If Not Cells(r, col).Locked Then Cells(r, col).Activate:  fnd = True: Exit For
            Next
            If fnd Then Exit For
            Next
                ActiveCell.Offset(0, 1).Activate
            Case 13
            'If ActiveCell = H3 And KeyCode = Enter Then GoTo D12
            'Else: ActiveCell.Offset(1, 0).Activate
            ActiveCell.Offset(1, 0).Activate
            Case Else
        End Select
    End Sub
    I'm wondering if I can't resolve this problem in a different way? There are a limited selection of cells that perform inappropriately. Would it be easier to say (as I tried) "if you are in cell H3 and you hit enter, then go to cell D12"? I don't know how that syntax must be written either, but it seems like it might be easier to have 6 or so instances of that type of code.

    I cannot get the 'case 9' portion to work like we wish it to. Where am I going wrong? Thanks.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    i tested the code for case 9 works correctly for locked cells, as long as there is some unlocked cells, within the searched area, remove the last line as that will move the selection from the first unlocked cell, to one in the next column that may be locked

    when you refer to cells that are locked, please clarify
    the default in my workbook is all cells are locked unless specifically unlocked, either manually or by code, this does not prevent values from being changed unless the worksheet is protected

    the alternative method you suggest can be worked, probably by using a second select case for each case, the second select would be activecell.address, based on the address you can activate some other cell
    vb Code:
    1. case "$H$3": range("d12").select  ' or (activate)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    OK, to clarify: the sheet is protected and the particular cells are specifically unlocked that I'm referring to try to switch between.

    I removed the last line as you mentioned and that helped. These two key functions do not select locked cells anymore, so that's good! But it changed the key function a bit. Now, the cells w/ the combobox's in them act differently. When I use tab or enter in the combobox cells, it "re-selects" the cell before it moves on.

    example- before this code in cell D12, if I hit enter once, it went to D13. Now, when in D12 (w/ the combobox) I hit enter once, it selects the entire cell D12. Then if I hit enter again, it moves on to D13. This change does NOT happen in cells without the combobox. From H1 to H2 for instance, straight general format cells, you only have to hit enter once for the move.

    Also, this code did not quite "wrap" like I was hoping. For instance I need to go from H3 to D12. 'Tab' will do this, but 'enter' will not, as has always been the case. 'Enter' goes to the 'G' column instead, as I guess it is more directly below 'H' than 'D' is in columns. Is there anything to change this?

    Here is the combobox code too w/ the key stuff, anything conspicuous to make it 'double-select' the combobox cells now on tab or enter keys?
    Code:
    Private Sub TempCombo_KeyDown(ByVal _
            KeyCode As MSForms.ReturnInteger, _
            ByVal Shift As Integer)
            Dim x As Integer
            Dim r As Long
            Dim col
            Dim fnd
            If KeyCode = vbKeyF And Shift = 2 Then PrintARX1
            If KeyCode = vbKeyR And Shift = 2 Then ClearForm
        Select Case KeyCode
            Case 9
            x = 4 ' number of columns to search for unlocked cell
            For col = ActiveCell.Column To ActiveCell.Column + x
            For r = ActiveCell.Row To Rows.Count ' change rows.count to bottom most cell you want to check in grid
            If Not Cells(r, col).Locked Then Cells(r, col).Activate:  fnd = True: Exit For
            Next
            If fnd Then Exit For
            Next
                'ActiveCell.Offset(0, 1).Activate
            Case 13
            x = 4 ' number of columns to search for unlocked cell
            For col = ActiveCell.Column To ActiveCell.Column + x
            For r = ActiveCell.Row To Rows.Count ' change rows.count to bottom most cell you want to check in grid
            If Not Cells(r, col).Locked Then Cells(r, col).Activate:  fnd = True: Exit For
            Next
            If fnd Then Exit For
            Next
                'ActiveCell.Offset(1, 0).Activate
            Case Else
        End Select
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim str As String
    Dim cboTemp As OLEObject
    Dim ws As Worksheet
    Dim wsL As Worksheet
    Set ws = ActiveSheet
    Set wsL = Worksheets("Control")
      On Error GoTo errHandler
    
    'If Target.Count > 1 Then GoTo exitHandler-- why did this kill it for me?
    
      Set cboTemp = ws.OLEObjects("TempCombo")
        On Error Resume Next
      If cboTemp.Visible = True Then
        With cboTemp
          .Top = 10
          .Left = 10
          .ListFillRange = ""
          .LinkedCell = ""
          .Visible = False
          .Value = ""
        End With
      End If
    
      On Error GoTo errHandler
      If Target.Validation.Type = 3 Then
        Application.EnableEvents = False
        str = Target.Validation.Formula1
        str = Right(str, Len(str) - 1)
        If Left(str, 8) = "INDIRECT" Then
            str = Replace(Replace(str, "INDIRECT(", ""), ")", "")
            str = ws.Range(str).Value
        End If
        With cboTemp
          .Visible = True
          .Left = Target.Left
          .Top = Target.Top
          .Width = Target.Width + 15
          .Height = Target.Height + 5
          .ListFillRange = str
          .LinkedCell = Target.Address
        End With
        cboTemp.Activate
      End If
    
    exitHandler:
      Application.EnableEvents = True
      Application.ScreenUpdating = True
      Exit Sub
    errHandler:
      Resume exitHandler
    End Sub

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    for enter key you should change all rows to cols and cols to rows, so as to move down instead of across, or whatever

    the cell with the combobox may not be the activecell even when the combobox receives keystrokes
    Last edited by westconn1; Jan 27th, 2011 at 05:42 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    For some reason the enter key functions fine as-is, but I will switch the row-column on it.

    Is there any way around the activecell-combobox double-keystroke issue?

    If not, I will probably try to make it go from 1 cell to another based on keystroke instead.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    For some reason the enter key functions fine as-is, but I will switch the row-column on it.
    if that works correctly then maybe it is the tab one that needs changing?
    based on what you want to do, one should search by rows, the other by columns

    Is there any way around the activecell-combobox double-keystroke issue?
    i did not notice this when i was testing, but maybe i was using different combobox, or something, post a sample workbook that demonstrates the problem, zip before attaching
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    Here's the example. The issues:

    - need cell H3 to go to D12 on 'enter' key (it does on 'tab')
    - need column D to not tab to the locked G column cells (the way it is now w/ your code it does not, but you have to 'double-tab' to move 1 cell. Without your portion of code on the 'tab' key it will select the locked white cells in column G).
    - need cell D23 to go to E25 on 'enter' key (currently D23 goes to a locked-D24. Again, with your code it does not select D23 to D24, but it does not go to E25 either. It just cycles the D column up and down.)

    You'll notice currently in the file, the 'enter' key has the original code that just moves it down a cell. It takes only 1 keystroke to move a cell. The 'tab' portion has the different code on it, and although it does not select the 'G' column or any other locked cells, it does take a double-tab now.
    Thanks.
    Attached Files Attached Files

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    each time i try to test with your file it crashes my excel, took a while to figure why

    to avoid the double tab change the code to
    vb Code:
    1. Case 9
    2.         x = 7 ' number of columns to search for unlocked cell
    3.         For col = ActiveCell.Column To ActiveCell.Column + x
    4.         For r = ActiveCell.Row To 23  ' change rows.count to bottom most cell you want to check in grid
    5.         If Not Cells(r, col).Locked And Not Cells(r, col).Address = ActiveCell.Address Then Cells(r, col).Activate: fnd = True: Exit For
    6.         Next
    7.         If fnd Then Exit For
    8.         Next
    see if this is improvement

    maybe i need a diagram of where you want to go from each cell by tab or enter
    draw some arrows or something one colour for tab
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    The enter/tab now do not require a double-tab. I don't know how to modify this:
    Code:
    Case 9
            x = 10 ' number of columns to search for unlocked cell
            For col = ActiveCell.Column To ActiveCell.Column + x
            For r = ActiveCell.row To 25  ' change rows.count to bottom most cell you want to check in grid
            If Not Cells(r, col).Locked And Not Cells(r, col).Address = ActiveCell.Address Then Cells(r, col).Activate: fnd = True: Exit For
            Next
    to work for 'tab' though. It is tabbing down now instead of over and I cannot see why based on the above. I can't change it to work.

    As far as what function I still need to do what. I would take just one change. I would have 'enter' from cell H3 -> D12. Currently with 'enter' H3 -> G13, but I wish it would go to D12. In post#3 I mentioned about finding the 'left-most' cell on the 'enter' key going down. This is what I was referring to: the enter key going from H3 to D12 in the above file.

    I figure these must all run together for you. You're probably helping about 23 poor foolish souls like me with their fun work! Thank you for that...

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    try
    vb Code:
    1. case 9
    2.   if activecell.address = "$H$3" then range("d12").activate: exit sub
    3. x = 10 ' number of columns to search for unlocked cell
    4.         For r = ActiveCell.row To 25  ' change rows.count to bottom most cell you want to check in grid
    5.         For col = ActiveCell.Column To ActiveCell.Column + x        
    6.        If Not Cells(r, col).Locked And Not Cells(r, col).Address = ActiveCell.Address Then Cells(r, col).Activate: exit sub
    7.         Next
    8.       next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    Well, it didn't work. I don't know what to say . The 'enter' key doesn't use the H3 to-> D12 portion. And now the 'tab' is back to double-keystrokes while moving from the comboboxes. Check the actual file if you wish, thanks:
    Code:
    Select Case KeyCode
            Case 9
            x = 10 ' number of columns to search for unlocked cell
            For r = ActiveCell.row To 25  ' change rows.count to bottom most cell you want to check in grid
            For col = ActiveCell.Column To ActiveCell.Column + x
            If Not Cells(r, col).Locked And Not Cells(r, col).Address = ActiveCell.Address Then Cells(r, col).Activate: fnd = True: Exit For 'Exit Sub
            Next
            If fnd Then Exit For
            Next
                'ActiveCell.Offset(0, 1).Activate
            Case 13
            If ActiveCell.Address = "$H$3" Then Range("D12").Activate: Exit Sub
            x = 10 ' number of columns to search for unlocked cell
            For col = ActiveCell.Column To ActiveCell.Column + x
            For r = ActiveCell.row To 25  ' change rows.count to bottom most cell you want to check in grid
            If Not Cells(r, col).Locked And Not Cells(r, col).Address = ActiveCell.Address Then Cells(r, col).Activate: Exit Sub 'fnd = True: Exit For
            Next
            'If fnd Then Exit For
            Next
                'ActiveCell.Offset(1, 0).Activate
            Case Else
        End Select

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    as the combobox never goes to H3 the code for combobox keypress is never called you would need to add code in selectionchange event
    try this change
    vb Code:
    1. On Error GoTo errHandler
    2.  
    3. 'If Target.Count > 1 Then GoTo exitHandler-- why did this kill it for me?
    4. If Target.Address = "$H$4" Then Range("d12").Activate: Exit Sub
    5.   Set cboTemp = ws.OLEObjects("TempCombo")
    6.     On Error Resume Next

    the double tab effect is because cells in cols e and f are not locked so can be selected under the combobox
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    1- The addition of line #4 above did not change the action as desired. I tried to change 'target.address' to 'activecell.address' for fun to no avail.

    2- I cannot lock cols e and f since they are merged. So the tab will have to be double tab effect as is?

  20. #20
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    1- The addition of line #4 above did not change the action as desired. I tried to change 'target.address' to
    worked when i tested before
    it would also depend on your setting, which way the enter key moves the selection, mine is down, hence target of H4
    So the tab will have to be double tab effect as is?
    i am sure the would be some work around, try
    vb Code:
    1. For col = ActiveCell.Column To ActiveCell.Column + x step3
    Last edited by westconn1; Jan 29th, 2011 at 07:53 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    My enter moves down also. This was a hapenstance, but I found out: when the sheet is locked, H4 is not an option and the code doesn't work. When I unlock the sheet, the code works. Something necessary to put in for the difference of the locked sheet?

  22. #22
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    i have tested with the sheet (as downloaded) both locked and unlocked, from h3 i go to d12, in fact even if i click on h4 i go to d12
    the only other consideration is if different versions of excel work differently in this area

    on further consideration, the code i had suggested originally (before i had seen the workbook) may not be the best for this application, and continually trying to fix may not be the best solution

    apart from h3, all the other places you want the enter or tab to work from are d12:d23 and g12:g23?

    on enter
    1) in d column you want to move down 1 row?
    2) in g column next row unlocked 2nd row?

    on tab
    1) from d move to g if unlocked, where if locked?
    2) from g to where?

    on row 23 got to E25?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    From H3, no matter if enter or tab, go to D12.

    Yes to what you have described for 'enter', down one row in all of column d and skip every other in column g as every other is locked.

    For tab, from d to g if unlocked, and simply down 1 if g=locked.

    on row 23:
    if column d, then go to e25 on enter only and tab still goes to column g
    if column g row 23 and tab OR enter, then go to e25

    I'm using excel 2003 sp3 unfortunately.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    I could unlock H4 and D24 and implement the above code that works on onlocked cells w/ this version. But that would be like cheating, no?

  25. #25
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    you can try this it seemed to work correctly, except that everytime it activated a cell in col G it selected, but excel crashed
    vb Code:
    1. Select Case KeyCode
    2.         Case 9
    3.             If ActiveCell.Row = 23 Then Range("e25").Activate: Exit Sub
    4.             If ActiveCell.Column = 4 Then
    5.                 If Not ActiveCell.Offset(, 1).Locked Then
    6.                     ActiveCell.Offset(, 1).Activate:exit sub
    7.                     Else: ActiveCell.Offset(1).Activate
    8.                 End If
    9.             End If
    10.             If ActiveCell.Column = 7 Then ActiveCell.Offset(1, -1).Activate
    11.         Case 13
    12.             If ActiveCell.Row = 23 Then Range("e25").Activate: Exit Sub
    13.             If ActiveCell.Column = 4 Then ActiveCell.Offset(1, 0).Activate
    14.             If ActiveCell.Column = 7 Then ActiveCell.Offset(2).Activate
    15.         Case Else
    16.     End Select
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    I had to take out your line #3 above, as I didn't want 'tab' to move to row 25 unless it was from 'column 7'. I didn't need to add anything for it b/c it acts appropriately going from g23 to e25 on it's own.

    Everything seems to work good, except that old pesky "h3 to d12" issue. That still doesn't work. I tried adding:
    If Target.Address = "$H$4" Then Range("D12").Activate: Exit Sub
    If ActiveCell.row = 4 Then Range("d12").Activate: Exit Sub
    to either the 'case 13' key or the 'selection change' combobox portion and neither worked.

    If I unlock cell H4, either works! It just doesn't seem to like it locked, like since the 'target' or 'row' is never active, it doesn't trigger it.

    Should I just leave cell H4 unlocked and move on since that appears to work? I hate to spend too much of your time or keep crashing your excel for some reason!

  27. #27
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    I tried adding:
    If Target.Address = "$H$4" Then Range("D12").Activate: Exit Sub
    If ActiveCell.row = 4 Then Range("d12").Activate: Exit Sub
    to either the 'case 13' key or the 'selection change' combobox portion and neither worked.
    it will not work in the combobox keypress event at all
    it must be selection change event

    i am used to excel crashing now, save everything before running any code

    i can not compensate for any differences in the way excel 2000 handles selecting of locked cells, compared to your version (or other versions)
    Last edited by westconn1; Jan 30th, 2011 at 06:06 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    OK so I guess I will just unlock H4 to get that portion to work.

    One more thing- try this file and keep hitting the tab key from cell d12. Does it double-back on itself in the same row sometimes for you too? It's hard to explain and doesn't happen the first couple of tabs for me. Then from like d15 on down, if it can tab to the g column it does, then when you press tab again, it goes back to the d column in the same row. Then back to g again, and then it moves down a row. So it goes (with all tab hits) to cells- d14,d15,g15,d15,g15,d16,d17,g17,d17,g17,d18 etc...

    Does it work that way for you too? Thanks.
    Attached Files Attached Files

  29. #29
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel- enter/tab choosing locked cell on sheet

    it was unable to test fully when i tried last night as excel would crash, though this does not seem to be a problem for you, at least you have not said so
    i will look again tonight, also if possible try on another computer, amazing what will work on one and not another
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  30. #30

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Excel- enter/tab choosing locked cell on sheet

    I have tried this file on all the computers I can here at work, and it runs fine. Never crashed once at any point through all this testing. This place uses Office Professional edition, Excel 2003 SP3.

Posting Permissions

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



Click Here to Expand Forum to Full Width