Consulting

Results 1 to 18 of 18

Thread: Solved: Code from a book

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Solved: Code from a book

    When ever I type code from a book it never works. I type it exactly as printed in the book. I found some code that will change my highlighted selection into Proper case but it is giving me a Sub or Function not defined on the Proper.

    [VBA]Sub ConvertToProper()
    Dim cellObject As Range
    For Each cellObject In Selection
    cellObject.Formula = WorksheetFunction(Proper(cellObject.Formula))
    Next

    End Sub[/VBA]

  2. #2
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    You must also define the Proper function, which looks like a user-defined function and not an Excel built-in one.

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Isn't proper a Function? It is to set Proper Case.

  4. #4
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Sorry I did not know this function. You must have copied your formula wrong or your book is bad. Try this code instead:


    [VBA]
    Sub ConvertToProper()
    Dim cellObject As Range
    For Each cellObject In Selection
    cellObject.Formula = WorksheetFunction.Proper(cellObject.Formula)
    Next

    End Sub
    [/VBA]

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    jungix's code works for me
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Djblois
    When ever I type code from a book it never works. I type it exactly as printed in the book. I found some code that will change my highlighted selection into Proper case but it is giving me a Sub or Function not defined on the Proper.

    [vba]Sub ConvertToProper()
    Dim cellObject As Range
    For Each cellObject In Selection
    cellObject.Formula = WorksheetFunction(Proper(cellObject.Formula))
    Next

    End Sub[/vba]
    Check your book, I'll bet that the line

    [vba]

    cellObject.Formula = WorksheetFunction(Proper(cellObject.Formula))
    [/vba]

    is actually written as

    [vba]

    cellObject.Formula = WorksheetFunction.Proper(cellObject.Formula)
    [/vba]

  7. #7
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Thank you for repeating what I just said

    Djblois you could mark this thread as solved.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by jungix
    Thank you for repeating what I just said
    Well your first answer was incomprehensible, and the second came closer, so I thought it best to be clear and specific.

    Quote Originally Posted by jungix
    Djblois you could mark this thread as solved.
    If he thinks it is solved he could, but that is for him to determine.

  9. #9
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    How do I mark it solved????

  10. #10
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Quote Originally Posted by xld
    Check your book, I'll bet that the line

    [vba]

    cellObject.Formula = WorksheetFunction(Proper(cellObject.Formula))
    [/vba]

    is actually written as

    [vba]

    cellObject.Formula = WorksheetFunction.Proper(cellObject.Formula)
    [/vba]
    No I double checked it, it is typed as

    [vba]

    cellObject.Formula = WorksheetFunction(Proper(cellObject.Formula))
    [/vba]

  11. #11
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Go to thread tools, and tick solved.

    Your book is wrong then, because the correction I suggested worked on my computer whereas yours gave me an error.

    xld, you're right my first message was wrong, but the second said exactly the same as yours

  12. #12
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I know you were correct. Your code worked, so I corrected it in my book.
    Thank you,
    Daniel

  13. #13
    I might add that instead of using the lengthy "WorksheetFunction", you can use:
    [vba]Msgbox Application.Proper("abcdef")[/vba]

  14. #14
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Or... Msgbox StrConv("abcdef", vbProperCase) ... (as we are working in visual basic in this case and there IS a visual basic function for this)
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Cyberdude
    I might add that instead of using the lengthy "WorksheetFunction", you can use:
    [vba]Msgbox Application.Proper("abcdef")[/vba]
    Dude,

    sometimes it is appropriate to use Application, sometimes you have to use WorksheetFunction. I tend to use Application as it is easier to test for success IMO. I once wrote a tome on the subject, sad isn't it?

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Djblois
    No I double checked it, it is typed as

    [vba]

    cellObject.Formula = WorksheetFunction(Proper(cellObject.Formula))
    [/vba]
    Really? Which book is it? Have you informed the publisher?

  17. #17
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    About 100 years ago I started writing macros in WordPerfect for DOS. For anyone who has dabbled in these, you may remember the dreaded ~ character that indicated the end of built-in functions. This was the source of great heartache for new (and not-so-new) developers, in making sure that they were in the right position.

    I remember seeing a book published on WP macros that did not have a SINGLE ~ character in any of the code samples. Near the start of the book was a statement along the lines of insert the ~ character where appropriate ...

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Even the Help files get it wrong. Have you come across this teaser?

    The following code illustrates how to create a Dictionary object:
    [VBA]Dim d 'Create a variable
    Set d = CreateObject(Scripting.Dictionary)
    d.Add "a", "Athens" 'Add some keys and items
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"
    [/VBA]
    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
  •