Can you help me to shorten this code? Thanks a lot

saftawy1

Board Regular
Joined
Oct 12, 2021
Messages
65
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
test-total column.xlsb
STUVWXYZAAABACADAEAFAGAHAIAJAKALAM
1p19p20p21p22p23p24p25p26p27p28p29p30p31p32p33p34p35p36p37p38p39
2111111111111111111111
3222222222222222222222
4333333333333333333333
5444444444444444444444
6555555555555555555555
7666666666666666666666
data
Cells with Conditional Formatting
CellConditionCell FormatStop If True
P2:AM7Expression=$AT2="هديه"textNO
P2:AM7Expression=$AT2="مقعد مجانى"textNO
P2:AM7Expression=$AT2="مجمد"textNO
P2:AM7Expression=$AT2="comp"textNO
P2:AM7Expression=$AT2="pc"textNO
P2:AM7Expression=$AT2="cxl"textNO
P2:AM7Expression=$AT2="cash"textNO

VBA Code:
Me.ListBox1.AddItem "Total"

 Dim sum1 As Double, sum2 As Double, sum3 As Double, sum4 As Double, sum5 As Double, sum6 As Double, sum7 As Double, sum8 As Double, sum9 As Double, sum10 As Double, _
 sum11 As Double, sum12 As Double, sum13 As Double, sum14 As Double, sum15 As Double, sum16 As Double, sum17 As Double, sum18 As Double, sum19 As Double, sum20 As Double
 
 For i = 1 To ListBox1.ListCount - 1
 
sum1 = sum1 + Val(Me.ListBox1.List(i, 1))
sum2 = sum2 + Val(Me.ListBox1.List(i, 2))
sum3 = sum3 + Val(Me.ListBox1.List(i, 3))
sum4 = sum4 + Val(Me.ListBox1.List(i, 4))
sum5 = sum5 + Val(Me.ListBox1.List(i, 5))
sum6 = sum6 + Val(Me.ListBox1.List(i, 6))
sum7 = sum7 + Val(Me.ListBox1.List(i, 7))
sum8 = sum8 + Val(Me.ListBox1.List(i, 8))
sum9 = sum9 + Val(Me.ListBox1.List(i, 9))
sum10 = sum10 + Val(Me.ListBox1.List(i, 10))
sum11 = sum11 + Val(Me.ListBox1.List(i, 11))
sum12 = sum12 + Val(Me.ListBox1.List(i, 12))
sum13 = sum13 + Val(Me.ListBox1.List(i, 13))
sum14 = sum14 + Val(Me.ListBox1.List(i, 14))
sum15 = sum15 + Val(Me.ListBox1.List(i, 15))
sum16 = sum16 + Val(Me.ListBox1.List(i, 16))
sum17 = sum17 + Val(Me.ListBox1.List(i, 17))
sum18 = sum18 + Val(Me.ListBox1.List(i, 18))
sum19 = sum19 + Val(Me.ListBox1.List(i, 19))
sum20 = sum20 + Val(Me.ListBox1.List(i, 20))

Me.ListBox1.List(ListBox1.ListCount - 1, 1) = sum1 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = sum2 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = sum3 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = sum4 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = sum5 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = sum6 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 7) = sum7 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 8) = sum8 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 9) = sum9 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 10) = sum10 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 11) = sum11 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 12) = sum12 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 13) = sum13 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 14) = sum14 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 15) = sum15 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 16) = sum16 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 17) = sum17 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 18) = sum18 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 19) = sum19 & ".00"
Me.ListBox1.List(ListBox1.ListCount - 1, 20) = sum20 & ".00"

Next i
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
How about:

VBA Code:
    Me.ListBox1.AddItem "Total"
'
    Dim i                   As Long
    Dim J                   As Long
    Dim SumArray(1 To 20)   As Variant
'
    For i = 1 To ListBox1.ListCount - 1
        For J = 1 To 20
            SumArray(J) = SumArray(J) + Val(Me.ListBox1.List(i, J))
'
            Me.ListBox1.List(ListBox1.ListCount - 1, J) = SumArray(J) & ".00"
        Next J
    Next i

After that instead of variable called sum1 ... use SumArray(1)
For the variable called sum2 ... use SumArray(2)
etc.
 
Upvote 0
Solution
How about:

VBA Code:
    Me.ListBox1.AddItem "Total"
'
    Dim i                   As Long
    Dim J                   As Long
    Dim SumArray(1 To 20)   As Variant
'
    For i = 1 To ListBox1.ListCount - 1
        For J = 1 To 20
            SumArray(J) = SumArray(J) + Val(Me.ListBox1.List(i, J))
'
            Me.ListBox1.List(ListBox1.ListCount - 1, J) = SumArray(J) & ".00"
        Next J
    Next i

After that instead of variable called sum1 ... use SumArray(1)
For the variable called sum2 ... use SumArray(2)
etc.
thank you johnnyl very much its very good
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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