Help with Else/If statements

nikdwg7

New Member
Joined
Jan 21, 2010
Messages
3
I'm new to writing macros and would like to check if a Sheet name contains a '#' or not.. ie: "1" vs. "1#". So far I have this:
If Sheets("1#") Then Sheets("Final Schedule").Select
Range("B4:Y4").Select
Selection.Copy
Sheets("1#").Select
Range("C14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ElseIf Sheets("1") Then Sheets("Final Schedule").Select
Range("B4:G4").Select
Selection.Copy
Sheets("1").Select
Range("C14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Final Schedule").Select
Range("H4:W4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("1").Select
Range("I12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Final Schedule").Select
Range("X4:Y4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("1").Select
Range("Y14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If

But am getting the msg: Compile error: Else without If

I'm sure this is something simple, but I can't figure it out :(
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Code:
If Sheets("1#") Then
    Sheets("Final Schedule").Range("B4:Y4").Copy
    Sheets("1#").Range("C14").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf Sheets("1") Then
    Sheets("Final Schedule").Range("B4:G4").Copy
    Sheets("1").Range("C14").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Sheets("Final Schedule").Range("H4:W4").Copy
    Sheets("1").Range("I12").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Sheets("Final Schedule").Range("X4:Y4").Copy
    Sheets("1").Range("Y14").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode=False
End If

Try that. You don't have to do all the selecting and such. And the CutCopyMode=False is irrelevant because when you start to copy a new range, it knows you want to release the last range you copied.

Else without If was because you were finishing your IF statements on the same line as you setup the condition, if you want an "opposite operation" (else) to your if condition you have to put the true operation on a seperate line. If you only want one thing to happen like:

Code:
If Range("A1").value=1 then Range("B5").Value=10

you can do it on one line, and no End If is needed.

One more edit :) ...just a constructive criticism...learn to indent your code, it makes it much easier to read for you....ok and us too :)
 
Last edited:
Upvote 0
nikdwg7,

Welcome to the MrExcel board.


The code will first check to see if either sheets "1" or "1X" exist.

If they do exist the ranges in sheets "1" and "1X" will be replaced by the values from sheet "Final Schedule".

If they do not exist they will be created, and the ranges in sheets "1" and "1X" will be replaced by the values from sheet "Final Schedule".



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Adding the Macro
1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.



Code:
Sub Test()

Application.ScreenUpdating = False

On Error Resume Next
Sheets("1#").Select
If Err Then Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "1#"
On Error GoTo 0
Sheets("1#").Range("C14:Z14").Value = Sheets("Final Schedule").Range("B4:Y4").Value


On Error Resume Next
Sheets("1").Select
If Err Then Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "1"
On Error GoTo 0
Application.CutCopyMode = False
Sheets("1").Range("C14:H14").Value = Sheets("Final Schedule").Range("B4:G4").Value
Sheets("1").Range("I12:X12").Value = Sheets("Final Schedule").Range("H4:W4").Value
Sheets("1").Range("Y14:Z14").Value = Sheets("Final Schedule").Range("X4:Y4").Value

Application.ScreenUpdating = True

End Sub


Then run the "Test" macro.
 
Upvote 0

Forum statistics

Threads
1,215,868
Messages
6,127,413
Members
449,382
Latest member
DonnaRisso

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