End With without With, BUT WITH EXISTS!

ramkrau

New Member
Joined
Jan 9, 2019
Messages
14
So the current error I get is an End With without With
I've tried to add something that has "with" and then I get the error "For without Next"
and if I add a Next, then I get the error "Next without For"

What the heck am I doing wrong?

I'm trying to do the following tasks:
1. Create a folder with the name being a specific cell. That works great.
2. Save 4 sheets together as one workbook as an excel XML file. That works great.
3. Save one other workbook as a CSV. That is not perfect... when the CSV is saved, blank cells turn into "0" which messes up other stuff, so I tried to insert more code to replace columns A-Z with nothing if they have just a zero. That's where it all gets messed up.

Maybe someone knows what the heck is happening? Please, I'm out of ideas!

Code:
Option ExplicitSub TwoSheetsAndYourOut()


Dim NewName As String
Dim nm As Name
Dim ws As Worksheet
Dim FolderName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim xWs As Worksheet
Dim xWb As Workbook
Dim Rng As Range
Dim WorkRng As Range




Application.ScreenUpdating = False




Set xWb = Application.ThisWorkbook
FolderName = xWb.Path & "\" & Range("B3")
MkDir FolderName
     
If MsgBox("Export Sample Rule File" & vbCr & _
"Test Cases Will Also Be Created" _
    , vbYesNo, "NewCopy") = vbNo Then Exit Sub
      


With Application
        .ScreenUpdating = False
         
        Sheets(Array("TransactionType", "REVERSAL", "SHIPPING + GIFT", "FORWARD NEW")).Copy


        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            ws.Cells.Hyperlinks.Delete
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select
         
        
        
NewName = InputBox("Please Specify the name of your new workbook, using the correct version number in '1-EU-ATINtoPTC-v?'", "New Copy")
         
ActiveWorkbook.SaveAs FolderName & "\" & NewName & ".xml", FileFormat:=xlXMLSpreadsheet, CreateBackup:=False
ActiveWorkbook.Close SaveChanges:=False
         
        


.ScreenUpdating = False
Sheets(Array("Test Cases")).Copy


         
         Set WorkRng = Application.Selection
         Set WorkRng = Application.Range("A1:Z30")
         For Each Rng In WorkRng
            If Rng.Value = 0 Then
                Rng.Value = ""
        On Error Resume Next
        
        End If
        


        
        
        
        NewName = InputBox("Please Specify the name of your test cases", "New Copy")
         




    
        ActiveWorkbook.SaveAs FolderName & "\" & NewName & ".csv", FileFormat:=xlCSV, CreateBackup:=False
        ActiveWorkbook.Close SaveChanges:=False
         
        .ScreenUpdating = True
End With
    Exit Sub






End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
At a glance, I think your real problem could be that you have an End If with no If to go with it.
 
Upvote 0
Maybe:
1. Try deleting "On Error Resume Next"
2. You missed a Next for "For Each Rng In WorkRng"

