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 did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

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