Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Select case statement using ranges in Excel

Posted on 2011-03-24
10
Medium Priority
?
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 2000 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
 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

618 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