Solved

Delete a certain name in all workbooks in a specific folder

Posted on 2012-03-24
3
272 Views
Last Modified: 2012-03-25
Dear Experts:

I got a - workbook level scope -  name called 'MyNamedRange' in more than 200 xlsx-workbooks that all reside in the same folder C:\ExcelFolder\

I would like to run a macro that deletes this 'workbook level scope' name in each and every workbook in one go.

Help is much appreciated. Thank you very much in advance.

Regards,  Andreas
0
Comment
Question by:AndreasHermle
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
ID: 37761570
As requested, the starting point is on the initial sheet, but you are prompted for the directory in case you'd like to either confirm/change it, then all files are opened, the workbook scoped name is deleted if it exists, then the file is closed and saved in the loop.

Here's the code:
Option Explicit
Sub deleteNamedRangeFilesInFolder()
Dim dialogFile As FileDialog
Dim strpath As String
Dim fName As String
Dim wkb As Workbook
Dim wks As Worksheet
Dim rgName As String
Dim myName As Name
Dim wkbProcess As Workbook
Dim wksProcess As Worksheet
Dim fPathFname As String

    Set wkb = ThisWorkbook
    Set wks = wkb.Sheets("Control Panel")
    
    strpath = IIf(Right([folderPath], 1) = "\", [folderPath], [folderPath] & "\")
    rgName = [rngName]
    
    ' Open the file dialog
    Set dialogFile = Application.FileDialog(msoFileDialogFolderPicker)
    With dialogFile
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .InitialFileName = strpath
        .Title = "Select Folder for Processing"
        .Show
    End With
    If dialogFile.SelectedItems.Count > 0 Then
        strpath = dialogFile.SelectedItems(1)
        
        fName = Dir(strpath & "\*.xlsx")
        If fName <> "" Then
        
            Application.EnableEvents = False
            Application.DisplayAlerts = False
            
            Do
                fPathFname = strpath & "\" & fName
                Application.StatusBar = "Processing File: " & fPathFname
                Set wkbProcess = Workbooks.Open(Filename:=fPathFname)
                
                On Error Resume Next
                wkbProcess.Names(rgName).Delete
                On Error GoTo 0
                
                wkbProcess.Close savechanges:=True
                
                fName = Dir()
                
            Loop Until fName = vbNullString
        End If
            
    End If
    
    MsgBox "Process Complete!"
        
gracefulExit:
    'cleanup
    Set dialogFile = Nothing
    Application.StatusBar = False
    Application.EnableEvents = True
    Application.DisplayAlerts = True
End Sub

Open in new window


See attached.

Dave
delRangeNameFromFiles-r1.xlsm
0
 

Author Comment

by:AndreasHermle
ID: 37763002
Wow, I am really impressed Dave! Great job, very sophisticated coding. I have tested it several times with different folders and files. It works just fine.

Thank you very much for your time taken, your swift support and your more than professional work.

This forum deserves its name. There is no doubt about it.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 37763011
Again, this forum is more than one can dream of.

Great job, Dave!

Regards, Andreas
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now