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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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