Home » excel » excel – How to transfer data into different sheet in same workbook (using ComboBox1) in user form?

excel – How to transfer data into different sheet in same workbook (using ComboBox1) in user form?

Posted by: admin May 14, 2020 Leave a comment

Questions:

The user @K.Davis provided the code for date not entering in the sheet (problem solved!) on my request he provided the code to use combobox from the user form to select in which sheet the data will be entered. But I’m sure I’m not doing it right and getting the error message. Here’s the code:

Private Sub CommandButton1_Click()

Dim dcc As Long
Dim abc As Worksheet

Set abc = Worksheets(Me.ComboBox1.Value)
dcc = Sheets("January2019").Range("A" & Rows.Count).End(xlUp).Row

With abc

    .Cells(dcc + 1, 1).Value = Date
    .Cells(dcc + 1, 2).Value = Me.TextBox1.Value
    .Cells(dcc + 1, 3).Value = Me.TextBox2.Value
    .Cells(dcc + 1, 4).Value = Me.TextBox3.Value
    .Cells(dcc + 1, 5).Value = Me.TextBox4.Value
    .Cells(dcc + 1, 6).Value = Me.TextBox5.Value

End With
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""

End Sub

Now I’m sure if I delete the line dcc = sheets(“January2019”).range till(xlup).row then it will stop entering the date in the first free row. I could be wrong. but this is the code on the CommandButton1 which transfers the data from user form to the sheet. At the moment only sheets are made in the workbook, jan and feb but I will add more sheets. Worth mentioning here, I made the drop down list to be used in the combobox1 by watching a video. just add name of the months in a new sheet and add the name of the sheet in the rawsource of the combobox1 and the combobox got filled. The name of the months sheet is named as Sheet1.

Thanks for the help in advance.

Cheers.

How to&Answers:

Instead of using Sheet1 that contains the names of your worksheets, just use the worksheets collection to automatically populate your combobox.

So, whatever code you have that populates your ComboBox1 – get rid of it. Also, delete your Sheet1 as you will no longer use that either.

Private Sub UserForm_Initialize()
    Dim i As Long
    For i = 1 To ThisWorkbook.Sheets.Count
        Me.ComboBox1.AddItem ThisWorkbook.Sheets(i).Name
    Next
End Sub

Private Sub CommandButton1_Click()

    Dim dcc As Long
    Dim abc As Worksheet

    Set abc = ThisWorkbook.Worksheets(Me.ComboBox1.Value)

    With abc

        dcc = .Range("A" & Rows.Count).End(xlUp).Row

        .Cells(dcc + 1, 1).Value = Date
        .Cells(dcc + 1, 2).Value = Me.TextBox1.Value
        .Cells(dcc + 1, 3).Value = Me.TextBox2.Value
        .Cells(dcc + 1, 4).Value = Me.TextBox3.Value
        .Cells(dcc + 1, 5).Value = Me.TextBox4.Value
        .Cells(dcc + 1, 6).Value = Me.TextBox5.Value

    End With
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""
    TextBox5.Text = ""

End Sub

Your dcc variable also needs to be qualified with the abc worksheet – so I moved that within your With...End With block.