Data from userform to cells

cvanderweide92177

New Member
Joined
Jun 17, 2019
Messages
1
below is the code i have so far. It works to bring the data from the userform to the sheet i want but it starts it in row 2. I want it to start in row 17.

Private Sub CommandButton1_Click()


Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("OUC_Vistana")
Dim n As Long


n = sh.Range("A" & Application.Rows.Count).End(xlUp).Row


sh.Range("K" & n + 1).Value = Me.ComboBox1.Value
sh.Range("L" & n + 1).Value = Me.TextBox1.Value
sh.Range("M" & n + 1).Value = Me.ComboBox2.Value
sh.Range("N" & n + 1).Value = Me.TextBox2.Value


'''''clear data'''''
Me.TextBox1.Value = " "
Me.TextBox2.Value = " "
Me.ComboBox1.Value = " "
Me.ComboBox2.Value = " "


'''''close form'''''
UserForm15.Hide
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi cvanderweide92177,

Welcome to MrExcel!!

A couple of things - you're setting the n variable based on the last row in Col. A yet you are posting to columns K to N. As such I have written the following code to find the last rows across those four columns and increment it by one. If that number is less than 17 then set it 17. Also you are not closing the form but hiding it. To close it you'd use the syntax Unload Me though you might well want it left open just not visible.

Regards,

Robert

Code:
Private Sub CommandButton1_Click()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("OUC_Vistana")
Dim n As Long

'n = sh.Range("A" & Application.Rows.Count).End(xlUp).Row
n = sh.Range("K:N").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1

If n < 17 Then
    n = 17
End If

sh.Range("K" & n).Value = Me.ComboBox1.Value
sh.Range("L" & n).Value = Me.TextBox1.Value
sh.Range("M" & n).Value = Me.ComboBox2.Value
sh.Range("N" & n).Value = Me.TextBox2.Value

'''''clear data'''''
Me.TextBox1.Value = " "
Me.TextBox2.Value = " "
Me.ComboBox1.Value = " "
Me.ComboBox2.Value = " "

'''''close form'''''
UserForm15.Hide
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,887
Messages
6,175,199
Members
452,617
Latest member
Narendra Babu D

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