How to paste clipboard value into a specific ComboBox on a UserForm?

179 views Asked by At

I copy a name in the first column of the active row on a spreadsheet to the clipboard.

I launch a UserForm by the name CommandsUserForm.

  • The UserForm is overlaid with multiple pages or tabs, so it defaults to the first tab (this is desired).
  • On this tab, there is a ComboBox by the name DPComboBox.

I want to paste the value in the clipboard into the ComboBox after the userform is launched.

Userform with the ComboBox highlighted.
enter image description here

Sub Show_Quick_Commands()
    DPName = ThisWorkbook.ActiveSheet.Cells(ActiveCell.Row, 1).Value
    Set DPNameforQ = New DataObject
    DPNameforQ.SetText DPName
    DPNameforQ.PutInClipboard
    CommandsUserForm.Show vbModeless
End Sub

I tried DPComboBox.PasteSpecial Transpose:=True, but that breaks the code and requests a debug.

1

There are 1 answers

0
Tim Williams On

For example:

Sub Show_Quick_Commands()
    Dim frm As CommandsUserForm
    
    Set frm = New CommandsUserForm
    frm.DPName = ActiveCell.EntireRow.Cells(1).Value 'set before showing the form
    frm.Show vbModeless
End Sub

Userform:

Option Explicit

Private m_DPName As String

Private Sub UserForm_Activate()
    Dim i As Long, v
    'put some dummy data in the combobox
    For i = 1 To 10
        Me.DPComboBox.AddItem "ClientPartner_" & Format(i, "000")
    Next i
    Me.DPComboBox.Text = m_DPName 'set the value
End Sub

'call this before showing the form
Property Let DPName(nm As String)
    m_DPName = nm
End Property