VBA CODE - Expected End With

glerwell

Well-known Member
Joined
Jun 25, 2006
Messages
1,082
Hi

Im trying to modify a VBA code I had.

However, I keep on getting a compile error "Expected End With". I am not that fimiliar with VBA to know whwere this "END WITH" should be placed.

Here is my code as it stands now:

Code:
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim Rng As Range

    With Worksheets("BETS")
    
        If .Range("B16").Value = "" Then
        
            Set Rng = .Range("B16")
        ElseIf .Range("B17") = "" Then
        
            Set Rng = .Range("B17")
        Else
        
            Set Rng = .Range("B16").End(xlDown).Offset(1, 0)
        End If
    
        With Rng
        
             .Offset(0, 0).Value = Me.ComboBox3.Value
            
        With .Offset(0, 1)
             .Formula = "=VLOOKUP(B" & Rng.Row & ",'BETTING BANKS'!$B$16:$O$60,14,FALSE)"
             .Value = .Value
            
             .Offset(0, 4).Value = Me.ComboBox1.Value
             .Offset(0, 5).Value = Me.ComboBox2.Value
            
        With .Offset(0, 6)
             .Formula = "=VLOOKUP(B" & Rng.Row & ",'BETTING BANKS'!$B$16:$N$60,12,FALSE)"
             .Value = .Value
End With
        End With
    End With
    Call sortselections
    Call copy_selections
    Unload Me
    add_selection.Show
End Sub

The problem has only occured since I added the lines that are like this:

Code:
With .Offset(0, 1)
             .Formula = "=VLOOKUP(B" & Rng.Row & ",'BETTING BANKS'!$B$16:$O$60,14,FALSE)"
             .Value = .Value

I will be adding more lines like the one above, so will somebody be kind enough to let me know where these "END WITH"'s should be placed in the code.

Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try

Code:
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim Rng As Range
With Worksheets("BETS")
    If .Range("B16").Value = "" Then
        Set Rng = .Range("B16")
    ElseIf .Range("B17") = "" Then
        Set Rng = .Range("B17")
    Else
        Set Rng = .Range("B16").End(xlDown).Offset(1, 0)
    End If
    With Rng
        .Offset(0, 0).Value = Me.ComboBox3.Value
        With .Offset(0, 1)
            .Formula = "=VLOOKUP(B" & Rng.Row & ",'BETTING BANKS'!$B$16:$O$60,14,FALSE)"
            .Value = .Value
            .Offset(0, 4).Value = Me.ComboBox1.Value
            .Offset(0, 5).Value = Me.ComboBox2.Value
        End With
        With .Offset(0, 6)
            .Formula = "=VLOOKUP(B" & Rng.Row & ",'BETTING BANKS'!$B$16:$N$60,12,FALSE)"
            .Value = .Value
        End With
    End With
End With
Call sortselections
Call copy_selections
Unload Me
add_selection.Show
End Sub
 
Upvote 0
Thanks VOG.

It did work but I did have a problem regarding

Code:
.Offset(0, 4).Value = Me.ComboBox1.Value
.Offset(0, 5).Value = Me.ComboBox2.Value

Only one of the values were being added to my sheet. However, I got it working by adding an "End With" before that line.

Hopefully, I can add the other lines to the code without any more assistance ;)

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,759
Messages
6,126,728
Members
449,332
Latest member
nokoloina

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