Excel/rewriting VBA

Advertisement


Question
Hi Tom

I asked you a question earlier today without sending all of the relevant information.  I am trying to run these two pieces of code in “ThisWorkbook”.  



Private Sub Workbook_SheetBeforeDelete(ByVal sh As Object)
   Call RemoveHyperFromIndexPage(sh.Name)
End Sub

Private Sub Workbook_Open()
  ThisWorkbook.Worksheets("StartSheet").Activate
   Call RemoveHyperFromIndexPage("StartSheet")
End Sub


I am getting an error message.  The other piece of related code is:


Sub RemoveHyperFromIndexPage(ByVal RemSheetName As String)
   Dim shpClass    As Shape
   Dim shIndex     As Worksheet
   
   Set shIndex = Sheets("Index page")
   shIndex.Select
   For Each shpClass In ActiveSheet.Shapes
       If shpClass.TextFrame.Characters.Text = RemSheetName Then
         shpClass.TextFrame.Characters.Text = ""
         shpClass.Select
         ActiveSheet.Hyperlinks.Delete
         ActiveSheet.[A1].Select
         Exit For
       End If
   Next
End Sub

Could you help me with this issue?

Chris Mitchell

Answer
Christopher Mitchell,

The next statement if factual - it isn't meant to be flippant:
saying I got an error is about as descriptive as saying the sun came up today.


I would guess that you need to move  

Sub RemoveHyperFromIndexPage(ByVal RemSheetName As String)
  Dim shpClass    As Shape
  Dim shIndex     As Worksheet
  
  Set shIndex = Sheets("Index page")
  shIndex.Select
  For Each shpClass In ActiveSheet.Shapes
      If shpClass.TextFrame.Characters.Text = RemSheetName Then
        shpClass.TextFrame.Characters.Text = ""
        shpClass.Select
        ActiveSheet.Hyperlinks.Delete
        ActiveSheet.[A1].Select
        Exit For
      End If
  Next
End Sub

to a general module (insert=>Module in the Visual Basic Editor).


That should make it visible to all the other routines in your workbook including the class modules associated with your events.  (so I am assuming the Workbook_Open event does not recognize the procedure name  RemoveHyperFromIndexPage because it is located in the Workbook_SheetBeforeDelete class module.

Also, this routine makes "Index page" the activesheet.  So you might want to reverse the order in your Open Event.

Private Sub Workbook_Open()
  Call RemoveHyperFromIndexPage("StartSheet")
  ThisWorkbook.Worksheets("StartSheet").Activate
End Sub


a possible alternative would be to try calling it like this:

Call SheetBeforeDelete.RemoveHyperFromIndexPage("StartSheet")

or if that still has problems then

Call Thisworkbook.SheetBeforeDelete.RemoveHyperFromIndexPage("StartSheet")

if none of that helps, then you would have to identify what line is raising the error.  If the routine is in a class module, then it probably won't show you the offending line unless you go into the vbe under Tools=>Options=>General Tab and under the Error Handling section ensure Break in Class Module is selected.

--
Regards,
Tom Ogilvy



--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.