What am i doing wrong ?

EEEEE

New Member
Joined
Oct 23, 2015
Messages
14
WHAT AM I DOING WRONG
I am trying to make a form in Excel that can update different tables on separate sheets depending on which sheet has been picked in a ComboBox. Can anyone please tell me what I am doing wrong:
Code:
[FONT=Lucida Grande]Private Sub CommandButton1_Click()[/FONT][FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]If ComboBox1.Text = "R&D" Then[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]Range("'R&D'!A2").Select[/FONT]
[FONT=Lucida Grande]ActiveCell.End(xlDown).Select[/FONT]
[FONT=Lucida Grande]lastrow = ActiveCell.Row[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 1).Value = TextBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 2).Value = ComboBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 3).Value = TextBox3.Text[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]ElseIf ComboBox1.Text = "Sales" Then[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]Range("'Sales'!A2").Select[/FONT]
[FONT=Lucida Grande]ActiveCell.End(xlDown).Select[/FONT]
[FONT=Lucida Grande]lastrow = ActiveCell.Row[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 1).Value = TextBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 2).Value = ComboBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 3).Value = TextBox3.Text[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]ElseIf ComboBox1.Text = "Marketing" Then[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]Range("'Marketing'!A2").Select[/FONT]
[FONT=Lucida Grande]ActiveCell.End(xlDown).Select[/FONT]
[FONT=Lucida Grande]lastrow = ActiveCell.Row[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 1).Value = TextBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 2).Value = ComboBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 3).Value = TextBox3.Text[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]ElseIf ComboBox1.Text = "Finance" Then[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]Range("'Finance'!A2").Select[/FONT]
[FONT=Lucida Grande]ActiveCell.End(xlDown).Select[/FONT]
[FONT=Lucida Grande]lastrow = ActiveCell.Row[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 1).Value = TextBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 2).Value = ComboBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 3).Value = TextBox3.Text[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]ElseIf ComboBox1.Text = "Accounting" Then[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]Range("'Accounting'!A2").<wbr>Select[/FONT]
[FONT=Lucida Grande]ActiveCell.End(xlDown).Select[/FONT]
[FONT=Lucida Grande]lastrow = ActiveCell.Row[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 1).Value = TextBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 2).Value = ComboBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 3).Value = TextBox3.Text[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]ElseIf ComboBox1.Text = "Legal" Then[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]Range("'Legal'!A2").Select[/FONT]
[FONT=Lucida Grande]ActiveCell.End(xlDown).Select[/FONT]
[FONT=Lucida Grande]lastrow = ActiveCell.Row[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 1).Value = TextBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 2).Value = ComboBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 3).Value = TextBox3.Text[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]Else[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]Range("'WEBSITE'!A2").Select[/FONT]
[FONT=Lucida Grande]ActiveCell.End(xlDown).Select[/FONT]
[FONT=Lucida Grande]lastrow = ActiveCell.Row[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 1).Value = TextBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 2).Value = ComboBox2.Text[/FONT]
[FONT=Lucida Grande]Cells(lastrow + 1, 3).Value = TextBox3.Text[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]End If[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]
[/FONT]
[FONT=Lucida Grande]End Sub[/FONT]
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You can eliminate all that selecting with code like

Code:
With ThisWorkbook.Sheets(ComboBox1.Text)
    With .Range("A2").End(xlDown).Offset(1,0)
        .Cells(1, 1).Value = TextBox2.Text
        .Cells(1, 2).Value = ComboBox2.Text
        .Cells(1, 3).Value = TextBox3.Text
    End With
End With
 
Upvote 0
Thank you so much, it works now. Much appreciated.

You can eliminate all that selecting with code like

Code:
With ThisWorkbook.Sheets(ComboBox1.Text)
    With .Range("A2").End(xlDown).Offset(1,0)
        .Cells(1, 1).Value = TextBox2.Text
        .Cells(1, 2).Value = ComboBox2.Text
        .Cells(1, 3).Value = TextBox3.Text
    End With
End With
 
Upvote 0
The code works fine, but is there any way to add the entries to the next row?
your code just updates the last field.

You can eliminate all that selecting with code like

Code:
With ThisWorkbook.Sheets(ComboBox1.Text)
    With .Range("A2").End(xlDown).Offset(1,0)
        .Cells(1, 1).Value = TextBox2.Text
        .Cells(1, 2).Value = ComboBox2.Text
        .Cells(1, 3).Value = TextBox3.Text
    End With
End With
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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