Compile Error: Else without If

SpanishSoul89

New Member
Joined
Aug 7, 2015
Messages
6
Good morning all,

I'm trying to create a calendar on excel. I'm not proficient in VBA at all but I was able to find a youtube video that matched what I wanted to do. I copied the code but now I'm getting this error:

Compile Error: Else without If

on this part of the code:

Code:
'determine what weekday 1st is. . .
If Weekday(fMon) = 1 Then stCol = 2
ElseIf Weekday(fMon) = 2 Then stCol = 4
ElseIf Weekday(fMon) = 3 Then stCol = 6
ElseIf Weekday(fMon) = 4 Then stCol = 8
ElseIf Weekday(fMon) = 5 Then stCol = 10
ElseIf Weekday(fMon) = 6 Then stCol = 12
ElseIf Weekday(fMon) = 7 Then stCol = 14
End If

This is the whole code I found on youtube:

Code:
Sub CreateCalendar()
Dim csheet As Worksheet
Set csheet = ThisWorkbook.Sheets("Jan")

selDate = [b3]
fMon = DateSerial(Year(selDate), Month(selDate), 1)
lMon = CDate(Application.WorksheetFunction.EoMo*nth(fMon, 0))

stRow = 6

'clear last cal
Rows(6).ClearContents
Rows(11).ClearContents
Rows(16).ClearContents
Rows(21).ClearContents
Rows(26).ClearContents
Rows(31).ClearContents


'determine what weekday 1st is. . .
If Weekday(fMon) = 1 Then stCol = 2
ElseIf Weekday(fMon) = 2 Then stCol = 4
ElseIf Weekday(fMon) = 3 Then stCol = 6
ElseIf Weekday(fMon) = 4 Then stCol = 8
ElseIf Weekday(fMon) = 5 Then stCol = 10
ElseIf Weekday(fMon) = 6 Then stCol = 12
ElseIf Weekday(fMon) = 7 Then stCol = 14
End If

For x = 1 To Day(lMon) If FirstT = Empty Then csheet.Cells(stRow, stCol) = fMon FirstT = 1 Else fMon = fMon + 1 csheet.Cells(stRow, stCol) = fMon End If If stCol = 8 Then stCol = 2 stRow = stRow + 5 Else stCol = stCol + 1 End If




Next x

End Sub

It's from a channel called ExcelVbalsFun.

Thank you in advance for any help!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Maybe....

Code:
'determine what weekday 1st is. . .
If Weekday(fMon) = 1 Then
stCol = 2 '[COLOR=#ff0000]<--- new line[/COLOR]
ElseIf Weekday(fMon) = 2 Then stCol = 4
ElseIf Weekday(fMon) = 3 Then stCol = 6
ElseIf Weekday(fMon) = 4 Then stCol = 8
ElseIf Weekday(fMon) = 5 Then stCol = 10
ElseIf Weekday(fMon) = 6 Then stCol = 12
ElseIf Weekday(fMon) = 7 Then stCol = 14
End If

Hope this helps

M.
 
Upvote 0
Hi

Instead of

Code:
If Weekday(fMon) = 1 Then stCol = 2

in this case, since you have Else parts, the assignemnt should be in the next line

Code:
If Weekday(fMon) = 1 Then
    stCol = 2

Remark:

This was just the syntax error, but I think this is very inefficient.

Instead of


Code:
'determine what weekday 1st is. . .
If Weekday(fMon) = 1 Then stCol = 2
ElseIf Weekday(fMon) = 2 Then stCol = 4
ElseIf Weekday(fMon) = 3 Then stCol = 6
ElseIf Weekday(fMon) = 4 Then stCol = 8
ElseIf Weekday(fMon) = 5 Then stCol = 10
ElseIf Weekday(fMon) = 6 Then stCol = 12
ElseIf Weekday(fMon) = 7 Then stCol = 14
End If

Assuming fMon is a valid date, I'd use instead

Code:
stCol = Weekday(fMon) * 2
 
Upvote 0
Thank you both :) it worked.

But now this isn't working:

Code:
For x = 1 To Day(lMon) If FirstT = Empty Then csheet.Cells(stRow, stCol) = fMon FirstT = 1 Else fMon = fMon + 1 csheet.Cells(stRow, stCol) = fMon End If If stCol = 8 Then stCol = 2 stRow = stRow + 5 Else stCol = stCol + 1 End If

I really shouldn't work with codes :/ I don't know them at all.

Thank you both for the help!
 
Upvote 0
I fix it and more problem :/

Code:
Sub CreateCalendar()
Dim csheet As Worksheet
Set csheet = ThisWorkbook.Sheets("Jan")

selDate = [b3]
fMon = DateSerial(Year(selDate), Month(selDate), 1)
lMon = CDate(Application.WorksheetFunction.EoMo*nth(fMon, 0))

stRow = 6

'clear last cal
Rows(6).ClearContents
Rows(11).ClearContents
Rows(16).ClearContents
Rows(21).ClearContents
Rows(26).ClearContents
Rows(31).ClearContents


'determine what weekday 1st is. . .
If Weekday(fMon) = 1 Then
stCol = 2
ElseIf Weekday(fMon) = 2 Then stCol = 4
ElseIf Weekday(fMon) = 3 Then stCol = 6
ElseIf Weekday(fMon) = 4 Then stCol = 8
ElseIf Weekday(fMon) = 5 Then stCol = 10
ElseIf Weekday(fMon) = 6 Then stCol = 12
ElseIf Weekday(fMon) = 7 Then stCol = 14
End If



For x = 1 To Day(lMon)
    If FirstT = Empty Then
        csheet.Cells(stRow, stCol) = fMon
        FirstT = 1
    Else
        fMon = fMon + 1
        csheet.Cells(stRow, stCol) = fMon
    End If
    
    If stCol = 14 Then
        stCol = 2
        stRow = stRow + 5
    Else
        stCol = stCol + 2
    End If
       
    
        
Next x




End Sub

That's what I'm using but it said: Object doesn't support this property or method

On this part
Code:
lMon = CDate(Application.WorksheetFunction.EoMo*nth(fMon, 0))
 
Upvote 0
lMon = CDate(Application.WorksheetFunction.EoMo*nth(fMon, 0))

Perhaps that asterisk there needs to be removed?
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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