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:
Can someone give me a hand?
Thanks in advance!
Bdrumr56
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
Thanks in advance!
Bdrumr56