+ Reply to Thread
Results 1 to 8 of 8

VBA Compile Error in Excel

  1. #1
    Registered User
    Join Date
    03-01-2016
    Location
    SD, USA
    MS-Off Ver
    2016
    Posts
    4

    VBA Compile Error in Excel

    Hello everyone,

    I'm *extremely* new to VBA programming (just started about an hour ago), though i'm well versed in other languages.

    I play a game called Dungeons and Dragons with some friends, and I was looking into an integrated solution for rolling a bunch of dice (-- i.e. 36d6 (36 - 6 sided dice)) onto our character sheets, which are in excel.

    I figured that the most elegant solution would be to have a macro that would read a string, and then take from it what it needs to make the appropriate calculation.

    WHAT I WOULD LIKE THE PROGRAM TO DO:

    read a string from a cell, and parse the necessary information, being stored into the appropriate variables (num - loop control, size - max of Randbetween)
    use a FOR loop to generate a number between 1 and max
    add generated number to 'answer' variable
    iterate until completion
    return answer to given cell

    '=======EXAMPLE======='

    string to read: 2d6
    num = 2
    max = 6
    answer = 0
    tmp = 0

    'loop num times to generate numbers between 1 and max'

    first iteration: tmp = 3
    answer = answer + tmp

    second iteration: tmp = 5
    answer = answer + tmp

    'loop exits'

    return answer 'to given cell'

    '=======END EXAMPLE======='

    I apologize up front if I overcomplicated either my attempt at a solution, or the phrasing of my inquiry.

    My code:
    Please Login or Register  to view this content.
    The error I receive when attempting to run my code:
    VBA error.png
    Last edited by mscheuvront; 03-01-2016 at 05:07 PM.

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: VBA Compile Error in Excel

    1. I'm assuming you're wanting to do this on a range of cells instead of just S14, but i didn't see the range specified
    2. You mention twice you want the answer outputted to a cell, but don't specify which cell, and your code shows a message box


    So This code does what you tried to do in your example. If you can't figure out how to modify to suit your needs, then please post an example workbook that shows an example data set.

    Please Login or Register  to view this content.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: VBA Compile Error in Excel

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    03-01-2016
    Location
    SD, USA
    MS-Off Ver
    2016
    Posts
    4

    Re: VBA Compile Error in Excel

    1. I'm assuming you're wanting to do this on a range of cells instead of just S14, but i didn't see the range specified
    I'm not trying to use a range of cells. The goal is to have a single cell, S14, containing a string of text (4d6 for example) and then using the macro to output to S15

    You mention twice you want the answer outputted to a cell, but don't specify which cell, and your code shows a message box
    I mention it, because that would be something that's nice to have, though the message box was my temporary solution, due to not knowing what exactly I needed to do to accomplish what I was after.

    ...If you can't figure out how to modify to suit your needs, then please post an example workbook that shows an example data set.
    I apologize. I'll do so in the future.

  5. #5
    Registered User
    Join Date
    03-01-2016
    Location
    SD, USA
    MS-Off Ver
    2016
    Posts
    4

    Re: VBA Compile Error in Excel

    Quote Originally Posted by InvisibleMan View Post
    Please Login or Register  to view this content.
    Would you mind explaining to me why this works? I'm a little confused with just tying to eyeball it.

  6. #6
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: VBA Compile Error in Excel

    Sorry if you interpreted my tone as negative. Was just trying to be clear what my code would/would not do.

    Anyway, try this code. Should do what you want.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    03-01-2016
    Location
    SD, USA
    MS-Off Ver
    2016
    Posts
    4

    Re: VBA Compile Error in Excel

    Thank you very much! I'll go ahead and mark my thread as solved!

  8. #8
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: VBA Compile Error in Excel

    You have to refer to a range as, for example, Range("S14"), not just S14. You can't use Find, that's an Excel worksheet function ... so use Instr instead. Again, RandBetween is a worksheet function, but you can use it if you say: Application.Worksheetfunction.RandBetween. Left and Right are text functions so I've used CInt to ensure the result is converted to Integer. And the Right formula wasn't quite right so that was adjusted.

    But using Split is a much neater way of separating the values.

    Note that [S14] is a shorthand way of referring to Range("S14"). Got to admit, it's not a shortcut I choose to use.

+ 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. Compile error in Excel
    By MBA2004 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-03-2015, 11:01 PM
  2. Excel macro (compile error. syntax error.) error
    By salar_younis in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-06-2014, 06:11 AM
  3. [SOLVED] Compile Error in Hidden Module and Compile Error: Can't find project or library
    By Taislin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-10-2013, 07:03 PM
  4. [SOLVED] VBA, Excel run-time error '381' and now a Compile Error
    By eemiller1997 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2012, 12:46 PM
  5. Compile Error in excel 2010
    By molesy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-14-2012, 10:13 AM
  6. Replies: 3
    Last Post: 02-13-2006, 11:30 AM
  7. [SOLVED] VBAProject name compile error, not defined at compile time
    By Matthew Dodds in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-13-2005, 03:20 PM
  8. excel compile error. Only some computers?
    By Bob Aylward in forum Excel General
    Replies: 0
    Last Post: 12-13-2005, 03:20 PM

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