VBA Macro for coding mulpiple cases

halcyon

New Member
Joined
Dec 12, 2004
Messages
4
Hi, I'm trying to write a macro (below) that will add an appropriate range to a column. There are about 8 different cases and each case has about 6 distinct salary ranges (different from each case). The macro below is my attempt at a 'test', when run an a quickly made sheet though it dosen't quite work. I *think* the problem is in the logic between the For and IF statements. Can anyone give me a hand with this?

Cheers.



Dim ROWCOUNT As Integer

ROWCOUNT = Application.CountA(ActiveSheet.Range("A:A"))
For X = 2 To ROWCOUNT
If Cells(X, 2) = "CSR" Then Cells(X, 4).Select
ActiveCell.Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC[-1]>=1,RC[-1]<=5),""1-5"",IF(AND(RC[-1]>=6,RC[-1]<=10),""6-10"",""Check""))"
Next
For X = 2 To ROWCOUNT
If Cells(X, 2) = "CM" Then Cells(X, 4).Select
ActiveCell.Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC[-1]>=1,RC[-1]<=10),""1-10"",IF(AND(RC[-1]>=11,RC[-1]<=20),""11-20"",""Check""))"
Next
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this (and just add more CASE IS statements):
Code:
Dim ROWCOUNT As Integer

ROWCOUNT = Application.CountA(ActiveSheet.Range("A:A"))
For X = 2 To ROWCOUNT
    Select Case Cells(X, 2).Value
    Case Is = "CSR"
        Cells(X, 4).FormulaR1C1 = _
            "=IF(AND(RC[-1]>=1,RC[-1]<=5),""1-5"",IF(AND(RC[-1]>=6,RC[-1]<=10),""6-10"",""Check""))"
    Case Is = "CM"
        Cells(X, 4).FormulaR1C1 = _
            "=IF(AND(RC[-1]>=1,RC[-1]<=10),""1-10"",IF(AND(RC[-1]>=11,RC[-1]<=20),""11-20"",""Check""))"
    Case Else
    End Select
Next X

Let me know if you need any further help.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,685
Members
449,117
Latest member
Aaagu

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