PDA

View Full Version : Save textbox to specific cell based on combobox seletection



Maxicus
02-18-2018, 01:07 AM
Hi

I am trying to save the text input in a textbox based on the combobox selection. for example

A combobox with the options of :
(Sheet3 Cell A1) Colour
(Sheet3 Cell B1) Animal
(Sheet3 Cell C1) Car
(Sheet3 Cell D1) Country.

A textbox where the user can input a text.
A Button that saves the textbox text to a dynamic name range.

I want the text in the textbox to be saved under the dynamic name range based on the selection of the combobox.

for examlpe, if i have chosen Colour and wrote blue in the textbox, i want it to save the text (blue) on the next available empty cell in Sheet3 Cell B2.

thanks in advance

mana
02-18-2018, 03:49 AM
Option Explicit


Private Sub CommandButton1_Click()
Dim col As Long

Select Case ComboBox1.Value
Case "Colour": col = 1
Case "Animal": col = 2
Case "Car": col = 3
Case "Country": col = 4
End Select

If col > 0 Then
Sheets("Sheet3").Cells(Rows.Count, col).End(xlUp).Offset(1).Value = TextBox1.Value
End If

End Sub

Maxicus
02-18-2018, 04:06 AM
Thanks Mana

I managed to get my code to work but your simplified it. here is what i was using.


Private Sub cmdAdd_Click()
Dim i As Long
If Me.Combobox1.Text = "Colour" Then
Sheet3.Activate
ActiveSheet.Range("A2").Activate
Do While IsEmpty(ActiveCell.Offset(i, 0)) = False
i = i + 1
Loop
ActiveCell.Offset(i, 0).Value = Textbox1.Value
End If

If Me.Combobox1.Text = "Animal" Then
Sheet1.Activate
ActiveSheet.Range("B2").Activate
Do While IsEmpty(ActiveCell.Offset(i, 0)) = False
i = i + 1
Loop
ActiveCell.Offset(i, 0).Value = Textbox1.Value
End If

'And repeat for rest

End Sub