Trying to simplify

cheoke

New Member
Joined
Feb 19, 2009
Messages
32
Does anyone know a simpler way to code what I have here? This is the code for a command button I have set up. The code works fine I'm just wondering if there is a more simple way to go about doing it for future reference. Thank You

Code:
Dim Sum As Double
Dim A As Range
Dim B As Range
Dim C As Range
Dim D As Range
Dim E As Range
Dim F As Range
Dim G As Range
Dim H As Range
Dim I As Range
Dim J As Range
Dim K As Range
Dim L As Range
Dim M As Range
Dim N As Range
Dim O As Range
Dim P As Range
Dim Q As Range
Dim R As Range
Dim S As Range
Dim T As Range
Dim U As Range
Dim V As Range
Dim W As Range
Dim X As Range
Dim Y As Range
Dim Z As Range
Set A = Range("F2")
Set B = Range("F4")
Set C = Range("F6")
Set D = Range("F8")
Set E = Range("F10")
Set F = Range("F12")
Set G = Range("F14")
Set H = Range("F16")
Set I = Range("S2")
Set J = Range("S4")
Set K = Range("S6")
Set L = Range("S8")
Set M = Range("S10")
Set N = Range("S12")
Set O = Range("S14")
Set P = Range("S16")
Set Q = Range("R22")
Set R = Range("R24")
Set S = Range("R32")
Set T = Range("R34")
Set U = Range("H26")
Set V = Range("H28")
Set W = Range("H30")
Set X = Range("H32")
Set Y = Range("H34")
Set Z = Range("H36")
Sum = A + B + C + D + E + F + G + H + I + J + K + L + M + N + O + P + Q + R + S + T + U + V + W + X + Y + Z
Range("R37").Value = Sum
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Perhaps something like this

Code:
Dim MyRange as Range
Dim AllRanges as Variant
Dim i As Long

AllRanges = Array("F2","F4","F6","F8",etc...)
Set MyRange = Range(AllRanges(lbound(AllRanges)))
For i = lbound(AllRanges) + 1 to ubound(AllRanges)
    Set MyRange = Union(MyRange, Range(AllRanges(i)))
Next i

MySum = Application.Sum(MyRange)
 
Upvote 0
try this

Code:
Sub OddRange()
Dim A As Range
Dim SumOdd As Variant
Set A = Range("F2,F4,F6,F8,F10,F12,F14,F16,S2,S4,S6,S8,S10,S12,S14,S16,R22,R24,R32,R34,H26,H28,H30,H32,H34,H36")
SumOdd = WorksheetFunction.Sum(A)
Range("R37").Value = SumOdd
End Sub
 
Upvote 0
Dim sum_range As Range
Dim count As Integer

Set sum_range = Range("f2,f4,f6")
count = Application.WorksheetFunction.Sum(sum_range)

Range("a1") = count
 
Upvote 0

Forum statistics

Threads
1,203,515
Messages
6,055,844
Members
444,828
Latest member
StaffordStag

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