Copy manually (Ctrl + C) COMBOBOX

ZTK

Board Regular
Joined
Aug 20, 2021
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
I ask for your valuable knowledge, I have a drop-down list macro that they supported me right here in the forum, it is almost perfect for my needs, the fact is that I need to copy the content of the cell of the text applied by the COMBOBOX that contains it manually (CTRL + C) to another file or document.

The detail is that when selecting the COMBOBOX cell, it makes Dropdown automatically and does not allow copying ...

Is there a way to avoid this? For example, the list is displayed ONLY IF THE UP or DOWN ARROW KEY IS PRESSED?

I hope you understand me.

Thanks in advance



Attached file for reference




 
Is there a way that it doesn't matter the name of the sheet and still does exactly what is already in the code?

Try replacing the code (in sheet "00") with this:
VBA Code:
Option Explicit
'=================================================================================================
'=============== ADJUST THE CODE IN THIS PART: ===================================

Private shN As String

'where the cursor go after leaving the combobox
' ofs1 As Long = 1 means 1 row below
' ofs2 As Long = 1 means 1 column to the right
Private Const ofs1 As Long = 0
Private Const ofs2 As Long = 1


' NOTE: you might adjust combobox property in Sub toShowCombobox()

'-------- Do not change this part --------------
Private rCell As Range
Private sCOL As Long
Private vList
Private nFlag As Boolean
Private d As Object
Private xRange As Range
Private oldVal As String
'named range: XDAV_

Private Sub ComboBox1_LostFocus()
    If ComboBox1.Visible = True Then ComboBox1.Visible = False
    vList = Empty
    Application.OnKey xdvKey
End Sub

Sub toShowCombobox()

Dim Target As Range
'make sure the focus is still on this sheet
If ActiveWorkbook Is ThisWorkbook And ActiveSheet.Name = shN Then
    Set Target = ActiveCell
        
 'setting up combobox property, change to suit
        With ComboBox1
        .Height = Target.Height + 5
        .Width = Target.Width + 10
        .Top = Target.Top - 2
        .Left = Target.Offset(0, 1).Left
        
        .Visible = True
        .Value = ""
        .Activate
        End With

Else
        Application.OnKey xdvKey

End If

End Sub


'=================================================================================================
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Cells.CountLarge > 1 Then Exit Sub

    If isValid(Target) Then 'if activecell has data validation type 3
        shN = ActiveSheet.Name
        Set xRange = Evaluate(Target.Validation.Formula1)
            Application.OnKey xdvKey, ThisWorkbook.ActiveSheet.CodeName & ".toShowCombobox"
    End If

End Sub


Function isValid(f As Range) As Boolean
    Dim v
    On Error Resume Next
        v = f.Validation.Type
    On Error GoTo 0
    isValid = v = 3
End Function
Private Sub ComboBox1_GotFocus()
Dim dar As Object, x
If xRange Is Nothing Then ActiveCell.Activate: Exit Sub

    With ComboBox1
        .MatchEntry = fmMatchEntryNone
        .Value = ""
            
            vList = xRange.Value
          
            Set dar = CreateObject("System.Collections.ArrayList") 'note: arraylist always case sensitive
            Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
            
            For Each x In vList
                d(CStr(x)) = Empty
            Next
            If d.Exists("") Then d.Remove ""
            
            For Each x In d.keys
                dar.Add x
            Next
               dar.Sort
               'vList becomes unique, sorted & has no blank
               vList = dar.Toarray()
               .List = vList
               .DropDown
               dar.Clear: d.RemoveAll

    End With

        
End Sub



Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
nFlag = False
With ComboBox1
    Select Case KeyCode
        
        Case 13 'Enter
                If IsError(Application.Match(.Value, vList, 0)) Then
                    If .Value = "" Then
                        Application.EnableEvents = False
                        ActiveCell = ""
                        Application.EnableEvents = True
                        ActiveCell.Offset(ofs1, ofs2).Activate
                    Else
                        MsgBox "Wrong input", vbCritical
                    End If
                Else
                    ActiveCell.Offset(ofs1, ofs2).Activate
                End If

        Case 27, 9 'esc 'tab
                ActiveCell.Offset(ofs1, ofs2).Activate
        
        Case vbKeyDown, vbKeyUp
             nFlag = True 'don't change the list when combobox1 value is changed by DOWN ARROW or UP ARROW key
        
    End Select
End With
End Sub

Private Sub ComboBox1_Change()

With ComboBox1
    If IsNumeric(Application.Match(.Value, vList, 0)) Then
        Application.EnableEvents = False
        ActiveCell = .Value
        Application.EnableEvents = True
    End If
    
    If nFlag = True Then Exit Sub
    If Trim(.Value) = oldVal Then Exit Sub
            
            If .Value <> "" Then
                    
                    Call get_filterX
                    .List = d.keys
                    d.RemoveAll
                    .DropDown
    
            Else 'if combobox1 is empty then get the whole list
                On Error Resume Next
               .List = vList
                On Error GoTo 0
                    
            End If
    
    oldVal = Trim(.Value)
End With

End Sub


Sub get_filterX()
'search without keyword order
Dim i As Long, x, z, q
Dim v As String
Dim flag As Boolean
    
    d.RemoveAll
    z = Split(UCase(ComboBox1.Value), " ")

    For Each x In vList
        flag = True: v = UCase(x)
            For Each q In z
                If InStr(1, v, q, vbBinaryCompare) = 0 Then flag = False: Exit For
            Next
        If flag = True Then d(x) = Empty
    Next

End Sub

Sub get_filterY()
'search with keyword order
Dim x
Dim tx As String
    
    d.RemoveAll
    tx = UCase("*" & Replace((ComboBox1.Value), " ", "*") & "*")
    For Each x In vList
        If UCase(x) Like tx Then d(x) = Empty
    Next

End Sub

Sub toEnable()
Application.EnableEvents = True
End Sub

Private Sub Worksheet_Deactivate()
    Application.OnKey xdvKey
End Sub
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,214,832
Messages
6,121,847
Members
449,051
Latest member
excelquestion515

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