Consulting

Results 1 to 9 of 9

Thread: Solved: Extracting just the Number in a name

  1. #1
    VBAX Regular
    Joined
    Mar 2008
    Posts
    32
    Location

    Solved: Extracting just the Number in a name

    Hey guys. So I set up a code to mine data out of multiple workbooks. I pull the data from each workbook and put it into a new sheet within a single workbook. I then take the data and create a summary with all the data from all worksheets in a single table on one worksheet. I need to be able to use the number out of the worksheet titles as the header for each dataset in the table.

    Worksheet titles are as follows:
    HK284_SD_Depth_of_Focus
    HK293_SD_Depth_of_Focus
    HK226_SD_Depth_of_Focus ... etc

    I hope this makes sense. the only part I need help with is getting the 3 digit number from the worksheet name into a stored value in VB.

    Thanks!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The VAL function should do this.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Mar 2008
    Posts
    32
    Location
    ok I've tried the following and none work

    curSN = Val(activeworkbook.name)
    Problem: returne 0 (probably because the first to characters in the name are letters

    curSN = val(mid(activeworkbook.name))
    Problem: does not accept Val and Mid together

    I've also tried:
    curSN = val(string(...etc

    any ideas?

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    It failed with me too, but it SHOULD work!!!

    If no-one comes up with a better solution, it can be done by checking each ascii value in the string.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Hi

    If there's always two letters at the front then you could use:

    curSN = val(mid(activesheet.name,3))
    I changed to Activesheet.Name as I wasn't sure if you were working with sheets or workbook names - you can always amend back.

    Richard

  6. #6
    VBAX Regular
    Joined
    Mar 2008
    Posts
    32
    Location
    either works because the sheet name is the same as the workbook name. the previous code generated the two to be identical.

    I'll give this a shot. I was wondering about the ",3" portioin of the code because I used it elsewhere before but didnt know the precise function of it.

  7. #7
    VBAX Regular
    Joined
    Mar 2008
    Posts
    32
    Location
    Success!
    all it needed was the 'start as long' position.

    Thanks guys!

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If not, this trivial formula does it

    [vba]

    Const BASE_FORMULA As String = _
    "MID(""<val>"",MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},""<val>0123456789""))," & _
    "SUMPRODUCT(LEN(""<val>"")-LEN(SUBSTITUTE(""<val>"",{0,1,2,3,4,5,6,7,8,9},""""))))"

    wsTitle = "HK284_SD_Depth_of_Focus"
    MsgBox ActiveSheet.Evaluate(Replace$(BASE_FORMULA, "<val>", wsTitle))

    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    It failed with me too, but it SHOULD work!!!
    I should read the Help in more detail!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •