+ Reply to Thread
Results 1 to 8 of 8

Beginner VBA error

  1. #1
    Registered User
    Join Date
    12-11-2017
    Location
    Healdsburg, California
    MS-Off Ver
    2016
    Posts
    3

    Beginner VBA error

    So, I often get issues like this when I grab a macro from online and I'm not sure what the problem is. It's actually a UDF and the code is pasted below, followed by a description of the error. Any ideas what the issue is here? Any help is greatly appreciated.

    'This VBA code will create a function to get the numeric part from a string
    Function GetNumeric(CellRef As String)
    Dim StringLength As Integer
    StringLength = Len(CellRef)
    For i = 1 To StringLength
    If IsNumeric(Mid(CellRef, i, 1)) Then Result = Result & Mid(CellRef, i, 1)
    Next i
    GetNumeric = Result
    End Function

    The error is:

    Compile error:

    Variable not defined

    The in-code error highlighting is:

    Highlights the first line in yellow (with the little arrow). And highlights the first i (on line 4)

  2. #2
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Beginner VBA error

    Why not using a formula?

    Please Login or Register  to view this content.
    Is an array formula, so Ctrl+Shift+Enter

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,283

    Re: Beginner VBA error

    Please use Code Tags when posting code (Forum Rule #3)
    Add this to the function.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Beginner VBA error

    This is the "information age" not the "good information age". Most of the code you will find online is either written by people helping someone else and they are going to write it as short as possible to save time and/or it was written to accomplish a very specific task in a very specific context which may (almost 100% never) not match your needs. VBA is not one size fits all.

    Generally when you find code online (or even when you record code) you need to dissect it, pull out what you need and/or re-write portions of it to meet your needs. There are often things I add to code I find to complete it like declarations, constants, comments, and error handling.

    Its also important to know how old the code is and what version of Excel it was meant to work with. When I write my own code I always comment it to include the date and what version I made it in. Often going back to my own code (or code you find online) it is outdated compared to what version of Excel you may now be using. Sometimes its a simple fix sometimes it requires a new approach.

    If you are a programmer yourself or aspiring to be a VBA programmer then you are better off using online code as an example and learning how/why it works, then writing it yourself to match your own style and your existing code. I often dig up code online to do something and find someone else doing it a way I would not have thought of. I dont however take it at face value, but instead try and understand why it works, what conditions it may not work under, test it and then incorporate it into my code by writing it to match how I write code myself. I may only use portions of it, or adopt its approach but in another way.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  5. #5
    Registered User
    Join Date
    12-11-2017
    Location
    Healdsburg, California
    MS-Off Ver
    2016
    Posts
    3

    Re: Beginner VBA error

    I believe you may be replying to the wrong thread.

    Quote Originally Posted by PaulM100 View Post
    Why not using a formula?

    Please Login or Register  to view this content.
    Is an array formula, so Ctrl+Shift+Enter

  6. #6
    Registered User
    Join Date
    12-11-2017
    Location
    Healdsburg, California
    MS-Off Ver
    2016
    Posts
    3

    Re: Beginner VBA error

    I, of course, will try it right away. Actually, I've found this to be a common fix to these issues. I just couldn't remember!

    Quote Originally Posted by bakerman2 View Post
    Please use Code Tags when posting code (Forum Rule #3)
    Add this to the function.
    Please Login or Register  to view this content.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,283

    Re: Beginner VBA error

    Glad to help. Thanks for rep+.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  8. #8
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371
    Design everything to be as simple as possible, but no simpler.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner needs help! "Run-time error 438" while attempting to format multiple series
    By elodiePhD in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-14-2016, 06:10 AM
  2. [SOLVED] Quick beginner question-Why doesn't On Error Exit Function work?
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-09-2013, 06:23 AM
  3. Beginner
    By ruhat145 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 12-01-2012, 10:05 PM
  4. Beginner in VBA
    By neeraj_joshi48 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-01-2012, 06:58 AM
  5. Beginner
    By Mardikas09 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-11-2009, 05:59 AM
  6. macro beginner error
    By andreas_d in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-27-2007, 09:47 AM
  7. Beginner help! error no return() or halt() function found on macro sheet
    By steppin16 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2006, 03:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1