Unlock a cell with a ComboBox entry

XrayLemi

Board Regular
Joined
Aug 1, 2018
Messages
153
Office Version
  1. 365
Platform
  1. Windows
Hello all,
I cannot figure out how to unlock a cell with a value from an activex combobox. The cells that link to the combobox are cells G6:G3000. The cells I need to unlock are in the next row H6:H3000. There was originally just a dropdown list in column G, but I updated to use a combobox as well for more versatility. A single click brings the drop down list in the cell. A double click opens the combobox in the same cell.

Unlocking column H using the dropdown list works fine. Here is the code I used. Case 7 (column G) controls the Lock / unlock status of the corresponding cell in column H

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim r As Range, c As Range
  Set r = Union(Range("G6:G3000"))
  Set r = Intersect(Target, r)
  If Not r Is Nothing Then
   Application.EnableEvents = False
   For Each c In r
    Select Case True
    Case 7 = c.Column 'G
        If c.Value = "Not Listed" Then
          Cells(c.Row, "H").Locked = False
          Else
          Cells(c.Row, "H").Locked = True
          Cells(c.Row, "H").Value = ""
        End If
      Case Else
    End Select
  Next c
  End If

However, the combobox, does not seem to play by the same rules. After double clicking a cell in column G, the combobox appears over the cell you double clicked on. When you chose the entry "Not Listed" in the combobox, even if you Tab, Enter, or right arrow over to column H, column H will not unlock. I figured that since I was double clicking on the cell, the cell would respond as a worksheet change the same as the drop down box. I was wrong.

Here is all the code that relates to the combobox.

VBA Code:
Private Sub MDList_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
         
    Select Case KeyCode
        Case 9 'Tab
            ActiveCell.Offset(0, 1).Activate
            MDList.Visible = False
        Case 37 'Lt Arrow
            ActiveCell.Offset(0, -1).Activate
            MDList.Visible = False
        Case 39 'Rt Arrow
            ActiveCell.Offset(0, 1).Activate
            MDList.Visible = False
        Case 13
            ActiveCell.Offset(0, 1).Activate
            MDList.Visible = False
        'Case Else 'do nothing
    End Select

End Sub

Private Sub TempCombo_LostFocus()
  With Me.TempCombo
    .Top = 10
    .Left = 10
    .Width = 0
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
    .Value = ""
  End With
End Sub

Private Sub Worksheet_BeforeDoubleClick _
  (ByVal Target As Range, Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim wsList As Worksheet
Dim lSplit As Long
Dim wb As Workbook
Dim nm As Name
Dim wsNm As Worksheet
Dim rng As Range

Set ws = ActiveSheet
Set wsList = Sheets("List")
Set cboTemp = ws.OLEObjects("MDList")
On Error Resume Next

With cboTemp
  .ListFillRange = ""
  .LinkedCell = ""
  .Visible = False
End With

On Error GoTo errHandler

  If Not Intersect(Target, Range("G6:G3000")) Is Nothing Then
  Cancel = True
  Application.EnableEvents = False
  str = Target.Validation.Formula1
  str = Right(str, Len(str) - 1)
      
  'for simple INDIRECT function (English)
  ' e.g. =INDIRECT(B2)
  'will create dependent list of items
  If Left(str, 4) = "INDI" Then
    lSplit = InStr(1, str, "(")
    str = Right(str, Len(str) - lSplit)
    str = Left(str, Len(str) - 1)
    str = Range(str).Value
  End If
  
  With cboTemp
    .Visible = True
    .Left = Target.Left
    .Top = Target.Top
    .Width = Target.Width + 25
    .Height = Target.Height + 15
    .ListFillRange = str
      If .ListFillRange <> str Then
        'for dynamic named ranges
        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
  End With
  cboTemp.Activate
End If

errHandler:
  Application.EnableEvents = True
  Exit Sub

End Sub

I hope this code helps.
Thank you in advance
Jim
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Okay,
Since the last post, I tried a bunch of things which utterly failed. The closest thing I could come up with is this.

VBA Code:
If MDList.Value = "Not Listed" Then
       Target.Offset(0, 1).Locked = False
       Else: Target.Offset(0, 1).Locked = True
      End If

I added it toward the end part of this code.

VBA Code:
With cboTemp
    .Visible = True
    .Left = Target.Left
    .Top = Target.Top
    .Width = Target.Width + 25
    .Height = Target.Height + 15
    .ListFillRange = str
      If .ListFillRange <> str Then
        'for dynamic named ranges
        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 MDList.Value = "Not Listed" Then
       Target.Offset(0, 1).Locked = False
       Else: Target.Offset(0, 1).Locked = True
      End If
    
  End With
  cboTemp.Activate
End If

errHandler:
  Application.EnableEvents = True
  Exit Sub

End Sub

This only works sometimes, and frequently only after two tries. It will also ONLY work with the TAB key. It does not work with Right Arrow or Return (Enter).
I'm still trying. Hope someone can steer me in the right direction.
 
Upvote 0
Okay, I have gotten no responses But I have found a solution myself. It may not be the best way to do what I need, but it works every time.

To keep column H "Locked" I used Data validation. If Column G does not have the value "Not Listed", you cannot enter a value into column H.

This is the formula I used.
=G11="Not Listed"

To get my ComboBox to Hide, and to remove the value in H if the data is changed in column G, I had to put the code in the DownKey area. This is what I used.

VBA Code:
Select Case KeyCode
        Case 9 'Tab
            If List.Value <> "Not Listed" Then
            ActiveCell.Offset(0, 1).ClearContents
            End If
            ActiveCell.Offset(0, 1).Activate
            List.Visible = False

Although not very straight forward or "elegant" it does what I need and again it works every time.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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