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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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