Solved

Select case statement using ranges in Excel

Posted on 2011-03-24
10
384 Views
Last Modified: 2012-05-11
Hello Experts.

I am trying to insert a value into column A based on a value in column E.  For example, if E1 = 40179 to 40209 then A1 = "1-10".  

I'd like this to run on all sheets within my workbook.  There aren't any blank rows within each range, and the number of rows varies on each sheet.  The first row contains headers not data.  The values I need are:

Case 40179 To 40209
         Cells(i, 1).Value = "1-10"

      Case 20210 To 40237
         Cells(i, 1).Value = "2-10"

      Case 40238 To 40268
         Cells(i, 1).Value = "3-10"

      Case 40269 To 40298
         Cells(i, 1).Value = "4-10"

      Case 40299 To 40329
         Cells(i, 1).Value = "5-10"

      Case 40330 To 40359
         Cells(i, 1).Value = "6-10"

      Case 40360 To 40390
         Cells(i, 1).Value = "7-10"

      Case 40391 To 40421
         Cells(i, 1).Value = "8-10"

      Case 40422 To 40451
         Cells(i, 1).Value = "9-10"

      Case 40452 To 40482
         Cells(i, 1).Value = "10-10"

      Case 40483 To 40512
         Cells(i, 1).Value = "11-10"

      Case 40513 To 40543
         Cells(i, 1).Value = "12-10"

      Case 40544 To 40574
         Cells(i, 1).Value = "1-11"

      Case 40575 To 40602
         Cells(i, 1).Value = "2-11"

      Case 40603 To 40633
         Cells(i, 1).Value = "3-11"

Open in new window


Any help would be greatly appreciated.

Thanks,
J
0
Comment
Question by:ferguson_jerald
10 Comments
 
LVL 8

Expert Comment

by:ragnarok89
ID: 35208420
Dim WrkSheet As Excel.Worksheet

    ' Loop thru all the worksheets.
    For Each WrkSheet In ThisWorkbook.Worksheets

your macro

    Next
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 35208498
Sub TryThis()

Dim WrkSheet As Excel.Worksheet

i = 2

For Each WrkSheet In ThisWorkbook.Worksheets

Do Until Cells(i,5).Value = ""
            
Select Case Cells(i,5).Value


      Case 40179 To 40209
            Cells(i, 1).Value = "1-10"

      Case 20210 To 40237
         Cells(i, 1).Value = "2-10"

      Case 40238 To 40268
         Cells(i, 1).Value = "3-10"

      Case 40269 To 40298
         Cells(i, 1).Value = "4-10"

      Case 40299 To 40329
         Cells(i, 1).Value = "5-10"

      Case 40330 To 40359
         Cells(i, 1).Value = "6-10"

      Case 40360 To 40390
         Cells(i, 1).Value = "7-10"

      Case 40391 To 40421
         Cells(i, 1).Value = "8-10"

      Case 40422 To 40451
         Cells(i, 1).Value = "9-10"

      Case 40452 To 40482
         Cells(i, 1).Value = "10-10"

      Case 40483 To 40512
         Cells(i, 1).Value = "11-10"

      Case 40513 To 40543
         Cells(i, 1).Value = "12-10"

      Case 40544 To 40574
         Cells(i, 1).Value = "1-11"

      Case 40575 To 40602
         Cells(i, 1).Value = "2-11"

      Case 40603 To 40633
         Cells(i, 1).Value = "3-11"

End Select

i = i + 1

Loop

Next
End Sub
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35208504
try something like this

Thomas
Sub FilterAndUpdate()
Dim sht As Worksheet, arr(1 To 15, 1 To 3) As Variant
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
arr(1, 1) = 20210
arr(2, 1) = 40179
arr(3, 1) = 40238
arr(4, 1) = 40269
arr(5, 1) = 40299
arr(6, 1) = 40330
arr(7, 1) = 40360
arr(8, 1) = 40391
arr(9, 1) = 40422
arr(10, 1) = 40452
arr(11, 1) = 40483
arr(12, 1) = 40513
arr(13, 1) = 40544
arr(14, 1) = 40575
arr(15, 1) = 40603
arr(1, 2) = 40237
arr(2, 2) = 40209
arr(3, 2) = 40268
arr(4, 2) = 40298
arr(5, 2) = 40329
arr(6, 2) = 40359
arr(7, 2) = 40390
arr(8, 2) = 40421
arr(9, 2) = 40451
arr(10, 2) = 40482
arr(11, 2) = 40512
arr(12, 2) = 40543
arr(13, 2) = 40574
arr(14, 2) = 40602
arr(15, 2) = 40633
arr(1, 3) = "2-10"
arr(2, 3) = "1-10"
arr(3, 3) = "3-10"
arr(4, 3) = "4-10"
arr(5, 3) = "5-10"
arr(6, 3) = "6-10"
arr(7, 3) = "7-10"
arr(8, 3) = "8-10"
arr(9, 3) = "9-10"
arr(10, 3) = "10-10"
arr(11, 3) = "11-10"
arr(12, 3) = "12-10"
arr(13, 3) = "1-11"
arr(14, 3) = "2-11"
arr(15, 3) = "3-11"

For Each sht In ActiveWorkbook.Worksheets
    For i = 1 To 15
        With sht.Cells(1, 1).CurrentRegion
            .AutoFilter field:=5, Criteria1:=">=" & arr(i, 1), Operator:=xlAnd, Criteria2:="<=" & arr(i, 2)
            If Application.WorksheetFunction.Subtotal(3, .Resize(, 1)) > 1 Then .Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible) = arr(i, 3)
            .AutoFilter
        End With
    Next
Next
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 500 total points
ID: 35208527
These are dates... you don't need macros to do so...
just put in the cells this formula and drag:

=MONTH(D7)&"-"&RIGHT(YEAR(D7),2)
0
 
LVL 18

Expert Comment

by:krishnakrkc
ID: 35208536
Hi,

Try


Sub kTest()
    Dim i As Long, r As Long, a As String
   
    For i = 1 To Worksheets.Count
        With Worksheets(i)
            r = .Range("e" & .Rows.Count).End(xlUp).Row
            a = .Range("e1:e" & r).Address
            .Range("a1:a" & r).NumberFormat = "@"
            .Range("a1:a" & r).Value2 = Evaluate("=if(isnumber(" & a & "),text(" & a & ",""m-yy""),"""")")
        End With
    Next
End Sub


Kris
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 39

Expert Comment

by:nutsch
ID: 35208537
Smart pick Fernando, in that case, I'd recommend
=text(d7,"mm-yy")

T
0
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35208540
IF your date is in D7 as example...
but as far as I see, you're checking in which mnth the date is, by comparing first and last day of each month with a specific data, and then showing m-yy in the column 1...
it's that simple !
0
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35208566
nutsch, you're so right :-)
0
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35208572
by the way, 20210 (mentioned in the question) was mistyped ...
0
 

Author Closing Comment

by:ferguson_jerald
ID: 35208591
Thanks Fernando!  That was great.  I've never used that fx before, but it's simple to understand.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now