How to make Combobox work for merged cells having data validation

Du Pisani

New Member
Joined
Aug 10, 2018
Messages
1
Hallo,
I have found below code online, which checks if cell has data validation. If it has, it will make combo box visible to search the data validation list. But this code does not work for merged rows, it only works for merged columns.
Please suggest correction to be made.


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim Tgt As Range
Dim TgtMrg As Range
Dim c As Range
Dim TgtW As Double
Dim AddW As Long
Dim AddH As Long
 
Set ws = ActiveSheet
On Error Resume Next
'extra width to cover drop down arrow
AddW = 15
'extra height to cover cell
AddH = 5
 
If Target.Rows.Count > 1 Then GoTo exitHandler
 
Set Tgt = Target.Cells(1, 1)
Set TgtMrg = Tgt.MergeArea
On Error GoTo errHandler
 
  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 Tgt.Validation.Type = 3 Then
    Application.EnableEvents = False
    If Not TgtMrg Is Nothing Then
      'get total width of merged cells
      TgtW = 0
      For Each c In TgtMrg.Cells
        TgtW = TgtW + c.Width
      Next c
    End If
   
    str = Tgt.Validation.Formula1
    str = Right(str, Len(str) - 1)
    With cboTemp
      .Visible = False
      .Left = Tgt.Left
      .Top = Tgt.Top
      If TgtW <> 0 Then
        'use total width for merged cells
        .Width = TgtW + AddW
      Else
        .Width = Tgt.Width + AddW
      End If
      .Height = Tgt.Height + AddH
      .ListFillRange = str
      .LinkedCell = Tgt.Address
    End With
    cboTemp.Activate
    Me.TempCombo.DropDown
  End If
 
exitHandler:
  Application.EnableEvents = True
  Application.ScreenUpdating = True
  Exit Sub
errHandler:
  Resume exitHandler
 
End Sub
 
'Optional code to move to next cell if Tab or Enter are pressed
'from code by Ted Lanham
'***NOTE: if KeyDown causes problems, change to KeyUp
 
Private Sub TempCombo_KeyDown(ByVal _
        KeyCode As MSForms.ReturnInteger, _
        ByVal Shift As Integer)
  'change text value to number, if possible
  On Error Resume Next
 
  Select Case KeyCode
    Case 9 'Tab - change text to number, move right
      ActiveCell.Value = --ActiveCell.Value
      ActiveCell.Offset(0, 1).Activate
    Case 13 'Enter - change text to number, move down
      ActiveCell.Value = --ActiveCell.Value
      ActiveCell.Offset(1, 0).Activate
    Case Else
        'do nothing
  End Select
End Sub
 
 
'Private Sub Worksheet_Change(ByVal Target As Range)
   ' If Target.Address = "$J$31" Then
      '  Call Copy_Paste
   ' End If
'End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,223,958
Messages
6,175,632
Members
452,661
Latest member
Nonhle

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