finding the right character length

mario92

New Member
Joined
Oct 16, 2011
Messages
43
Office Version
  1. 365
Platform
  1. Windows
Hi,

Im having trouble creating code to read off the name of my excel tabs. the names always start w/ 2 blank spaces and then they can be 2, 3, or 4 letters after it and then followed by a space. I need the code to discern if the name of the tab has a certain amount of letters to continue my code.

right now i have this and a few on error goto(s) to try to find it out such as tname = Mid(SheetName, 3, 3) or tname = Mid(SheetName, 3, 2)

SheetName = ActiveWorkbook.Worksheets(I).Name
tname = Mid(SheetName, 3, 4)
ActiveWorkbook.Worksheets(I).Activate


im sure there is a better way to do this probably with if statements or a switch. can anyone provide any suggestions?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this:
VBA Code:
    SheetName = ActiveWorkbook.Worksheets(I).Name
    
    Dim arr() As String
    arr = Split(Trim(SheetName), " ")
    tname = arr(0)
 
Upvote 0
Solution
In the n you have the number of characters

VBA Code:
Sub countletters()
  Dim n As Long, i As Long
  
  For i = 1 To Sheets.Count
    n = Len(Trim(Sheets(i).Name))
    MsgBox n
  Next
End Sub
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,215,883
Messages
6,127,542
Members
449,385
Latest member
KMGLarson

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