Locking/unlocking a cell with a Combo Box value Mouse click problem

XrayLemi

Board Regular
Joined
Aug 1, 2018
Messages
153
Office Version
  1. 365
Platform
  1. Windows
This problem has come back to haunt me again. I have posted this before (twice actually). One of the posts gave answer that didn't work with my code and It crashed violently. The other post gave me no answers but, I thought I had found a solution my self. I was wrong. These are the previous posts.


Users have found a way around the protections I "Thought" I put in. So I'm back to square one.

Here is the problem. The cells in column G are set to use combo boxes for their entries. When you select any cell in column G the combo box appears. After making a selection in the combo box the combo box goes away.
This is the combo box code.
VBA Code:
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim wsList As Worksheet
Dim nm As Name
Dim wsNm As Worksheet
Dim rng As Range
Set wsList = Sheets("List")
Set ws = ActiveSheet
  On Error GoTo errHandler
If Target.Count > 1 Then GoTo exitHandler
  Set cboTemp = ws.OLEObjects("MDList")
    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 Not Intersect(Target, Range("G10:G5000")) Is Nothing Then
   If Target.Locked = True Then
   GoTo exitHandler
   Else
    Application.EnableEvents = False
    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)
    With cboTemp
      .Visible = True
      .Left = Target.Left
      .Top = Target.Top
      .Width = Target.Width + 75
      .Height = Target.Height + 13
      .ListFillRange = str
      If .ListFillRange <> str Then
        str = Target.Validation.Formula1
        str = Right(str, Len(str) - 1)
        Set wb = ActiveWorkbook
        Set nm = wb.Names(str)
        Set wsNm = wb.Worksheets _
          (nm.RefersToRange.Parent.Name)
        Set rng = wsNm.Range _
          (nm.RefersToRange.Address)
        .ListFillRange = "'" & wsNm.Name _
              & "'!" & rng.Address
      End If
      .LinkedCell = Target.Address
        If Target.Address <> "Not Listed" Then
         Target.Offset(0, 1).ClearContents
         End If
    End With
    cboTemp.Activate
    End If
  End If
exitHandler:
  Application.EnableEvents = True
  Application.ScreenUpdating = True
  Exit Sub
errHandler:
  Resume exitHandler
End Sub


To make the combo box go away you use the Tab key, Left/Right arrow keys, enter key, or left mouse click. Doing this will get the user to the next column either "H" or "I" (dependent on the selection in the combo box) Here is the code for the Key selections.

VBA Code:
Private Sub MDList_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
      Case 9, 39, 13 'Tab, R-Arrow, Enter
       If ActiveCell = "Not Listed" Then
        ActiveCell.Offset(0, 1).Locked = False
        ActiveCell.Offset(0, 1).Activate
       Else
        ActiveCell.Offset(0, 1).Locked = True
        ActiveCell.Offset(0, 2).Activate
        MDList.Visible = False
       End If
    End Select
    Select Case KeyCode
     Case 37 'L-Arrow
       If ActiveCell = "Not Listed" Then
        ActiveCell.Offset(0, 1).Locked = False
        ActiveCell.Offset(0, -1).Activate
       Else
        ActiveCell.Offset(0, -1).Activate
        ActiveCell.Offset(0, 2).Locked = True
        MDList.Visible = False
       End If
      End Select
    
    Select Case KeyCode
      Case 1 'L-Mouse
      If ActiveCell = "Not Listed" Then
        ActiveCell.Offset(0, 1).Locked = False
        ActiveCell.Offset(0, 1).Activate
      Else
        ActiveCell.Offset(0, 2).Activate
        MDList.Visible = False
      End If
      End Select
End Sub

The codes for Tab, Enter, Left Arrow and Right arrow work great. It is left mouse click that will not work. You can select the combo box with the mouse, use the scroll bar with the mouse, and make a choice with the mouse. But after you have made the choice with the mouse, the cell in column H will not unlock or re-lock like it does with the other keys (Tab, Enter, R and L arrows). If it cannot be done with a mouse click, is there a way around it? Sorry to be long winded but I am trying to be thorough.
Thank you in advance,
Jim
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top