How to add a else statement to code

wao5012

New Member
Joined
May 7, 2018
Messages
32
Hello all,

I am using the below code to copy specific cell values and paste the entire row into a new worksheet. I am having trouble finishing the code. After the last ElseIf statement i want to add another ElseIf or Else (not sure which one i would use) that will move the rest of the data into its own worksheet.

Does anyone know how to add this statement into the code below?

Sub CopyPasteRows()
a = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To a


If Worksheets("Sheet2").Cells(i, 14).Value = "Wc" Then


Worksheets("Sheet2").Rows(i).Copy
Worksheets("Sheet3").Activate
b = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet2").Activate

ElseIf Worksheets("Sheet2").Cells(i, 14).Value = "ETOF" Then
Worksheets("Sheet2").Rows(i).Copy
Worksheets("Sheet3").Activate
b = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet2").Activate

End If

Next


Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).Select






End Sub
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
If you have a third condition, you would use ElseIf, like this...

Code:
    If Range("A1").Value = "x" Then
        'etc
        '
        '
    ElseIf Range("A1").Value = "y" Then
        'etc
        '
        '
    ElseIf Range("A1").Value = "z" Then
        'etc
        '
        '
    End If
Otherwise, to execute lines of code when the two conditions are not met, you would use Else, like this...


Code:
    If Range("A1").Value = "x" Then
        'etc
        '
        '
    ElseIf Range("A1").Value = "y" Then
        'etc
        '
        '
    Else
        'etc
        '
        '
    End If
Hope this helps!
 
Upvote 0
If you look at the code i posted you see i am splitting two cell values onto one worksheet i have a total of 10 values in that column I want the above two values on one worksheet (the code does this currently) then i want the rest of the data (8 other values) all on one in a different worksheet. How would i write the ELSE statement in the code.

Else ="every other value"

Code:
If Range("A1").Value = "x" Then
'etc
'
'
ElseIf Range("A1").Value = "y" Then
'etc
'
'
Else
'etc
'
'
End If

Hope this helps!
 
Last edited:
Upvote 0
In that case, use Else to copy rows that don't meet your conditions to another worksheet. But the method you're using is very inefficient. Try using the AutoFilter instead. First filter the data for rows that meet the two conditions, and copy them to your destination worksheet. Then filter the data again for rows that don't meet the two criteria, and copy them to your other destination worksheet. Should be much more efficient.
 
Upvote 0
Ok i will look into that. However, the file gets completed and produces what i want but it still says it is failing because of the below line. Do you know why this would do that?

The error i am receiving "Subscript out of Range"

ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).Select
 
Upvote 0
ThisWorkbook refers to the workbook running the code. Therefore, the error suggests that the workbook running the code does not contain a sheet called "Sheet2". Is this the case? Maybe you meant to refer to the active workbook?

Also, in that particular line, the Select method of the Range object will fail if Sheet2 is not the active sheet. You'll get the run time error "Select method of range class failed".
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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