make code is short (loop throught tools on userform)

abdo meghari

Board Regular
Joined
Aug 3, 2021
Messages
71
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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

abdo meghari

Board Regular
Joined
Aug 3, 2021
Messages
71
Office Version
  1. 2019
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
Did that line work with your code?
 

abdo meghari

Board Regular
Joined
Aug 3, 2021
Messages
71
Office Version
  1. 2019

ADVERTISEMENT

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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
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.
 

abdo meghari

Board Regular
Joined
Aug 3, 2021
Messages
71
Office Version
  1. 2019

ADVERTISEMENT

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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
Please stop trying to change the question. Did the code I posted put the right values in the right cells?
 

Forum statistics

Threads
1,148,283
Messages
5,745,842
Members
423,981
Latest member
ph1l

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
Top