• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Selecting single cell instead of whole column in VBA

bakgerman

New Member
Hello,

This is a newb question:

I have two sheets. Sheet 1 is where there is a form to enter data. When you double click on any cell in column A, a user form pop up comes up. You enter a few keys from any entry that is in the A column of sheet 2 and it autocompletes.

The problem I am having is: I only want to enter data on a specific cell, for instance A1 .. not the whole column of A. A second thing I wanted was that instead of a double click, I wanted it to work with a single click. Can anyone please help.

Here is the VBA code for Sheet 1 where you enter the data
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim uiChosen As String
    Dim MyList As Range
    Dim myPrompt As String
   
    If Target.Column <> 1 Then Exit Sub
   
    Set MyList = Sheet2.Range("Cariler")
    myPrompt = "Lütfen Bir Cari Seçin"
    uiChosen = UserForm1.ChooseFromList(MyList, myPrompt, Default:=Target.Value, xlFilterStyle:=xlContains)

    If StrPtr(uiChosen) <> 0 Then
        Target.Value = uiChosen
        Cancel = True
    End If
End Sub

Here is the code for the user form
Code:
Option Explicit

' in userform's code module

Dim FullList As Variant
Dim FilterStyle As XlContainsOperator
Dim DisableMyEvents As Boolean
Dim AbortOne As Boolean
Const xlNoFilter As Long = xlNone

Private Sub butCancel_Click()
    Unload Me
End Sub

Private Sub butOK_Click()
    Me.Tag = "OK"
    Me.Hide
End Sub

Private Sub ComboBox1_Change()
    Dim oneItem As Variant
    Dim FilteredItems() As String
    Dim NotFlag As Boolean
    Dim Pointer As Long, i As Long
   
    If DisableMyEvents Then Exit Sub
    If AbortOne Then AbortOne = False: Exit Sub
    If TypeName(FullList) Like "*()" Then
        ReDim FilteredItems(1 To UBound(FullList))
        DisableMyEvents = True
        Pointer = 0
        With Me.ComboBox1
            Select Case FilterStyle
                Case xlBeginsWith: .Tag = LCase(.Text) & "*"
                Case xlContains: .Tag = "*" & LCase(.Text) & "*"
                Case xlDoesNotContain: .Tag = "*" & LCase(.Text) & "*": NotFlag = True
                Case xlEndsWith: .Tag = "*" & LCase(.Text)
                Case xlNoFilter: .Tag = "*"
            End Select
           
            For Each oneItem In FullList
                If (LCase(oneItem) Like .Tag) Xor NotFlag Then
                    Pointer = Pointer + 1
                    FilteredItems(Pointer) = oneItem
                End If
            Next oneItem
           
            .List = FilteredItems
            .DropDown
       
        DisableMyEvents = False
            If Pointer = 1 Then .ListIndex = 0
        End With
    End If
End Sub

Private Sub ComboBox1_Click()
    butOK.SetFocus
End Sub

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case vbKeyReturn: Call butOK_Click
        Case vbKeyUp, vbKeyDown: AbortOne = True
    End Select
End Sub

Private Sub Label1_Click()

End Sub

Private Sub UserForm_Activate()
    ComboBox1.SetFocus
    If ComboBox1.Text <> vbNullString Then
        Call ComboBox1_Change
    End If
End Sub

Private Sub UserForm_Initialize()
    ComboBox1.MatchEntry = fmMatchEntryNone
End Sub

Public Function ChooseFromList(ListSource As Variant, Optional Prompt As String = "Choose one item", _
                                        Optional Title As String = "Cari Arama Programı", Optional Default As String, _
                                        Optional xlFilterStyle As XlContainsOperator = xlBeginsWith) As String
   
    Dim Pointer As Long, oneItem As Variant
    If TypeName(ListSource) = "Range" Then
        With ListSource
            Set ListSource = Application.Intersect(.Cells, .Parent.UsedRange)
        End With
        If ListSource Is Nothing Then Exit Function
        If ListSource.Cells.Count = 1 Then
            ReDim FullList(1 To 1): FullList(1) = ListSource.Value
        ElseIf ListSource.Rows.Count = 1 Then
            FullList = Application.Transpose(Application.Transpose(ListSource))
        Else
            FullList = Application.Transpose(ListSource)
        End If
    ElseIf TypeName(ListSource) Like "*()" Then
        ReDim FullList(1 To 1)
        For Each oneItem In ListSource
            Pointer = Pointer + 1
            If UBound(FullList) < Pointer Then ReDim Preserve FullList(1 To 2 * Pointer)
            FullList(Pointer) = oneItem
        Next oneItem
        ReDim Preserve FullList(1 To Pointer)
    ElseIf Not IsObject(ListSource) Then
        ReDim FullList(1 To 1)
        FullList(1) = CStr(ListSource)
    Else
        Err.Raise 1004
    End If
   
    Me.Caption = Title
    Label1.Caption = Prompt
    FilterStyle = xlFilterStyle
   
    DisableMyEvents = True
    ComboBox1.Text = Default
    ComboBox1.List = FullList
    DisableMyEvents = False
   
    butOK.SetFocus
    Me.Show
   
    With UserForm1
        If .Tag = "OK" Then ChooseFromList = .ComboBox1.Text
    End With
