Sheet Number in VBA

Sheet Number in VBA

stuartmp
Advocate Advocate
2,311 Views
7 Replies
Message 1 of 8

Sheet Number in VBA

stuartmp
Advocate
Advocate
Hi All
I was just wondering if it is possible to get the current sheet number through VBA?

Thanks
0 Likes
2,312 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

The sheet number is appended to the sheet name in
the browser.  You can get this name using the Name property of the Sheet
object and then extract the number from the name.  Here's an example of a
function that does this.

 

Public Function GetSheetNumber(ByVal Sheet
As Inventor.Sheet) As Integer
    Dim strSheetName As
String
    strSheetName = Sheet.Name
   

    If InStr(strSheetName, ":")
Then
        ' Extract the sheet number
from the name.
        GetSheetNumber =
CInt(Right$(strSheetName, Len(strSheetName) - InStrRev(strSheetName,
":")))
    Else
       
' This sheet is not numbered so return
zero.
        GetSheetNumber =
0
    End If
End Function



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hi
All I was just wondering if it is possible to get the current sheet number
through VBA? Thanks
0 Likes
Message 3 of 8

stuartmp
Advocate
Advocate
Thanks Brian.

I originally had.

Dim SheetNumber as string
SheetNumber = Mid(Trim(oActiveSheet.Name), 1 + InStrR(Trim(oActiveSheet.Name), ":", vbTextCompare))


Function InStrR(ByVal sTarget As String, ByVal sFind As String, ByVal iCompare As Long) As Long

Dim P As Long, LastP As Long, Start As Long

P = InStr(1, sTarget, sFind, iCompare)

Do While P

LastP = P

P = InStr(LastP + 1, sTarget, sFind, iCompare)

Loop

InStrR = LastP

End Function


' I did not realize until I just tried it that you can not override the sheet number manually in the browser. It does not accept you renaming it to
Test:2 for example. I guess I should have tried it first. I also did not know there was a standard function for searching strings in reverse
I was just using one that I got a few year ago

Thanks for effort you put into the GetSheetNumber function. I will add it to my collection

Kind regards

Stuart Penberthy
0 Likes
Message 4 of 8

stuartmp
Advocate
Advocate
Please delete Edited by: stuartmp on Oct 14, 2008 4:55 PM
0 Likes
Message 5 of 8

Anonymous
Not applicable

Mid(oSheet.Name, InStr(1, oSheet.Name, ":") +
1)

 

Joe ...

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hi
All I was just wondering if it is possible to get the current sheet number
through VBA? Thanks
0 Likes
Message 6 of 8

jR0sal3s
Advocate
Advocate

yes...

 

Public Sub x()

'The sheet number is appended to the sheet name in _
the browser. You can get this name using the Name _
property of the Sheet object and then extract the number from the name. *Brian Ekins

Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim NoOfSheets As Integer
Dim CurrentSheet As Integer

NoOfSheets = oDoc.Sheets.Count

Dim strSheetName As String
strSheetName = oDoc.ActiveSheet.Name

If InStr(strSheetName, ":") Then
' Extract the sheet number from the name.
GetSheetNumber = CInt(Right$(strSheetName, Len(strSheetName) - InStrRev(strSheetName, ":")))
Else

'This sheet is not numbered so returnzero.
GetSheetNumber = 0
End If

CurrentSheet = GetSheetNumber

Debug.Print CurrentSheet
Debug.Print NoOfSheets

End Sub

0 Likes
Message 7 of 8

JamieVJohnson2
Collaborator
Collaborator

Isn't the sheet number the same as the document.sheets index value?  If so;

dim sheetNo as integer = 0

For index as Integer = 1 to document.sheets.count

if documents.sheets(index).name = activesheet.name then

'sheet number found it is index.

sheetNo = index

exit for

end if

next

 

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 8 of 8

jR0sal3s
Advocate
Advocate

i am not quiet sure about that...but the solution posted by Brian Ekins seems to work fine...

0 Likes