• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Modify existing code to save to current directory, remove named ranges, and prevent dialog boxes

Asbestos_Jen

New Member
Hello all! I'm relatively new to writing macros, mostly just recording or taking things from searches and modifying to (try to) fit my needs.

I'm using the code I found here at the link below, and have modified it and it almost does what I want.
I would like to change it to:
1. save the new file "report" to the directory the source file is in;
2. delete the default sheets created in the "report" file, without dialog boxes for each one (our system defaults to 3, I don't know if that's normal);
3. stop Excel from asking about "large amount of data on clipboard";
4. remove all named ranges from the Name Manage in the "report" file.

If these should all be separate threads, please let me know.
I would be grateful for any help!

Code:
Sub Demo()
'modified from https://chandoo.org/forum/threads/range-of-selected-cells-copy-paste-with-all-formats-except-formula-save-in-a-specific-folder-name-as-a-cell-value.44527/post-265782

Dim wb As Workbook
Dim fName As String
Dim fPath As String: fPath = "C:\Users\REDACTED" 'CHANGE TO \current directory\ IN FINAL VERSION JD
Dim obj

OptimizeVBA True
Set wb = Workbooks.Add
With ThisWorkbook.Sheets("XLSTemp") 'changed sheetname JD
    fName = .Range("Z8").Text       'changed range JD
    .Copy , wb.Sheets(1)
End With

With wb
    Call DeleteRows 'delete rows that say "Delete"
    .Sheets("Sheet1").Delete
    .Sheets("Sheet2").Delete    'Added line JD
    .Sheets("Sheet3").Delete    'Added line JD
 .SaveAs fPath & fName & ".xlsx", 51

    With .Sheets("XLSTemp")     'changed sheetname JD
        .Range("U:AB").Delete   'changed range JD
        .Range("A:T").Copy      'changed range JD
        .Range("A1").PasteSpecial xlValues
        For Each obj In .Shapes
            obj.Delete
        Next
    End With
                                'Call ProtectSheet() goes here? JD
    .Close True
End With
OptimizeVBA False
End Sub
Sub OptimizeVBA(isOn As Boolean)
    Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
    Application.EnableEvents = Not (isOn)
    Application.DisplayAlerts = Not (isOn)
    Application.ScreenUpdating = Not (isOn)
End Sub
Sub DeleteRows()
'Modified from:
'https://www.mrexcel.com/board/threads/vba-delete-rows-based-on-cell-value.1171142/post-5692643
'Joe4 MyDeleteMacro()

    Dim lr As Long, lr2 As Long

    Application.ScreenUpdating = False

'   Find last row with data in column P (U in my case)
    lr = Cells(Rows.Count, "U").End(xlUp).Row
    
'   Hide all rows not equal to zero (Delete)
    Columns("U:U").AutoFilter
    ActiveSheet.Range("$U$1:$U$" & lr).AutoFilter Field:=1, Criteria1:="Delete"
    
'   Find last row in column P with data after filter (U in my case)
    lr2 = Cells(Rows.Count, "U").End(xlUp).Row

'   Exit sub if no data to delete data (only header visible)
    If lr2 = 2 Then Exit Sub

'   Delete unhidden data
    Application.DisplayAlerts = False
    ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
    Application.DisplayAlerts = True

'   Remove filter
    Range("U1").AutoFilter
    
    Application.ScreenUpdating = True
    
End Sub

Sub ProtectSheet()
   Dim Ws As Worksheet
   Set Ws = Worksheets("XLSTemp")
   Ws.Protect Password:="MyPW"
End Sub

Thanks!
Jen (sadly, still on Excel 2007)
 
Hello, my two cents :​
  1. Use ThisWorkbook.Path

  2. As copying / pasting can't be coding ! Your VBA procedure OptimizeVBA already does the trick
    but your VBA procedure DeleteRows ruins it ! So within DeleteRows you have to delete
    the dumb codelines Application.DisplayAlerts, the same for codelines Application.ScreenUpdating

  3. Already solved in previous point !

  4. Should need a For Each loop, you could start to learn just activating the Macro Recorder then by deleting a named range …

sadly, still on Excel 2007
Very not a concern with such Excel basics stuff ! Could achieve the same with older versions like 2000, 2003 …​
 
Thanks for your help, @Marc L ! The number of things I've broken by combining different codes I found online greatly exceeds the number of things I've fixed, but it's all part of the learning process.

When I've finished cleaning everything up, I'll post again for critiquing.

I'm sad about having outdated systems at work because so many of my spreadsheets would be simpler with some of the new functions I've been learning using the web version. Maybe one day the bosses will upgrade us!
 
I'm so close. I'm trying to hide and/or PW protect the ClientReport_XLS! in the source workbook. The source workbook filename is dynamic, so I'm not sure how to go back to it at the end of the process. My attempt below tries to hide the ClientReport_XLS! in the new workbook.

