For each cl ... case select working as group

Matt111

Board Regular
Joined
Jul 20, 2004
Messages
83
Hi all,
The following code is supposed to run through whatever cells the user has currently selected with their mouse, and apply a formula which varies according to whats in the col header is in row 2. What i dont get is why when it runs, instead of evaluating each cell individually, and applying the appropriate formula (depending on the case select) it seems to do the Case Select on the first cell, and apply the same formula across all cells selected. I want it to assess one cell, apply appropriate formula, move on to the next, case select and apply formula, etc. Can anyone point me in the right direction please?

Code:
Sub AddFormula()
    
    Dim cl As Range
    Dim ColHeading As String

    For Each cl In Selection
   
        ColHeading = Cells(2, cl.Column).Value   
        Select Case heading
               
            Case "SalesQty"
            Selection.FormulaR1C1 = "=SUMPRODUCT((R1C=SalesData!R2C1:R140193C1)*(Main!RC12=SalesData!R2C4:R140193C4)*(Main!RC11=SalesData!R2C2:R140193C2)*(SalesData!R2C5:R140193C5))"

            Case "SalesRev"
            Selection.FormulaR1C1 = "=SUMPRODUCT((R1C=SalesData!R2C1:R140193C1)*(Main!RC12=SalesData!R2C4:R140193C4)*(Main!RC11=SalesData!R2C2:R140193C2)*(SalesData!R2C6:R140193C6))"

            Case "SalesCost"
            Selection.FormulaR1C1 = "=SUMPRODUCT((R1C=SalesData!R2C1:R140193C1)*(Main!RC12=SalesData!R2C4:R140193C4)*(Main!RC11=SalesData!R2C2:R140193C2)*(SalesData!R2C7:R140193C7))"

            Case "GratisCost"
            Selection.FormulaR1C1 = "=SUMPRODUCT((R1C=SalesData!R2C1:R140193C1)*(Main!RC12=SalesData!R2C4:R140193C4)*(Main!RC11=SalesData!R2C2:R140193C2)*(SalesData!R2C11:R140193C11))"

            Case "Net Margin"
            Selection.FormulaR1C1 = "=RC[-3]-RC[-2]-RC[-1]"
    
            End Select
        
        Next cl
    
    'Selection.Copy
    'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Shouldn't it be like this?

Rich (BB code):
Select Case ColHeading
               
            Case "SalesQty"
            cl.FormulaR1C1 = "=SUMPRODUCT((R1C=SalesData!R2C1:R140193C1)*(Main!RC12=SalesData!R2C4:R140193C4)*(Main!RC11=SalesData!R2C2:R140193C2)*(SalesData!R2C5:R140193C5))"
 
Upvote 0
Ah blimey it's CL not SELECTION! Yes - thank you!
(The change in name to ColHeading is a bit of a side, i changed it a bit when posting thinking it would help the reader). Thats sorted now thank you very much, Cheers. Matt.
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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