Results 1 to 4 of 4

Run Time Error 9

Threaded View

  1. #1
    Registered User
    Join Date
    01-25-2010
    Location
    West Midlands, England
    MS-Off Ver
    Excel 2007
    Posts
    14

    Run Time Error 9

    Hi All

    I'm pretty new to VBA and have run into a stubbon error when working on a textbook example.

    The sample code is supposed to write settings from an add-in to the User Interface Worksheets.

    The error is 'Run Time Error 9, Subscript Out of Range'

    Any help with this would be greatly appreciated.

    (Excel 2007 & XP Pro)

    Mark


    The code is:


    Option Explicit
    
    Private Const msFILE_TEMPLATE As String = "testworkbook.xltx"
    Private Const msRNG_NAME_LIST As String = "tblRangeNames"
    Private Const msRNG_SHEET_LIST As String = "tblSheetNames"
    
    
    Public Sub WriteSettings()
    
        Dim rngSheet As Range
        Dim rngSheetList As Range
        Dim rngName As Range
        Dim rngNameList As Range
        Dim rngSetting As Range
        Dim sSheetTab As String
        Dim wkbTemplate As Workbook
        Dim wksSheet As Worksheet
        
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        Set wkbTemplate = Application.Workbooks(msFILE_TEMPLATE)
        
        Set rngSheetList = UISettings.Range(msRNG_SHEET_LIST)
        
        Set rngNameList = UISettings.Range(msRNG_NAME_LIST)
        
        For Each rngSheet In rngSheetList
            
            sSheetTab = sSheetTabName(wkbTemplate, rngSheet.Value)
            Set wksSheet = wkbTemplate.Worksheets(sSheetTab) <Run Time Error 9 HERE>
            
            For Each rngName In rngNameList
            
                Set rngSetting = Intersect(rngSheet.EntireRow, _
                                                rngName.EntireColumn)
                                                
                If Len(rngSetting.Value) > 0 Then
                    wksSheet.Names.Add rngName.Value, _
                                                "=" & rngSetting.Value
                End If
                
            Next rngName
            
        Next rngSheet
        
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        
    End Sub
    
    
    
    Public Function sSheetTabName(ByRef wkbProject As Workbook, _
                               ByRef sCodeName As String) As String
                    
        Dim wksSheet As Worksheet
    
        For Each wksSheet In wkbProject.Worksheets
            If wksSheet.CodeName = sCodeName Then
                sSheetTabName = wksSheet.Name
                Exit For
            End If
        Next wksSheet
    End Function
    Last edited by Leith Ross; 11-20-2011 at 02:14 PM. Reason: Changed HTML Tags to Code Tags

Thread Information

Users Browsing this Thread

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

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