make code is short (loop throught tools on userform)

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
hi
i try making macro to copy data from userform to sheet with multiple textbox and combobox . it take from me more time if i have many combobox and textbox so i truly apprecitae if there is way to make the code is short
VBA Code:
Private Sub CommandButton1_Click()
Dim lr As Integer
Dim ws As Worksheet
Set ws = Sheets("invoice")
Range("e5").MergeArea = Me.TextBox1.Value
 Range("e8").MergeArea = Me.TextBox2.Value
Range("a16").Value = Me.TextBox3
Range("a17").Value = Me.TextBox4
Range("a18").Value = Me.TextBox5
Range("a19").Value = Me.TextBox6
Range("E16").Value = Me.TextBox7
Range("E17").Value = Me.TextBox8
Range("E18").Value = Me.TextBox9
Range("E19").Value = Me.TextBox10
Range("F16").Value = Me.TextBox11
Range("F17").Value = Me.TextBox12
Range("F18").Value = Me.TextBox13
Range("F19").Value = Me.TextBox14
Range("G16").Value = Me.TextBox15
Range("G17").Value = Me.TextBox16
Range("G18").Value = Me.TextBox17
Range("G19").Value = Me.TextBox18
Range("B16").Value = Me.ComboBox1
Range("B17").Value = Me.ComboBox2
Range("B18").Value = Me.ComboBox3
Range("B19").Value = Me.ComboBox4
Range("C16").Value = Me.ComboBox5
Range("C17").Value = Me.ComboBox6
Range("C18").Value = Me.ComboBox7
Range("C19").Value = Me.ComboBox8
Range("D16").Value = Me.ComboBox9
Range("D17").Value = Me.ComboBox10
Range("D18").Value = Me.ComboBox11
Range("D19").Value = Me.ComboBox12

TextBox15.Value = Me.TextBox7.Value * Me.TextBox11.Value
TextBox16.Value = Me.TextBox8.Value * Me.TextBox12.Value
TextBox17.Value = Me.TextBox9.Value * Me.TextBox13.Value
TextBox18.Value = Me.TextBox10.Value * Me.TextBox14.Value

End Sub
can anybody help ,please? if this is not enough I will attach picture for userform
thanks in advance
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
How about
VBA Code:
Private Sub CommandButton1_Click()
   Dim i As Long
   
   With Sheets("Invoice")
      .Range("e5").MergeArea = Me.TextBox1.Value
      .Range("e8").MergeArea = Me.TextBox2.Value

      For i = 3 To 6
         .Cells(i + 13, 1).Value = Me.Controls("TextBox" & i)
      Next i
      For i = 7 To 18
         .Cells(((i - 7) Mod 4) + 16, Int((i - 7) / 4) + 5).Value = Me.Controls("Textbox" & i)
      Next i
      For i = 1 To 12
         .Cells(((i - 1) Mod 4) + 16, Int((i - 1) / 4) + 2).Value = Me.Controls("ComboBox" & i)
      Next i
   End With
   TextBox15.Value = Me.TextBox7.Value * Me.TextBox11.Value
   TextBox16.Value = Me.TextBox8.Value * Me.TextBox12.Value
   TextBox17.Value = Me.TextBox9.Value * Me.TextBox13.Value
   TextBox18.Value = Me.TextBox10.Value * Me.TextBox14.Value
End Sub
 
Upvote 0
Solution
thanks . it copies the data to sheet but even that it shows error type mismatch in this line
VBA Code:
TextBox17.Value = Me.TextBox9.Value *   Me.TextBox13.Value
and clear the headers .
by the way I fill in theses textboxes just numbers and the headers begins from row 15 and the data from row 16
 
Upvote 0
Did that line work with your code?
 
Upvote 0
the error shows because I don't fill textbox17,9,13. how can I overcome this problem if I don't fill them
the error occurs in this part of code if I don't fill
VBA Code:
 TextBox15.Value = Me.TextBox7.Value * Me.TextBox11.Value
   TextBox16.Value = Me.TextBox8.Value * Me.TextBox12.Value
   TextBox17.Value = Me.TextBox9.Value * Me.TextBox13.Value
   TextBox18.Value = Me.TextBox10.Value * Me.TextBox14.Value
 
Upvote 0
That has nothing to do with your question in this thread. Did the code I suggested work other than the problem with those last few lines.
 
Upvote 0
I would share this image. not always I need fill all comboboxes and textboxes .it depends on what the customer wants so normally should not show error.

in my orginal code deals with empty combobox and textboxes as no value when copy to the sheet and doesn't show error.
1.PNG
 
Upvote 0
Please stop trying to change the question. Did the code I posted put the right values in the right cells?
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,052
Members
448,940
Latest member
mdusw

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