I am using the following to get the sheet codename, but I would like to get just the index of that codename. Lets say the codename is "Sheet4" I would like to get just the number 4. If it's "Sheet12" I would like just the 12.
ActiveSheet.CodeName
Thanks!
这很简单:
ActiveSheet.Index
Okay to extract the Numbers of a String they are basically two ways. First you could write a Function which will extract all nummeric chars from your string:
Function ExtractNumericChars(sInput As String) As String
Dim i As Integer
Dim sResult As String
Dim sChr As String
For i = 1 To Len(sInput)
sChr = Mid(sInput, i, 1)
If IsNumeric(sChr) Then
sResult = sResult & sChr
End If
Next
removeBadChars = sResult
End Function
This works fine for your case. The more elegant way would be using a Regex. This would Need the reference to "Microsoft VBScript Regular Expressions 5.5". The Advantage is, that you basically can match anything with regex. In your case it would look like the following:
Dim regEx As New RegExp
regEx.Pattern = "[^\d]+"
Debug.Print regEx.Replace(ActiveSheet.CodeName, "")
I managed to get a function which gives me the sheet based on its codename.
Function GetSheetWithCodename(ByVal worksheetCodename As String, Optional wb As Workbook) As Worksheet
Dim iSheet As Long
If wb Is Nothing Then Set wb = ThisWorkbook ' mimics the default behaviour
For iSheet = 1 To wb.Worksheets.Count
If wb.Worksheets(iSheet).CodeName = worksheetCodename Then
Set GetSheetWithCodename = wb.Worksheets(iSheet)
Exit Function
End If
Next iSheet
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.