End if without block If error

jme851

New Member
Joined
Jan 27, 2014
Messages
7

I am using Excel 2010, I receivean error message "end if without block if" I've been working on this forseveral days and I have been un-successful. Can someone please tell me what Iam missing? The code is to look at what is in combox1 and based off what is selected by the user, it will locate that tab and put in the information from the userform. Thank you inadvance <o:p></o:p>

<code> Private SubCommandButton1_Click()<o:p></o:p>

"S:\location of file.xlsx"<o:p></o:p>

<o:p> </o:p>

'UPDATES WORKBOOK<o:p></o:p>

<o:p> </o:p>

Application.ScreenUpdating = False<o:p></o:p>

<o:p> </o:p>

Workbooks.Open "S:\locationof file.xlsx"<o:p></o:p>

<o:p> </o:p>

Set wb2 = ActiveWorkbook<o:p></o:p>

<o:p> </o:p>

If ComboBox1.Value = "Name 1" Then<o:p></o:p>

rowCount = Worksheets("sheet1").Range("A1").CurrentRegion.Rows.Count<o:p></o:p>

With Worksheets("sheet").Range("A1")<o:p></o:p>

.Offset(rowCount, 0).Value =Me.ComboBox1.Value 'NAME<o:p></o:p>

.Offset(rowCount, 1).Value =Me.ComboBox2.Value 'TASK<o:p></o:p>

.Offset(rowCount, 2).Value =Format(Now(), "MM/DD/YYYY") 'DATE<o:p></o:p>

.Offset(rowCount, 3).Value =Format(Now(), "h:mm AM/PM") 'START<o:p></o:p>

<o:p> </o:p>

If ComboBox1.Value = "Name 2" Then<o:p></o:p>

rowCount =Worksheets("Sheet2").Range("A1").CurrentRegion.Rows.Count<o:p></o:p>

WithWorksheets("Sheet2").Range("A1")<o:p></o:p>

.Offset(rowCount, 0).Value =Me.ComboBox1.Value 'NAME<o:p></o:p>

.Offset(rowCount, 1).Value =Me.ComboBox2.Value 'TASK<o:p></o:p>

.Offset(rowCount, 2).Value =Format(Now(), "MM/DD/YYYY") 'DATE<o:p></o:p>

.Offset(rowCount, 3).Value =Format(Now(), "h:mm AM/PM") 'START<o:p></o:p>

<o:p> </o:p>

If ComboBox1.Value = "Name 3" Then<o:p></o:p>

rowCount =Worksheets("Sheet3").Range("A1").CurrentRegion.Rows.Count<o:p></o:p>

With Worksheets("Sheet3").Range("A1")<o:p></o:p>

.Offset(rowCount, 0).Value =Me.ComboBox1.Value 'NAME<o:p></o:p>

.Offset(rowCount, 1).Value =Me.ComboBox2.Value 'TASK<o:p></o:p>

.Offset(rowCount, 2).Value =Format(Now(), "MM/DD/YYYY") 'DATE<o:p></o:p>

.Offset(rowCount, 3).Value =Format(Now(), "h:mm AM/PM") 'START<o:p></o:p>

<o:p> </o:p>

wb2.Save<o:p></o:p>

' MsgBox "message.", vbExclamation<o:p></o:p>

Application.ScreenUpdating = True<o:p></o:p>

End If<o:p></o:p>

End Sub <code> <o:p></o:p>
 
You are missing some End Withs and End Ifs. You can avoid adding more End Ifs by using:

Rich (BB code):
ElseIf ComboBox1.Value = "Name 2" Then
 
Upvote 0
You have 3 If statements but only one End If, each of those statements needs an End If

As well as missing End Ifs you are missing 3 End Withs.

This will compile.
Code:
 Application.ScreenUpdating = False



    Workbooks.Open "S:\locationof file.xlsx"



    Set wb2 = ActiveWorkbook



    If ComboBox1.Value = "Name 1" Then

        RowCount = Worksheets("sheet1").Range("A1").CurrentRegion.Rows.Count

        With Worksheets("sheet").Range("A1")

            .Offset(RowCount, 0).Value = Me.ComboBox1.Value    'NAME

            .Offset(RowCount, 1).Value = Me.ComboBox2.Value    'TASK

            .Offset(RowCount, 2).Value = Format(Now(), "MM/DD/YYYY")    'DATE

            .Offset(RowCount, 3).Value = Format(Now(), "h:mm AM/PM")    'START
        End With
    End If


    If ComboBox1.Value = "Name 2" Then

        RowCount = Worksheets("Sheet2").Range("A1").CurrentRegion.Rows.Count

        With Worksheets("Sheet2").Range("A1")

            .Offset(RowCount, 0).Value = Me.ComboBox1.Value    'NAME

            .Offset(RowCount, 1).Value = Me.ComboBox2.Value    'TASK

            .Offset(RowCount, 2).Value = Format(Now(), "MM/DD/YYYY")    'DATE

            .Offset(RowCount, 3).Value = Format(Now(), "h:mm AM/PM")    'START
        End With
    End If


    If ComboBox1.Value = "Name 3" Then

        RowCount = Worksheets("Sheet3").Range("A1").CurrentRegion.Rows.Count

        With Worksheets("Sheet3").Range("A1")

            .Offset(RowCount, 0).Value = Me.ComboBox1.Value    'NAME

            .Offset(RowCount, 1).Value = Me.ComboBox2.Value    'TASK

            .Offset(RowCount, 2).Value = Format(Now(), "MM/DD/YYYY")    'DATE

            .Offset(RowCount, 3).Value = Format(Now(), "h:mm AM/PM")    'START
        End With
    End If

PS You could probably cut the code down a bit, for example.
Code:
Option Explicit
Private Sub CommandButton1_Click()
Dim wb2 As Workbook
Dim RowCount As Long
Dim ws As Worksheet

    'UPDATES WORKBOOK

    Application.ScreenUpdating = False

    Set wb2 = Workbooks.Open("S:\locationof file.xlsx")

    Select Case ComboBox1.Value
        Case "Name 1"
            Set ws = wb2.Sheets("Sheet1")
        Case "Name 2"
            Set ws = wb2.Sheets("Sheet2")
        Case "Name 1"
            Set ws = wb2.Sheets("Sheet3")
    End Select

    RowCount = ws.Range("A1").CurrentRegion.Rows.Count

    With ws.Range("A1")

        .Offset(RowCount, 0).Value = Me.ComboBox1.Value    'NAME

        .Offset(RowCount, 1).Value = Me.ComboBox2.Value    'TASK

        .Offset(RowCount, 2).Value = Format(Now(), "MM/DD/YYYY")    'DATE

        .Offset(RowCount, 3).Value = Format(Now(), "h:mm AM/PM")    'START
    End With

End Sub
 
Last edited:
Upvote 0

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