Adjusting code transfer data from combo box & textbox into userform to sheet

Closed
abdelfatah_0230 Posts 73 Registration date Thursday 18 July 2019 Status Member Last seen 23 July 2022 - 3 Nov 2019 à 04:56
 Facebook - 21 Nov 2019 à 16:03
Hello,



i would help adjusting this code it doesn't work it gives me error when i transfer data to sheet
i want to begins transfer data from a23:g23 of course automatically when i fill a new data insert a new row under a23:g23 with the same format
Private Sub CommandButton1_Click()
Dim My_sh As Worksheet
Set My_sh = Worksheets("sheet1")
Dim Lastrow As Integer
Dim i% Application.ScreenUpdating = False
With My_sh Lastrow = .Cells(Rows.Count, 1).End(3).Row + 1
.Range(.Cells(Lastrow - 1, 1), .Cells(Lastrow - 1, 7)).Copy
.Cells(Lastrow, 1).PasteSpecial Paste:=xlPasteFormats
For i = 1 To 7
.Cells(Lastrow, i).Value = Me.Controls("TextBox" & "combobox" & i)
Me.Controls("TextBox" & "combobox" & i) = ""
Next
End With
Application.ScreenUpdating = True
Application.CutCopyMode = False
MsgBox "ok"
end sub


this code gives me the error





and this is my userform :


and this is my sheet1:
Related:

1 response

TrowaD Posts 2921 Registration date Sunday 12 September 2010 Status Contributor Last seen 27 December 2022 555
5 Nov 2019 à 11:41
Hi Abdel,

You are getting an error because you have TextBox# and ComboBox#, but not a TextBoxComboBox#.

You could change:
.Cells(Lastrow, i).Value = Me.Controls("TextBox" & "combobox" & i)
Me.Controls("TextBox" & "combobox" & i) = ""

into:
.Cells(Lastrow, i).Value = Me.Controls("TextBox" & i) & " " & Me.Controls("ComboBox" & i)
Me.Controls("TextBox" & i) = vbnullstring
Me.Controls("ComboBox" & i) = vbnullstring

Best regards,
Trowa
abdelfatah_0230 Posts 73 Registration date Thursday 18 July 2019 Status Member Last seen 23 July 2022
5 Nov 2019 à 14:43
it gives me the same error
i attach my file
https://ufile.io/u2xzo9cy
TrowaD Posts 2921 Registration date Sunday 12 September 2010 Status Contributor Last seen 27 December 2022 555
11 Nov 2019 à 11:52
Hi Abdel,

When the code errors out, place your cursor over the yellow line above the "i". It should tell you that the current value for i = 7. That means that 1 to 6 went ok. So I checked TextBox7 and ComboBox7. Which showed the reason for the error: you don't have a ComboBox7.

Best regards,
Trowa
abdelfatah_0230 Posts 73 Registration date Thursday 18 July 2019 Status Member Last seen 23 July 2022
11 Nov 2019 à 13:24
you are right but despite correcting your note it is still problem it only transfer describe the brand in combobox1 and ignore the rest of me.controls also it begins from row40 not 23 i attache my image
TrowaD Posts 2921 Registration date Sunday 12 September 2010 Status Contributor Last seen 27 December 2022 555
12 Nov 2019 à 11:49
Hi Abdel,

Row 40 is the first empty row of column A from the bottom up, which is what this part of the code does:
Cells(Rows.Count, 1).End(3).Row + 1

And since that cell is merged, only the value in column A is shown.

Best regards,
Trowa
abdelfatah_0230 Posts 73 Registration date Thursday 18 July 2019 Status Member Last seen 23 July 2022
12 Nov 2019 à 13:47
so how i can enforce the code runs from row 23 under topics item, brand ,type...etc
by the way the cells rows from 23:24 not emerged cells