variable ranges using VBA

luvbite38

Active Member
Joined
Jun 25, 2008
Messages
368
Hi,

Can someone please help me with following situation:

I have to sum a range, but the issue is the range will always be variable. see example below:
A B
1 x 1
2 x 1
3 x 1 < here I want a macro to select the range of all the x in col A 4 y 1
5 y 1 < here I want a macro to select the range of all the Y in col A
6 z 1 < here I want a macro to select the range of all the Z in col A
7 p 1 < here I want a macro to select the range of all the p in col A

How can i get macro select the variable range. I dont wanna name the ranges, as there may be 100s of diffirent types of items in column A.

please help

Kind Regards
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,

Can someone please help me with following situation:

I have to sum a range, but the issue is the range will always be variable. see example below:
A B
1 x 1
2 x 1
3 x 1 < here I want a macro to select the range of all the x in col A 4 y 1
5 y 1 < here I want a macro to select the range of all the Y in col A
6 z 1 < here I want a macro to select the range of all the Z in col A
7 p 1 < here I want a macro to select the range of all the p in col A

How can i get macro select the variable range. I dont wanna name the ranges, as there may be 100s of diffirent types of items in column A.

please help

Kind Regards
You say you want to sum the ranges, but then you say you want the macro to select a range. What is it you are really trying to do?

Is the data sorted so that all the equal column A values are together?

If not, would it be alright to sort it like that?
 
Upvote 0
Also, does the data have a header row in row 1?
 
Upvote 0
Thanks Peter,

Basically, I have written a macro to insert a blank row everytime the value is changed in column A. Now using macro, I want to sum the ranges of each item in column A.

I want something like this:

A B
1 x 1
2 x 1
3 x 1
sub-total = 3
5 y 1

sub-total = 1
6 z 1 <
sub-total = 1
7 p 1 <
sub-total = 1
Total = 6

hope it makes sense?
 
Upvote 0
Sounds like you are trying to re-invent the wheel. Excel already has a subtotal feature that does this. You could just apply that manually, or if you really need a macro ..

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> ST()<br>    Range("A4", Range("A" & Rows.Count).End(xlUp)).Resize(, 2) _<br>        .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), _<br>        Replace:=True, PageBreaks:=False, SummaryBelowData:=<SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Thanks a million Peter,

Is there a possibility, I can send you my workbook for you to have a quick look at?

I actually trying to achieve something else, and part of it to define the variable ranges?

I am sorry for being pain.


Regards,
 
Upvote 0
Hope this makes more sense?

I am really very thankful for sparing time to help me here.

Kind Regards,
A
 
Upvote 0
Macro used is as follow:

Sub Summary2()

Application.ScreenUpdating = False


' delete the row if see sub-total

Dim r As Long, mcol As String, i As Long
Dim ChkRange As Range
Set ChkRange = Range("A:A")
Dim cell As Range
For Each cell In ChkRange
If cell = "Sub-Total" Then
cell.EntireRow.Delete
End If
Next

r = Cells(Rows.Count, "B").End(xlUp).Row

mcol = Cells(r, 1).Value

For i = r To 4 Step -1
If Cells(i, 1).Value <> mcol Then
mcol = Cells(i, 1).Value
Rows(i + 1).Insert
With Cells(i + 1, 1)
.Value = "Sub-Total"
.Offset(0, 2).Value = "@ Dia 12"
.Offset(0, 4).Value = "@ Dia 16"
.Offset(0, 6).Value = "@ Dia 20"
.Offset(0, 8).Value = "@ Dia 24"
.Offset(0, 10).Value = "@ Dia 28"
.Offset(0, 12).Value = "@ Dia 32"
.Offset(0, 14).Value = "@ Dia 36"
.Offset(0, 16).Value = "@ Dia 40"
End With
Range("A1048576").Select
Selection.End(xlUp).Select
Selection.Offset(1, 0).Select
With ActiveCell
.Value = "Sub-Total"
.Offset(0, 2).Value = "@ Dia 12"
.Offset(0, 4).Value = "@ Dia 16"
.Offset(0, 6).Value = "@ Dia 20"
.Offset(0, 8).Value = "@ Dia 24"
.Offset(0, 10).Value = "@ Dia 28"
.Offset(0, 12).Value = "@ Dia 32"
.Offset(0, 14).Value = "@ Dia 36"
.Offset(0, 16).Value = "@ Dia 40"
End With
End If
Next i
Selection.EntireRow.Delete
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I know I haven't answered your question about summing a variable range, but see if this does what you want. Test in a copy of your workbook. Check that In have data in correct rows/columns or a slight tweak will be needed.

