"Run-time error '9' Subscript out of Range" when trying to copy Sheet name to an array element

bdrumr56

New Member
Joined
Aug 25, 2011
Messages
4
Hello all:

First post. I'm using VBA in Excel 2007. I have been trying to code a subroutine to find the last sheet and the sheet names of the tabs in a workbook. Then I'm attempting to place those sheet names in an array.

I suspect that I'm not using the right class of object or the right method, but here is the code:

Code:
Sub PopulateArrayWithSheetNames()
    Dim LastAddedSheet As Worksheet
    Dim oneSheet As Worksheet
    Dim My_sheetNames() As String
    Dim i As Long
    Dim rngThisWorkbook As Range
    Dim wrkbkName As String
    
    
    i = 0
    wrkbkName = "TEST901GOODA.xlsm"
    Workbooks(wrkbkName).Activate
    
    With ThisWorkbook
        Set LastAddedSheet = .Sheets(1)
        For Each oneSheet In .Sheets
            i = i + 1
            If Val(Mid(oneSheet.CodeName, 6)) > Val(Mid(LastAddedSheet.CodeName, 6)) Then
                Set LastAddedSheet = oneSheet
            End If
            
            'Copy OneSheet(SheetName) to an Array Value(i)
            MsgBox "My sheet is named: " & oneSheet.Name
            MsgBox "My sheet's Codename is: " & oneSheet.CodeName
            'My_sheetNames((i)) = oneSheet.CodeName
            My_sheetNames(1) = oneSheet.Name
        
        'i = i + 1
        Next oneSheet
    End With
    MsgBox LastAddedSheet.Name & " was last added."
End Sub
Can someone give me a hand?

Thanks in advance!

Bdrumr56
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the board.

Here's one way. It assumes that the codenames of the worksheets have not been changed from their default Sheet# names.

Code:
Sub PopulateArrayWithSheetNames()
    Dim iSht        As Long
    Dim asSht()     As String
    Dim iNum        As Long
    Dim iNumMax     As Long
    Dim wks         As Worksheet
 
    With ThisWorkbook
        ReDim Preserve asSht(1 To .Worksheets.Count, 1 To 2)
        For iSht = 1 To .Worksheets.Count
            asSht(iSht, 1) = .Worksheets(iSht).Name
            asSht(iSht, 2) = .Worksheets(iSht).CodeName
            iNum = Mid(asSht(iSht, 2), 6)
            If iNum > iNumMax Then
                iNumMax = iNum
                Set wks = .Worksheets(iSht)
            End If
        Next iSht
        MsgBox wks.CodeName & "(" & wks.Name & ") was last added (maybe)."
    End With
End Sub
 
Upvote 0
Thanks, shg! Tested it and it works fine.

Some of the CodeNames have changed, but I can access those data files and change the CodeNames back.

This looks like what I'm needing.

Now is there a way to use those saved Names or Codenames to activate the particular sheet and pull a range of data from it to copy to a sheet in another workbook?

I think I'll be able to figure that part out on my own, but any thoughts would be appreciated. After all, I realize that is carrying things a lot farther.

Thanks, again. The "Subscript out of range" error was driving me crazy!

bdrumr56
 
Upvote 0
You're welcome, good luck.
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,318
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top