Solved

Select case statement using ranges in Excel

Posted on 2011-03-24
10
387 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

765 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