Excel Workbook
ABCDEFGHIJKLMNOPQR
3IDItemsBAR DIASHAPESEQ NO.BAR GRADEBAR DIMENSIONSPIN DIA (P)LAPSNOBAR LENGTHTOTAL MASS
4ABCEFLNOTYPEREQ
51A12111111111111111
61A12111111111111111
71A40111111111111111
81A40111111111111111
92B12222222222222222
102B12222222222222222
112B16222222222222222
122B40222222222222222
133C40333333333333333
143C40333333333333333
153C40333333333333333
163C40333333333333333
17
SubTotals (Before)



Excel Workbook
ABCDEFGHIJKLMNOPQR
3IDItemsBAR DIASHAPESEQ NO.BAR GRADEBAR DIMENSIONSPIN DIA (P)LAPSNOBAR LENGTHTOTAL MASS
4ABCEFLNOTYPEREQ
51A12111111111111111
61A12111111111111111
71A40111111111111111
81A40111111111111111
9Sub-Total4@ Dia 122@ Dia 160@ Dia 200@ Dia 240@ Dia 280@ Dia 320@ Dia 360@ Dia 402
102B12222222222222222
112B12222222222222222
122B16222222222222222
132B40222222222222222
14Sub-Total8@ Dia 124@ Dia 162@ Dia 200@ Dia 240@ Dia 280@ Dia 320@ Dia 360@ Dia 402
153C40333333333333333
163C40333333333333333
173C40333333333333333
183C40333333333333333
19Sub-Total12@ Dia 120@ Dia 160@ Dia 200@ Dia 240@ Dia 280@ Dia 320@ Dia 360@ Dia 4012
20
SubTotals (After)



<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> STot()<br>    <SPAN style="color:#00007F">Dim</SPAN> a, st, strw, t<br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    st = Array("Sub-Total", 1, "@ Dia 12", 12, "@ Dia 16", 16, _<br>        "@ Dia 20", 20, "@ Dia 24", 24, "@ Dia 28", 28, _<br>        "@ Dia 32", 32, "@ Dia 36", 36, "@ Dia 40", 40)<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">ReDim</SPAN> t(1 <SPAN style="color:#00007F">To</SPAN> 40) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Range("A4", Range("A" & Rows.Count).End(xlUp)).Resize(, 18)<br>        Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN><br>        .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), _<br>            Replace:=True, PageBreaks:=False, SummaryBelowData:=<SPAN style="color:#00007F">True</SPAN><br>        Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br>        lr = Range("A" & Rows.Count).End(xlUp).Row - 1<br>        a = .Offset(-.Row + 1).Resize(lr).Value<br>        i = .Row<br>        <SPAN style="color:#00007F">Do</SPAN><br>            i = i + 1<br>            <SPAN style="color:#00007F">If</SPAN> a(i, 3) = "" <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">ReDim</SPAN> strw(LBound(st) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(st))<br>                <SPAN style="color:#00007F">For</SPAN> c = <SPAN style="color:#00007F">LBound</SPAN>(st) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(st)<br>                    <SPAN style="color:#00007F">If</SPAN> IsNumeric(st(c)) <SPAN style="color:#00007F">Then</SPAN><br>                        strw(c) = t(st(c))<br>                    <SPAN style="color:#00007F">Else</SPAN><br>                        strw(c) = st(c)<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Next</SPAN> c<br>                <SPAN style="color:#00007F">ReDim</SPAN> t(1 <SPAN style="color:#00007F">To</SPAN> 40) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>                <SPAN style="color:#00007F">With</SPAN> Cells(i, 1).Resize(, 18)<br>                    .Font.Bold = <SPAN style="color:#00007F">False</SPAN><br>                    .RowHeight = 25<br>                    .Value = strw<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            <SPAN style="color:#00007F">Else</SPAN><br>                t(a(i, 3)) = t(a(i, 3)) + a(i, 18)<br>                t(1) = t(1) + a(i, 18)<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> i < lr<br>        .EntireColumn.AutoFit<br>        .Resize(lr).RemoveSubtotal<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

I hope that is at least close to what you are after.
 
Upvote 0

Forum statistics

Threads
1,216,126
Messages
6,129,021
Members
449,480
Latest member
yesitisasport

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