Select Case problem

ambarrovecchio

New Member
Joined
Jun 7, 2007
Messages
28
Hello all,

I'm trying to get a Select Case macro to work, but I can't quite get it to do what I want. I created a spreadsheet with names of fruit in column A, and I want the macro to copy all of the "Apple" entries into a sheet called "Apple," and the same with "Banana" and "Cherry." However, for all others (Case Else), I want the rows to go into a sheet called "Others."

Instead of splitting the rows up into the different sheets, it dumps all of the data into the "Others" sheet. The code is below. Can someone please help me? I don't understand why it isn't working. Thanks!

Code:
Select Case Range("A1:A25").Text 
Case "Apple" 
    Rows.Copy Sheets("Apple").Range("A" & Rows.Count).End(xlUp) 
Case "Banana" 
    Rows.Copy Sheets("Banana").Range("A" & Rows.Count).End(xlUp) 
Case "Cherry" 
    Rows.Copy Sheets("Cherry").Range("A" & Rows.Count).End(xlUp) 
Case Else 
    Rows.Copy Sheets("Others").Range("A" & Rows.Count).End(xlUp) 
End Select
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
You didn't loop through the range:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> foo()
    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Range("A1:A25")
            <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> c.Text
                <SPAN style="color:#00007F">Case</SPAN> "Apple"
                    c.EntireRow.Copy Sheets("Apple").Range("A" & Rows.Count).End(xlUp).Offset(1)
                <SPAN style="color:#00007F">Case</SPAN> "Banana"
                    c.EntireRow.Copy Sheets("Banana").Range("A" & Rows.Count).End(xlUp).Offset(1)
                <SPAN style="color:#00007F">Case</SPAN> "Cherry"
                    c.EntireRow.Copy Sheets("Cherry").Range("A" & Rows.Count).End(xlUp).Offset(1)
                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN>
                    c.EntireRow.Copy Sheets("Others").Range("A" & Rows.Count).End(xlUp).Offset(1)
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>
        <SPAN style="color:#00007F">Next</SPAN> c
End <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,

Smitty
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Your first three Case statements are exactly the same except for the sheet name which is the same as the value in c.Text, you could combine the first three:

Code:
Sub foo()
Dim c As Range
For Each c In Range("A1:A25")
    Select Case c.Text
        Case "Apple", "Banana", "Cherry"
            c.EntireRow.Copy Sheets(c.Text).Range("A" & Rows.Count).End(xlUp).Offset(1)
        Case Else
            c.EntireRow.Copy Sheets("Others").Range("A" & Rows.Count).End(xlUp).Offset(1)
    End Select
Next c
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,160
Messages
5,768,527
Members
425,480
Latest member
br400821

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
Top