Results 1 to 10 of 10

Macro to find and replace links in hidden worksheets

Threaded View

  1. #1
    Registered User
    Join Date
    08-19-2012
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2010
    Posts
    62

    Macro to find and replace links in hidden worksheets

    Hello,
    I'd really appreciate if someone can help me to fix my code so that it works.

    Here's a summary of what the macro is supposed to do:
    1. It unhides all sheets
    2. It unprotects all sheets
    3. It unprotects the workbook
    4. If there any errors during any time, it goes to an error handler so that the workbook and sheets aren't left unprotected or certain sheets unhidden.

    5. It does several find and replaces to fix links within cells of the entire workbook and all sheets.

    6. It re-hides all sheets
    7. It re-protects all sheets
    8. It re-protects the workbook

    Here is my code, please help if you can...Thank you very much!!

    Sub Fix_All_Links()
    '
    ' Fix_All_Links Macro
    'Application.EnableCancelKey = xlErrorHandler
    'On Error GoTo ErrHandl
    
    
    'Turn off screen updating.
     Application.ScreenUpdating = False
    
    
    'Unprotect Workbook temporarily
    ActiveWorkbook.Unprotect Password:="ABC"
    
    'Unprotect All Sheets & unhide all sheets temporarily
    Dim wSheet As Worksheet
    
    
        For Each wSheet In Worksheets
        wSheet.Visible = xlSheetVisible
            wSheet.Unprotect Password:="ABC"
        Next wSheet
        
    
        
    'CODE TO REPLACE ALL HARD LINKS WITH NOTHING
    On Error Resume Next
    
    Cells.Replace What:= _
            "'C:\Program Files\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
    
    
        Cells.Replace What:= _
            "'C:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
        
            Cells.Replace What:= _
            "'D:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'E:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'F:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'G:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'H:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'I:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'J:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'K:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'L:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'M:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'N:\SMF\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
    
     Cells.Replace What:= _
            "'C:\Program Files\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
    
    
        Cells.Replace What:= _
            "'C:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
        
            Cells.Replace What:= _
            "'D:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'E:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'F:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'G:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'H:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'I:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'J:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'K:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'L:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'M:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
                Cells.Replace What:= _
            "'N:\SMF Add-In\RCH_Stock_Market_Functions.xla'!", Replacement _
            :="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
            
    On Error Resume Next
        
    '----------------------------
        
        On Error GoTo ErrHandl
        
        
    'Reprotect All Sheets
        For Each wSheet In Worksheets
            wSheet.Protect Password:="ABC"
        Next wSheet
    
    
    'ReHide Specific Sheets
        Sheets("Formulas").Select
            ActiveWindow.SelectedSheets.Visible = False
        Sheets("EPS").Select
            ActiveWindow.SelectedSheets.Visible = False
        Sheets("PE Ratio").Select
            ActiveWindow.SelectedSheets.Visible = False
        Sheets("ROE").Select
            ActiveWindow.SelectedSheets.Visible = False
        Sheets("ROCI").Select
            ActiveWindow.SelectedSheets.Visible = False
        Sheets("GPM").Select
            ActiveWindow.SelectedSheets.Visible = False
        Sheets("ABC Values").Select
            ActiveWindow.SelectedSheets.Visible = False
        Sheets("Pictures").Select
            ActiveWindow.SelectedSheets.Visible = False
        Sheets("InvestPData").Select
            ActiveWindow.SelectedSheets.Visible = False
    
    
      'Reprotect Workbook
        ActiveWorkbook.Protect Password:="ABC"
    
        
    'Turn off screen updating.
     Application.ScreenUpdating = True
     
     'Save Workbook
    ActiveWorkbook.Save
            
    'ReAllow Esc button to cancel processes
    Application.EnableCancelKey = xlInterrupt
        
        
    'Error Handler
    ErrHandl:
    'Rehide specific sheets
    Sheets("Formulas").Visible = xlSheetHidden
        Sheets("EPS").Visible = xlSheetHidden
        Sheets("PE Ratio").Visible = xlSheetHidden
        Sheets("ROE").Visible = xlSheetHidden
        Sheets("ROCI").Visible = xlSheetHidden
        Sheets("GPM").Visible = xlSheetHidden
        Sheets("ABC Values").Visible = xlSheetHidden
        Sheets("Pictures").Visible = xlSheetHidden
            Sheets("InvestPData").Visible = xlSheetHidden
    
    'Protect All Sheets
        For Each wSheet In Worksheets
            wSheet.Protect Password:="ABC"
        Next wSheet
        
    'Protect Workbook
        ActiveWorkbook.Protect Password:="ABC"
    
    'Turn off screen updating.
     Application.ScreenUpdating = True
     
     Application.EnableCancelKey = xlInterrupt
    
    'Show Error Message
    MsgBox "Links could not be updated. Please contact SOMEBODY @ EMAIL ADDRESS for help."
    
    End Sub
    Last edited by lottidotti; 02-09-2013 at 11:59 AM. Reason: updated code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1