Besides that issue, can you see anything I could improve?


Code:
Sub Demo()
'modified from https://chandoo.org/forum/threads/range-of-selected-cells-copy-paste-with-all-formats-except-formula-save-in-a-specific-folder-name-as-a-cell-value.44527/post-265782

Dim wb As Workbook
Dim fName As String
Dim fPath As String: fPath = ThisWorkbook.Path        'mod 2024-11-21 JD

Dim obj
     
    Sheets("ClientReport_XLS").Visible = True   'Code errors with ClientReport_XLS! hidden and/or protected JD 2024-11-21
   
OptimizeVBA True
Set wb = Workbooks.Add
With ThisWorkbook.Sheets("ClientReport_XLS")        'changed sheetname JD
    fName = .Range("AC10").Text                                 'changed range JD
    .Copy , wb.Sheets(1)
End With

With wb
   Call DeleteRows 'delete rows that say "Delete"
    .Sheets("Sheet1").Delete
    .Sheets("Sheet2").Delete                                         'Added line JD
    .Sheets("Sheet3").Delete                                         'Added line JD
 .SaveAs fPath & "\" & fName & ".xlsx", 51                 'Added & "\" 2024-11-21 JD

    With .Sheets("ClientReport_XLS")                            'changed sheetname JD
        .Range("U:AZ").Delete                                         'changed range JD
        .Range("A:T").Copy                                              'changed range JD
        .Range("A1").PasteSpecial xlValues
        For Each obj In .Shapes
            obj.Delete
        Next
    End With
 
  Call DeleteNames
  Call ProtectSheet
   
End With

OptimizeVBA False
Application.GoTo Reference:="R1C1"     'Go to cell A1 added 2024-11-21

    Sheets("ClientReport_XLS").Select               'Want ClientReport_XLS! in source wb hidden and/or protected JD 2024-11-21
    ActiveWindow.SelectedSheets.Visible = False     '
 
End Sub
--------------
Sub OptimizeVBA(isOn As Boolean)
    Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
    Application.EnableEvents = Not (isOn)
    Application.DisplayAlerts = Not (isOn)
    Application.ScreenUpdating = Not (isOn)
End Sub
--------------
Sub DeleteRows()
'Modified from:
'https://www.mrexcel.com/board/threads/vba-delete-rows-based-on-cell-value.1171142/post-5692643
'Joe4 MyDeleteMacro()

    Dim lr As Long, lr2 As Long

'   Find last row with data in column P (U in my case)
    lr = Cells(Rows.Count, "U").End(xlUp).Row
   
'   Hide all rows not equal to zero (<>"Delete")
    Columns("U:U").AutoFilter
    ActiveSheet.Range("$U$1:$U$" & lr).AutoFilter Field:=1, Criteria1:="Delete"
   
'   Find last row in column P with data after filter (U in my case)
    lr2 = Cells(Rows.Count, "U").End(xlUp).Row

'   Exit sub if no data to delete data (only header visible)
    If lr2 = 2 Then Exit Sub

'   Delete unhidden data
    ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete

'   Remove filter
    Range("U1").AutoFilter
    
End Sub
--------------
Sub ProtectSheet()
   Dim Ws As Worksheet
   Set Ws = Worksheets("ClientReport_XLS")
   Ws.Protect Password:="MyPW"
End Sub
------------------
Sub DeleteNames()
'modified from: https://www.mrexcel.com/board/threads/vba-remove-all-named-ranges.1052604/post-5065838

Dim RgeName As Name

On Error Resume Next
For Each RangeName In Names
    ActiveWorkbook.Names(RangeName.Name).Delete
Next
On Error GoTo 0
End Sub
--------------
 
