End If without block If compile error

rawkus123

New Member
Joined
Jun 6, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Ok I'm no pro when it comes to VBA, I generally copy and paste code and basically try and figure out what means what by trial and error but for the life of me I have zero idea how to fix this.

In a nut shell I have a sheet that when I select any part of Range("B10:F34") it selects the given nCell and pastes it below but what I want is if the Range has certain text values such as "Aprrentice" or "Master" I want it "GoTo" the Labour section (I havent compiled anything there yet but it keeps giving me "End If without block If compile error" why....???


VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)
    
Dim myCell As Range
Dim myRange As Range
Set nCell = Rows(ActiveCell.Row).Columns(2).Resize(1, 5)
Set r2 = Sheets("Form").Range("B75").End(xlUp).Offset(1, 0)
Set Rng = Range("B10:B35")
         
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("B10:F34")) Is Nothing Then
    
    For Each myCell In Rng
            If myCell Like "*Apprentice*" Or _
                myCell Like "*Master*" Or _
                myCell Like "*Tradesman*" Then
                Goto Labour
    
    nCell.Copy
    r2.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Application.CutCopyMode = False
    
    ThisWorkbook.Save
       
       End If
    End If

Labour:

Next myCell

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You have (in order) If, If, For, If so you need to end them in the reverse order, End If, Next, End If, End If Your code has End If, End If, Next (incorrect order and one missing).

Any paired commands, If - End If, For - Next, While - Wend, Do - Loop (and any others that I've missed) need to be written on the basis of last opened, first closed. They can not overlap.
 
Upvote 0
I think the code can be simplified to this:

VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.CountLarge > 1 Then Exit Sub
  If Not Intersect(Target, Range("B10:F34")) Is Nothing Then
    If Not Range("B" & Target.Row) Like "*Apprentice*" And _
      Not Range("B" & Target.Row) Like "*Master*" And _
      Not Range("B" & Target.Row) Like "*Tradesman*" Then
        Sheets("Form").Range("B75").End(xlUp).Offset(1, 0).Resize(1, 5).Value = _
          Range("B" & Target.Row).Resize(1, 5).Value
    End If
  End If
End Sub
 
Upvote 0
AHHH thankyou @jasonb75 lol that makes sense, I'll keep that in mind now.
And @DanteAmor thanks for the simplified version, I'll compare the two and see how future codes can be simplified as well!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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