vba help - combine two loops in a Single loop

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am putting subtotal formula above the header. below code works but I want to put both code into single loop.


VBA Code:
'Situation on put formula on Second Row  - [B]Cells(2[/B], ary1(i)).FormulaR1C1

ary1 = Array(2, 6, 8) ' put formula on second Row
   For i = 0 To UBound(ary1)
         Cells(2, ary1(i)).FormulaR1C1 = "=SUBTOTAL(9,R4C" & ary1(i) & ":R" & lr_new & "C" & ary1(i) & ")"
   Next i
  
'Put formula on third Row - [B]Cells(3,[/B] ary1(i)).FormulaR1C1

ary1 = Array(3,7) 'Column 3 and 7, put formula on 3rd Row
For i = 0 To UBound(ary1)
         Cells(3, ary1(i)).FormulaR1C1 = "=SUBTOTAL(9,R4C" & ary1(i) & ":R" & lr_new & "C" & ary1(i) & ")"
Next i


Thanks
mg
 
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
How about
VBA Code:
   Ary = Array(2, 2, 3, 3, 6, 2, 7, 3, 8, 2)
   For i = 0 To UBound(Ary) Step 2
         Cells(Ary(i + 1), Ary(i)).FormulaR1C1 = "=SUBTOTAL(9,R4C" & Ary(i) & ":R" & lr_new & "C" & Ary(i) & ")"
   Next i
 
Upvote 0
Hi Fluff,

It worked !Thanks for your help, (y) ... I tried below code I used select case, getting answer.

VBA Code:
ary1 = Array(2, 6, 8 ,9,10,14) ' put formula on second Row
   For i = 0 To UBound(ary1)
   select Case Ary(i)
     case 2,6,8
         Cells(2, ary1(i)).FormulaR1C1 = "=SUBTOTAL(9,R4C" & ary1(i) & ":R" & lr_new & "C" & ary1(i) & ")"
    Case else
       Cells([B]3[/B], ary1(i)).FormulaR1C1 = "=SUBTOTAL(9,R4C" & ary1(i) & ":R" & lr_new & "C" & ary1(i) & ")"
end select

   Next i

Thanks
mg
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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