+ Reply to Thread
Results 1 to 4 of 4

User input on worksheet to directly edit or amend code in VBA

  1. #1
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    User input on worksheet to directly edit or amend code in VBA

    Attachment 357212

    Hi Guys

    I’m after some direction on this one.

    I want the user to be able to amend code by changing a cell in the worksheet without the need to edit the code.

    Background
    I have the attached file which creates a 100 x 100 grid of x and y coordinates and using a hardcoded function determines the z coordinate for each x and y and then plots a 3 dimensional image representing that function. At present it just makes pretty images (try it for yourself) and one day I may actually find a practical use for it.

    For the example attached I use a 2 dimensional array for z and calculate as follows:

    z(x, y) = (x - 50) ^ 3 + 50000 * Sin((y - 50) / 4)

    The (x-50) and (y-50) are simply to ensure the 100 x 100 grid is centred around x=0 and y= 0 axis.

    If I want to plot a different function I need to edit this line of the code.

    I would like to be able to enter in a cell (say y3)in the worksheet a similar function say x^2 + y^2 and have the code adjust accordingly, but attempts to set z(x,y) = range(“y3”).value simply sets z(x,y) to a string value equal to “x^2 + y^2” rather than amending the line of code. Once I work this out I can use simple substitution formula’s to change the x’s and y’s back to (x-50) and (y-50) so I’m not too concerned about that at present.

    Is there a way to take the text string from a cell and use this in the code? If so how do I do it

    WARNING
    I’m self taught in VBA so my code might be a bit messy


    Please Login or Register  to view this content.
    Happy with my advice? Click on the * reputation button below

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: User input on worksheet to directly edit or amend code in VBA

    You can do this without VBA.

    Lets say that you have the string " (x - 50) ^ 3 + 50000 * Sin((y - 50) / 4)" in A1
    And you have your x values in C2:C100 and your Y values in D1:AC1 (or how ever far down and out you want to go)
    Select cell D2 and define a name

    Name: zValue
    RefersTo: =EVALUATE(SUBSTITUTE(SUBSTITUTE(Sheet1!$A$1,"x", Sheet1!$C2),"y", Sheet1!D$1))

    (Note the absolute/relative addressing in the last two cell references.)

    Then put the formula =zValue in D2 and drag down and right.
    The values in your matrix will change when you change the formula in A1.

    In the attached, A1 allows you to either choose from the list or to enter your own formula (lower case x and y)
    (Note that the attached is a .xlsx file, no macros)
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: User input on worksheet to directly edit or amend code in VBA

    Mikerickson

    That wasn't what I was looking for because I'm trying to steer clear of the excel charts but thanks anyway becauue I learnt something new today. I think I'll use that somewhere else.

  4. #4
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: User input on worksheet to directly edit or amend code in VBA

    Hi Guys,

    About to close this one as I've worked it out myself but with a very big hint from Mickrickson (thanks).

    For those of you who are interested or for those in the future searching the code is below. Evalute certainly got me heading in the right direction it's just that it appears you can't assign an array value to an evaluate formula so I had to do this in two steps.

    Please Login or Register  to view this content.

+ 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. Using a userform input to find and edit value in a worksheet
    By izzorac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-30-2014, 05:13 PM
  2. [SOLVED] Can excel edit an existing macro with very little user input?
    By Angelfish13 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-30-2013, 02:28 PM
  3. [SOLVED] Excel vba user form- open directly to user form not worksheet
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 04-24-2013, 05:07 PM
  4. How to amend code to send specific worksheet
    By charliefunkuk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-16-2008, 08:00 AM
  5. Code to take me directly to a specified worksheet??
    By monkey2003 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-24-2008, 03:44 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