This code says end with without with.

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
This code says End Without with.
There are 3 With statements & 3 End With statements so why is it saying this?


VBA Code:
Sub Pre_pro()

Dim Prepro As CommandButton
Dim JCM As Worksheet, PreAssem As Worksheet
Dim Row As Long, lr As Long, preRow As Long
Dim rng As Range, PreProrng As Range, MyCell As Range

Set JCM = ThisWorkbook.Worksheets("Job Card Master")
Set PreAssem = ThisWorkbook.Worksheets("PRE ASSEMBLY3")
Set PreProrng = JCM.Range("M13:M299")
Set Prepro = Body_And_Vehicle_Type_Form.Fill_Details

If Prepro = True Then

With PreAssem

    .Cells(1, 7) = JCM.Cells(2, 7)
    .Cells(3, 3) = JCM.Cells(8, 1)
    .Cells(3, 7) = JCM.Cells(8, 7)
    .Cells(5, 3) = JCM.Cells(6, 1) & "   " & JCM.Cells(6, 4) & "   " & JCM.Cells(6, 5)
    .Cells(5, 7) = JCM.Cells(4, 1)
    .Cells(7, 3) = JCM.Cells(6, 6)
    .Cells(7, 7) = JCM.Cells(4, 4)
    .Cells(9, 3) = JCM.Cells(4, 5)
    End With
    
With JCM
    lr = .Range("A:K").Find("*", , , , xlByRows, xlPrevious).Row
    Set rng = Application.Union(.Range("A13:K61"), _
                                .Range("A66:K122"), _
                                .Range("A127:K178"), _
                                .Range("A188:K244"), _
                                .Range("A249:K299"), _
                                .Range("A" & lr & ":" & "K" & lr))
                                End With
                                
    For Each MyCell In PreProrng
    If PreProrng Like "*Prepro*" _
    Or MyCell Like "*PREPRO*" _
    Or MyCell Like "*Preproduction*" _
    Or MyCell Like "*Pre-pro*" _
    Or MyCell Like "*Pre pro*" Then
 
       
preRow = 12
With rng
    For Row = 1 To 292
        If ((Row >= 1 And Row <= 49) Or (Row >= 53 And Row <= 110) Or (Row >= 114 And Row <= 165) _
            Or (Row >= 176 And Row <= 232) Or (Row >= 237 And Row <= 287) Or (Row = 292)) Then
            If .Cells(Row, 1) <> "" And .Cells(Row, 3) <> "" And .Cells(Row, 5) <> "" And .Cells(Row, 8) <> "" And .Cells(Row, 11) <> "" Then

               

          
                PreAssem.Cells(preRow, 1) = .Cells(Row, 1)
                PreAssem.Cells(preRow, 2) = .Cells(Row, 3)
                PreAssem.Cells(preRow, 3) = .Cells(Row, 5)
                PreAssem.Cells(preRow, 4) = .Cells(Row, 8)
                PreAssem.Cells(preRow, 5) = .Cells(Row, 11)
                preRow = preRow + 1
                 End If
                 End If
                 End If
                 Next MyCell
                 
                 End With
                 End With
            

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
you have left our the "next row" which should go after the second endif of the three end ifs together, also you have too many end with, you have 4 of them , delete the last one
 
Upvote 0
which should go after the second endif of the three end ifs together
Expanding on @offthelip's reply, when you use any commands that need to be paired (For - Next, If - End If, With - End With, While - Wend, Do - Loop) it must always be last opened, first closed. You can't open an If between For and Next, then close it after Next, it must be both opened and closed between For and Next, or opened before For and closed after Next.

If you organise your indentation so that the open and close of paired commands are lined up vertically, then it makes it much easier to follow.

Many people will enter the open and close commands together, then insert additional code between them rather than trying to remember which ones are still open.
 
Upvote 0
Any chance you could show me what you mean in my code.
I get very confused with End With Ect
 
Upvote 0
I have changed your codel to correct all the "end" statements and the "next" statement according to level that they start at, I have not checked that this is functionally correct. I have indented to show you how to lay it out to solve the probelm yourself:
VBA Code:
Sub Pre_pro()

Dim Prepro As CommandButton
Dim JCM As Worksheet, PreAssem As Worksheet
Dim Row As Long, lr As Long, preRow As Long
Dim rng As Range, PreProrng As Range, MyCell As Range

Set JCM = ThisWorkbook.Worksheets("Job Card Master")
Set PreAssem = ThisWorkbook.Worksheets("PRE ASSEMBLY3")
Set PreProrng = JCM.Range("M13:M299")
Set Prepro = Body_And_Vehicle_Type_Form.Fill_Details

If Prepro = True Then

    With PreAssem
   
        .Cells(1, 7) = JCM.Cells(2, 7)
        .Cells(3, 3) = JCM.Cells(8, 1)
        .Cells(3, 7) = JCM.Cells(8, 7)
        .Cells(5, 3) = JCM.Cells(6, 1) & "   " & JCM.Cells(6, 4) & "   " & JCM.Cells(6, 5)
        .Cells(5, 7) = JCM.Cells(4, 1)
        .Cells(7, 3) = JCM.Cells(6, 6)
        .Cells(7, 7) = JCM.Cells(4, 4)
        .Cells(9, 3) = JCM.Cells(4, 5)
    End With
   
    With JCM
        lr = .Range("A:K").Find("*", , , , xlByRows, xlPrevious).Row
        Set rng = Application.Union(.Range("A13:K61"), _
                                    .Range("A66:K122"), _
                                    .Range("A127:K178"), _
                                    .Range("A188:K244"), _
                                    .Range("A249:K299"), _
                                    .Range("A" & lr & ":" & "K" & lr))
    End With
                                   
    For Each MyCell In PreProrng
        If PreProrng Like "*Prepro*" _
        Or MyCell Like "*PREPRO*" _
        Or MyCell Like "*Preproduction*" _
        Or MyCell Like "*Pre-pro*" _
        Or MyCell Like "*Pre pro*" Then
    
          
            preRow = 12
            With rng
                For Row = 1 To 292
                    If ((Row >= 1 And Row <= 49) Or (Row >= 53 And Row <= 110) Or (Row >= 114 And Row <= 165) _
                        Or (Row >= 176 And Row <= 232) Or (Row >= 237 And Row <= 287) Or (Row = 292)) Then
                        If .Cells(Row, 1) <> "" And .Cells(Row, 3) <> "" And .Cells(Row, 5) <> "" And .Cells(Row, 8) <> "" And .Cells(Row, 11) <> "" Then
           
                          
           
                     
                            PreAssem.Cells(preRow, 1) = .Cells(Row, 1)
                            PreAssem.Cells(preRow, 2) = .Cells(Row, 3)
                            PreAssem.Cells(preRow, 3) = .Cells(Row, 5)
                            PreAssem.Cells(preRow, 4) = .Cells(Row, 8)
                            PreAssem.Cells(preRow, 5) = .Cells(Row, 11)
                            preRow = preRow + 1
                        End If
                    End If
                Next Row
            End With
        End If
    Next MyCell
End If
           

End Sub
 
Upvote 0
Solution
See post 5. Unless you've made any more changes, that code shouldn't encounter any such errors.
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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