Help in modifying a macro

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello ,
I have this challenge here:
I am using this code to add on my userform then I got into a big trap and I need help to get out.
Now the textboxes I am adding are all not having the same naming as the “Rw”. One is Rw10 and the other is Reg10. I wish I can change them all to have the same naming but that will give me much headache so I am thinking of a way to add them from the macro I have below. Thanks in advance.
Kelly
Code:
Sub oSum()
    Dim n As Integer, s As Double, TbRay As Variant
    TbRay = Array(10, 30)
    For n = 0 To UBound(TbRay)
        s = s + Val(Userform1.Controls("Rw" & TbRay(n)).Object.Value)
        
    Next n
     Userform1.Label1.Caption = s
End Sub
 
If that works then this is an easier way
Code:
Sub oSumSimpler()
   Dim s As Double, ufc As Variant
       
   For Each ufc In UserForm1.Controls
      If ufc.Name = "Reg10" Or ufc.Name = "Rw10" Then
         s = s + Val(ufc.Object.Value)
      End If
   Next ufc
   UserForm1.Label1.Caption = s
End Sub

or even more compact, if you're sure those two controls exist
Code:
Sub oSumEvenSimpler()
   Dim s As Double
       
   s = Val(UserForm1.Rw10.Object.Value) _
     + Val(UserForm1.Reg10.Object.Value)
   UserForm1.Label1.Caption = s
End Sub


Okay thanks very much
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,215,873
Messages
6,127,470
Members
449,384
Latest member
purevega

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