End Function
 
Hi ,

It would make it easier for others to answer if you could upload a workbook with all of the data , userform and code in it.

Narayan
 
Hi ,

See if the first issue has been resolved. There is no event for a single-click ; there is one for selecting a cell , either by using the arrow keys or by using the mouse.

Narayan
 

Attachments

  • test - searchform.xlsm
    36.6 KB · Views: 3
Hi NArayan,

Thank you . That seems to work. What did you change?

How about me being able to specify which cell this user form works on?

For instance B4?
 
Hi Narayan,

Also I did not understand what you did here

Code:
    If StrPtr(uiChosen) <> 0 Then
        Range("A1").Value = uiChosen
    End If
 
Hi ,

The line of code :

Range("A1").Value = uiChosen

specifies that the selected country name will be entered in cell A1.

If you want it to be entered in B4 , change the A1 to B4.

Narayan
 
Hi Narayan,

OK I see. What I would like is: only B4 will call up the user form and the answer goes only into B4.
 
Hi Narayan,

OK that works perfectly. Thank you so much.

Now I have a new problem. The target cell is actually merged. C3:H3

I tried playing with the code you made and did different combinations of the below code but did not work:

Code:
    If Application.Intersect(Target, Range("C3:H3")) Is Nothing Then Exit Sub
    If Target.Cells.CountLarge > 6 Then Exit Sub
 
Hi Narayan,

Thanks again for your help.

Now I am trying to import what we worked on into my work in progress sheet. I am having trouble combining the two subroutines. Can you help please:

Code:
Private Sub Worksheet_Selectionchange(ByVal Target As Range)
    Dim uiChosen As String
    Dim MyList As Range
    Dim myPrompt As String
   
    If Application.Intersect(Target, Range("C3")) Is Nothing Then Exit Sub
    If Target.Cells.CountLarge > 6 Then Exit Sub
   
    Set MyList = Sheet3.Range("Cariler")
    myPrompt = "Lütfen Bir Cari Seçin"
    uiChosen = UserForm1.ChooseFromList(MyList, myPrompt, Default:=Target.Cells(1, 1).Value, xlFilterStyle:=xlContains)

    If StrPtr(uiChosen) <> 0 Then
        Target.Cells(1, 1).Value = uiChosen
    End If

End Sub

Private Sub Worksheet_Selectionchange(ByVal Target As Range)
If Target.Cells.Count > 2 Then Exit Sub
If Not Application.Intersect(Range("C5:D5"), Target) Is Nothing Then
ufCalendar.Show
End If
End Sub
 
Hi ,

Try this :
Code:
Private Sub Worksheet_Selectionchange(ByVal Target As Range)
    Dim uiChosen As String
    Dim MyList As Range
    Dim myPrompt As String
 
    If Not (Application.Intersect(Target, Range("C3")) Is Nothing) Then
      If Target.Cells.CountLarge > 6 Then Exit Sub
 
      Set MyList = Sheet3.Range("Cariler")
      myPrompt = "Lütfen Bir Cari Seçin"
      uiChosen = UserForm1.ChooseFromList(MyList, myPrompt, Default:=Target.Cells(1, 1).Value, xlFilterStyle:=xlContains)

      If StrPtr(uiChosen) <> 0 Then
          Target.Cells(1, 1).Value = uiChosen
      End If
    ElseIf Not (Application.Intersect(Target, Range("C5")) Is Nothing) Then
          If Target.Cells.Count > 2 Then Exit Sub
   
          ufCalendar.Show
    End If
End Sub
Narayan
 
Back
Top