can you see anything I could improve?
All ‼​
So removing the superfluous, the useless, the bad, the uggly, … as I can't test this revamped version​
you may have to operate some correction :​
Code:
Sub Demo1r()
   With Application
       .ScreenUpdating = False
       .EnableEvents = False
       .DisplayAlerts = False
       .CopyObjectsWithCells = False
       .Calculation = xlCalculationManual
   With Sheets("ClientReport_XLS")            ' better is to get rid of the sheet's Name just using its CodeName !
       .Protect "MyPW", , , , True
       .Visible = True
        Workbooks.Add xlWBATWorksheet
        ActiveSheet.Name = .Name
  With .UsedRange.Columns
       .Item(21).AutoFilter 1, "<>Delete"
       .Item("A:T").Copy [A1]
       .Item(21).AutoFilter
  End With
        With [A1].CurrentRegion:  .Formula = .Value:  End With
        ActiveWorkbook.SaveAs .Parent.Path & .["\"&AC10], 51
       .Visible = False
       .Parent.Activate
   End With
       .Calculation = xlCalculationAutomatic
       .CopyObjectsWithCells = True
       .DisplayAlerts = True
       .EnableEvents = True
       .ScreenUpdating = True
   End With
End Sub
Do you like it ? So thanks to click on bottom right Like !​
 
I tried your code from post #6, and it does most of what I need, except the following issues:

1. None of the cells are locked, and the sheet is not protected;
2. the page headers and footers don't appear in the new book;
3. Wrap Text is on in a few random places;
4. the row and column heights/widths are not correct.

Thanks again for your help.
 
I'm working on a redacted version of the sheet that's true enough to original to be useful, but there's a lot of sensitive information that I have to remove.
 
I tried your code from post #6, and it does most of what I need, except the following issues
Try the below VBA demonstration.​
In case you need more help, according to this forum rules an attachment is very necessary​
with the master sample workbook and accordingly the exact expected result workbook …​
To paste only to the ClientReport_XLS worksheet module : (v2)​
Code:
Sub Demo2()
  Const C = 21, D = "Delete", P = "MyPW"
    Dim oNm As Name, Rc As Range
        Protect P, , , , True
   With Application
       .ScreenUpdating = False
       .EnableEvents = False
       .DisplayAlerts = False
       .CopyObjectsWithCells = False
       .Calculation = xlCalculationManual
        Visible = -1
        Copy
        For Each oNm In ActiveWorkbook.Names:  oNm.Delete:  Next
        Set Rc = ActiveSheet.UsedRange.Columns
     If Rc.Count >= C Then
     If IsNumeric(.Match(D, Rc(C), 0)) Then
        Rc(C).AutoFilter 1, D
        Rc.Offset(1).EntireRow.Delete
        Rc.AutoFilter
     End If
        Rc(C).Resize(, Rc.Count - C + 1).EntireColumn.Delete
     End If
        Rc.Copy
        Rc.PasteSpecial xlPasteValues
       .Goto Rc.Cells(1), True
        Set Rc = Nothing
        ActiveSheet.Protect P
        ActiveWorkbook.SaveAs Parent.Path & ["\"&AC10], 51
        ActiveWorkbook.Close
        Visible = 0
       .Calculation = xlCalculationAutomatic
       .CopyObjectsWithCells = True
       .DisplayAlerts = True
       .EnableEvents = True
       .ScreenUpdating = True
   End With
End Sub
Do you like it ? So thanks to click on bottom right Like !
 
I have tested the code above using this heavily redacted spreadsheet. The number formats don't all transfer, the sheet isn't locked or protected, and the macro stays with the final ClientReport_xls.

I do have it working with a few adjustments to the code I have in post#4, so it's not vital to get your code working, but if you want to keep at it, I appreciate your efforts.
 

Attachments

  • RedactedTemplate.xlsm
    203.2 KB · Views: 1
The number formats don't all transfer
Weird the codeline Rc.Formula = Rc.Value changes some formats so back to Copy / PasteSpecial methods.​
the sheet isn't locked or protected
Yes, the Worksheet.Copy command does not keep the protection so adding a Protect statement.​
the macro stays with the final ClientReport_xls.
Wrong as any VBA procedure can't be saved within a .xlsx workbook like you can check just closing it and re-opening it !​
Post #10 Demo2 procedure edited accordingly …​
 
Wrong as any VBA procedure can't be saved within a .xlsx workbook like you can check just closing it and re-opening it !​
I thought I had checked by closing and reopening it, but I must have been looking at the wrong file, or had too many files open.

Everything looks as it should with the newest edit on post #10.

I will put it into my regular (non-redacted) template and see how it goes.

Thank you so much for your time in writing this code. I think I understand everything, but I definitely would not have come up with it!
 
but I must have been looking at the wrong file
In fact you have been looking to the correct file as the VBA procedure is still visible until the .xlsx workbook is closed !​
Somewhat like a safety in case the file format was not the correct one in order to not lose any code before to close the workbook …​
 
The line below errors in the full template.
Code:
 For Each oNm In ActiveWorkbook.Names:  oNm.Delete:  Next
1732665645353.png

When I commented that line out, the code ran, but the new file was saved without a file extension. I added .xlsx manually, and it opened fine.

When I replaced the line above with the code below, the full code ran properly.
Code:
Call DeleteNames 
----------
Sub DeleteNames()
'modified from: https://www.mrexcel.com/board/threads/vba-remove-all-named-ranges.1052604/post-5065838
Dim RgeName As Name

On Error Resume Next
For Each RangeName In Names
    ActiveWorkbook.Names(RangeName.Name).Delete
Next
On Error GoTo 0
End Sub
Does that mean that there's a name your code can't delete, or that it needs error handling?

Thank you again for your time and patience. I'm learning a lot!
 
Maybe something 'weird' on your real workbook as I have no issue with your attachment …​
You can try my codeline as it is between both On Error codelines.​
 
Back
Top