Add multiple values from userform and table to single cell VBA

KestutisTower

New Member
Joined
Jun 2, 2022
Messages
23
Office Version
  1. 2016
Platform
  1. Windows
Hello!

I have a code that transfers data from userform to datasheet.

VBA Code:
Sub Submit()

   Dim sh As Worksheet
   Dim iRow As Long
   
   Set sh = ThisWorkbook.Sheets("Pardavimai")
   iRow = [Counta(Pardavimai!A:A)] + 1
   
   With sh
       
       .Cells(iRow, 1) = UserForm1.txtVardas.Value
       
       .Cells(iRow, 10) = UserForm1.txtPreke.Value & "   " & UserForm1.txtKiekis.Value & " vnt." & "   " & UserForm1.txtKomentaras.Value & " - "
       
       .Cells(iRow, 11) = [Text(Now(), "DD-MM-YYYY")]
       
       .Cells(iRow, 2) = UserForm1.txtPreke.Value
       
       .Cells(iRow, 3) = UserForm1.txtKiekis.Value
       
       .Cells(iRow, 4) = UserForm1.txtKomentaras.Value
       
       .Cells(iRow, 6) = UserForm1.txtVienetoKaina.Value
              
   End With
    
End Sub

it transfers data to table:

Book111.xlsm
ABCDEFGHIJK
1PirkėjasPrekėKiekisKomentarasBendra kainaVieneto kainaLT kainaPelnasRita/LoretaPilnasData
2kestasakiniai1juodi1515akiniai 1 vnt. juodi - 2022.06.10
3ritasuknele1xl zalia2525suknele 1 vnt. xl zalia - 2022.06.10
4kestasmaike1xxl55maike 1 vnt. xxl - 2022.06.10
5kestaskremas1222kremas 1 vnt. 2 - 2022.06.09
6ritalankelis1zalias77lankelis 1 vnt. zalias - 2022.06.10
7ritakremas2ranku42kremas 2 vnt. ranku - 2022.06.11
Pardavimai
Cell Formulas
RangeFormula
E2:E7E2=F2*C2


line 8 in, the code ads values to a cell values in column J. What I need is to Also add value of coresponding cell in column E. So cell value in J1 would show " akiniai 1 vnt. juodi - 15 ", J2 - " suknele 1 vnt. xl zalia - 25 " and so on.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi,
untested but see if this update to your code does what you want

VBA Code:
Sub Submit()

   Dim sh       As Worksheet
   Dim iRow     As Long
   
   Set sh = ThisWorkbook.Worksheets("Pardavimai")
   iRow = [Counta(Pardavimai!A:A)] + 1
   
   With sh
       
       .Cells(iRow, 1) = Me.txtVardas.Value
       
       .Cells(iRow, 2) = Me.txtPreke.Value
       
       .Cells(iRow, 3) = Me.txtKiekis.Value
       
       .Cells(iRow, 4) = Me.txtKomentaras.Value
       
       .Cells(iRow, 6) = Me.txtVienetoKaina.Value
       
       .Cells(iRow, 10) = Me.txtPreke.Value & "   " & _
                          Me.txtKiekis.Value & " vnt." & "   " & _
                          Me.txtKomentaras.Value & " - " & _
                          Val(Me.txtVienetoKaina) * Val(Me.txtKiekis.Value)
                          
      .Cells(iRow, 11) = [Text(Now(), "DD-MM-YYYY")]
              
   End With
    
End Sub

Note I have replaced the hard coded UserForm1 with the Me keyword as allows you to refer to the object without specifying the name. However, this change does assume that the code is in your Userforms code page.

Hope Helpful

Dave
 
Upvote 0
Solution
Hi,
untested but see if this update to your code does what you want

VBA Code:
Sub Submit()

   Dim sh       As Worksheet
   Dim iRow     As Long
  
   Set sh = ThisWorkbook.Worksheets("Pardavimai")
   iRow = [Counta(Pardavimai!A:A)] + 1
  
   With sh
      
       .Cells(iRow, 1) = Me.txtVardas.Value
      
       .Cells(iRow, 2) = Me.txtPreke.Value
      
       .Cells(iRow, 3) = Me.txtKiekis.Value
      
       .Cells(iRow, 4) = Me.txtKomentaras.Value
      
       .Cells(iRow, 6) = Me.txtVienetoKaina.Value
      
       .Cells(iRow, 10) = Me.txtPreke.Value & "   " & _
                          Me.txtKiekis.Value & " vnt." & "   " & _
                          Me.txtKomentaras.Value & " - " & _
                          Val(Me.txtVienetoKaina) * Val(Me.txtKiekis.Value)
                         
      .Cells(iRow, 11) = [Text(Now(), "DD-MM-YYYY")]
             
   End With
   
End Sub

Note I have replaced the hard coded UserForm1 with the Me keyword as allows you to refer to the object without specifying the name. However, this change does assume that the code is in your Userforms code page.

Hope Helpful

Dave
Thanks a lot dmt32!
the code was in module, but You have suggested the way and I have done the job. :giggle: Thank you again.
 
Upvote 0
Thanks a lot dmt32!
the code was in module, but You have suggested the way and I have done the job. :giggle: Thank you again.

You are welcome - glad update does what you want

For interest, if want to keep your code in a standard module all need do is to pass the UserForm as an argument to your procedure

Modified code:

VBA Code:
Sub Submit(ByVal Form As Object)

   Dim sh       As Worksheet
   Dim iRow     As Long
   
   Set sh = ThisWorkbook.Worksheets("Pardavimai")
   iRow = [Counta(Pardavimai!A:A)] + 1
   
   With sh
       
       .Cells(iRow, 1) = Form.txtVardas.Value
       
       .Cells(iRow, 2) = Form.txtPreke.Value
       
       .Cells(iRow, 3) = Form.txtKiekis.Value
       
       .Cells(iRow, 4) = Form.txtKomentaras.Value
       
       .Cells(iRow, 6) = Form.txtVienetoKaina.Value
       
       .Cells(iRow, 10) = Form.txtPreke.Value & "   " & _
                          Form.txtKiekis.Value & " vnt." & "   " & _
                          Form.txtKomentaras.Value & " - " & _
                          Val(Form.txtVienetoKaina) * Val(Form.txtKiekis.Value)
                          
      .Cells(iRow, 11) = [Text(Now(), "DD-MM-YYYY")]
              
   End With
    
End Sub

and to call it from your userform

VBA Code:
 Submit Me

Dave
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

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