ComboBox If then Statement for moving cell

clarkp81

New Member
Joined
May 3, 2019
Messages
13
I am trying to have a spreadsheet enter the value of the NameTextBox into a cell if a selection in XYZComboBox is selected. However, when the code is run nothing happens. There are no errors indicated but still nothing appears in the selected box. The box that I would like the value to appear in moves down one row every time new data is entered which is why it is located using the find function. Any help would be greatly appreciated.

Private Sub OKButton_Click()

If Me.XYZComboBox.Value = "ABC" Then
Range(Cells.Find(what:="ABC").Address).Activate
Range(ActiveCell.Offset(0, 1)).Value = NameTextBox.Value
End If
end sub
 
Follow up question. The cell that I am trying to enter the nametextbox.value into should contain more than one value. So when the next line of data is entered it would look some thing like "Simpson, Bart - Burns, Montgomery - etc"
I tried to use the code
b.Offset(0, 1).Value = NameTextBox.Value &""& NameTextBox.Value
but the only thing that happens is it deletes the original value and then adds the same name for every line of data. ie
Simpson,Bart
Burns,MontgomeryBurns,Montgomery
Smpson,LisaSimpson,LisaSimpson,Lisa

Code:
Private Sub OKButton_Click()
    If NameTextBox.Value = "" Then
        MsgBox "Enter data in NameTextBox"
        NameTextBox.SetFocus
        Exit Sub
    End If
    If Me.XYZComboBox.Value = "ABC" Then
        Set b = Cells.Find("ABC", LookIn:=xlValues, lookat:=xlWhole)
        If Not b Is Nothing Then
            If b.Offset(0, 1).Value = "" Then
                b.Offset(0, 1).Value = NameTextBox.Value
            Else
                b.Offset(0, 1).Value = b.Offset(0, 1).Value & ", " & NameTextBox.Value
            End If
        Else
            MsgBox "ABC, does no exists"
        End If
    Else
        MsgBox "Enter 'ABC' in combobox"
    End If
End Sub
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
It would also be okay if the next name just moved to an adjacent cell but b.Offset(0, 1)+1 didn't work.

Try this:

Code:
Private Sub OKButton_Click()
    If NameTextBox.Value = "" Then
        MsgBox "Enter data in NameTextBox"
        NameTextBox.SetFocus
        Exit Sub
    End If
    If Me.XYZComboBox.Value = "ABC" Then
        Set b = Cells.Find("ABC", LookIn:=xlValues, lookat:=xlWhole)
        If Not b Is Nothing Then
            col = Cells(b.Row, Columns.Count).End(xlToLeft).Column + 1
            Cells(b.Row, col).Value = NameTextBox.Value
        Else
            MsgBox "ABC, does no exists"
        End If
    Else
        MsgBox "Enter 'ABC' in combobox"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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