Code:
For Each Rng In WorkRng
            If Rng.Value = 0 Then
                Rng.Value = ""
        [COLOR=#ff0000]On Error Resume Next[/COLOR]
        
        End If
 
Last edited:
Upvote 0
What's the purpose of the With?

Do you really need it?
Code:
Option Explicit

Sub TwoSheetsAndYourOut()
Dim NewName As String
Dim nm As Name
Dim ws As Worksheet
Dim FolderName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim xWs As Worksheet
Dim xWb As Workbook
Dim Rng As Range
Dim WorkRng As Range

    Application.ScreenUpdating = False

    Set xWb = ThisWorkbook
    
    FolderName = xWb.Path & "\" & Range("B3")
    
    MkDir FolderName

    If MsgBox("Export Sample Rule File" & vbCr & _
              "Test Cases Will Also Be Created" _
              , vbYesNo, "NewCopy") = vbNo Then
        Exit Sub

    End If

    Application.ScreenUpdating = False

    Sheets(Array("TransactionType", "REVERSAL", "SHIPPING + GIFT", "FORWARD NEW")).Copy

    For Each ws In ActiveWorkbook.Worksheets
        ws.Cells.Copy
        ws.[A1].PasteSpecial Paste:=xlValues
        ws.Cells.Hyperlinks.Delete
        Application.CutCopyMode = False
        Cells(1, 1).Select
        ws.Activate
    Next ws

    Cells(1, 1).Select

    NewName = InputBox("Please Specify the name of your new workbook, using the correct version number in '1-EU-ATINtoPTC-v?'", "New Copy")

    ActiveWorkbook.SaveAs FolderName & "\" & NewName & ".xml", FileFormat:=xlXMLSpreadsheet, CreateBackup:=False
    ActiveWorkbook.Close SaveChanges:=False

    Application.ScreenUpdating = False
    Sheets(Array("Test Cases")).Copy

    Set WorkRng = Application.Selection
    Set WorkRng = Application.Range("A1:Z30")
    
    For Each Rng In WorkRng
        If Rng.Value = 0 Then
            Rng.Value = ""
            On Error Resume Next

        End If

        NewName = InputBox("Please Specify the name of your test cases", "New Copy")

        ActiveWorkbook.SaveAs FolderName & "\" & NewName & ".csv", FileFormat:=xlCSV, CreateBackup:=False
        ActiveWorkbook.Close SaveChanges:=False

        Application.ScreenUpdating = True
        
    Next Rng

End Sub
 
Upvote 0
In this part the Next is missing:

Code:
         Set WorkRng = Application.Selection
         Set WorkRng = Application.Range("A1:Z30")
         [COLOR=#0000ff]For [/COLOR]Each Rng In WorkRng
            If Rng.Value = 0 Then
                Rng.Value = ""
        On Error Resume Next
        
        End If

That is the least of your problems, there are several lines that are not necessary in your macro.
But something important is, in this part:

Code:
ActiveWorkbook.SaveAs FolderName & "\" & NewName & ".xml", FileFormat:=xlXMLSpreadsheet, CreateBackup:=False
[COLOR=#0000ff]ActiveWorkbook.Close SaveChanges:=False[/COLOR]


You are closing your file with the macro, then the macro is no longer running.

First you must create a copy of your book, open the copy and save it as xml

The same for this part:

Code:
    ActiveWorkbook.SaveAs FolderName & "\" & NewName & ".csv", FileFormat:=xlCSV, CreateBackup:=False
[COLOR=#0000ff]    ActiveWorkbook.Close SaveChanges:=False[/COLOR]




I put the code with the blue lines not necessary. The green lines must correct.

Code:
Option Explicit


Sub TwoSheetsAndYourOut()


    Dim NewName As String
    Dim nm As Name
    Dim ws As Worksheet
    Dim FolderName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim xWs As Worksheet
    Dim xWb As Workbook
    Dim Rng As Range
    Dim WorkRng As Range
    
    Application.ScreenUpdating = False
    
    Set xWb = Application.ThisWorkbook
    FolderName = xWb.Path & "\" & Range("B3")
[COLOR=#00ff00]    MkDir FolderName[/COLOR]
     
    If MsgBox("Export Sample Rule File" & vbCr & _
        "Test Cases Will Also Be Created", vbYesNo, "NewCopy") = vbNo Then Exit Sub


[COLOR=#0000ff]'With Application[/COLOR]
[COLOR=#0000ff]'        .ScreenUpdating = False[/COLOR]
         
[COLOR=#0000ff]    'Sheets(Array("TransactionType", "REVERSAL", "SHIPPING + GIFT", "FORWARD NEW")).Copy[/COLOR]


    For Each ws In ActiveWorkbook.Worksheets
        ws.Cells.Copy
        ws.[A1].PasteSpecial Paste:=xlValues
        ws.Cells.Hyperlinks.Delete
[COLOR=#0000ff]        'Application.CutCopyMode = False[/COLOR]
[COLOR=#0000ff]        'Cells(1, 1).Select[/COLOR]
[COLOR=#0000ff]        'ws.Activate[/COLOR]
    Next ws
[COLOR=#0000ff]    'Cells(1, 1).Select[/COLOR]
        
    NewName = InputBox("Please Specify the name of your new workbook, using the correct version number in '1-EU-ATINtoPTC-v?'", "New Copy")
         
[COLOR=#00ff00]    'ActiveWorkbook.SaveAs FolderName & "\" & NewName & ".xml", FileFormat:=xlXMLSpreadsheet, CreateBackup:=False[/COLOR]
[COLOR=#00ff00]    'ActiveWorkbook.Close SaveChanges:=False[/COLOR]


[COLOR=#0000ff]    '.ScreenUpdating = False[/COLOR]
[COLOR=#0000ff]    'Sheets(Array("Test Cases")).Copy[/COLOR]
         
[COLOR=#0000ff]    'Set WorkRng = Application.Selection[/COLOR]
    Set WorkRng = Application.Range("A1:Z30")
    For Each Rng In WorkRng
        If Rng.Value = 0 Then
            Rng.Value = ""
            On Error Resume Next
        End If
[COLOR=#00ff00]    Next[/COLOR]
    NewName = InputBox("Please Specify the name of your test cases", "New Copy")


[COLOR=#00ff00]    'ActiveWorkbook.SaveAs FolderName & "\" & NewName & ".csv", FileFormat:=xlCSV, CreateBackup:=False[/COLOR]
[COLOR=#00ff00]    'ActiveWorkbook.Close SaveChanges:=False[/COLOR]
         
[COLOR=#0000ff]        '.ScreenUpdating = True[/COLOR]
[COLOR=#0000ff]'End With[/COLOR]
[COLOR=#0000ff]'    Exit Sub[/COLOR]
End Sub
 
Upvote 0
DanteAmor

Those line of code are not closing/saving the workbook that has the code, it's saving the active workbook that is created earlier in the code when sheets are copied.
 
Upvote 0
DanteAmor

Those line of code are not closing/saving the workbook that has the code, it's saving the active workbook that is created earlier in the code when sheets are copied.




You're right

Code:
Sheets(Array("TransactionType", "REVERSAL", "SHIPPING + GIFT", "FORWARD NEW")).Copy
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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