+ Reply to Thread
Results 1 to 12 of 12

Range in Variables?

  1. #1
    Registered User
    Join Date
    06-08-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    20

    Range in Variables?

    I got help here before with my script, but i am still tuning it, and i have a simple question i think, how can i prevent writing to cells, and instead keep everything in variables?

    Range("K11:L11").Value = .LinEst(rng1y, rng1x)

    There is an example, i would like to write the result in 2 variables, and not K11, L11.

    I have tried many things, but i canīt figure out how to do it.

    I was thinking that rng1y and rng1x should work, i mean, it writes from them.

    But i canīt use them for some reason, not sure why. Maybe i need to convert them or something.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Range in Variables?

    That method returns an array. The variables would have to be declared as such.

  3. #3
    Registered User
    Join Date
    06-08-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Range in Variables?

    Sadly that doesnīt tell me much, what can i do?


    The results i want to have, are in numbers.
    And i use later in the script functions that will use them (and if they are not number, it will not work.)

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Range in Variables?

    If you syntax is correct, you can store in to variable.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-08-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Range in Variables?

    The problem in that code there, is, K will store both results, right?

    I need to have Linest(A,B) separate.

    Or can i choose which value i want to use from K?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    Have a wee shifty here http://www.ozgrid.com/forum/showthread.php?t=33544.
    If posting code please use code tags, see here.

  7. #7
    Registered User
    Join Date
    06-08-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Range in Variables?

    I read that, but i still donīt get how i can get the results.

  8. #8
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Range in Variables?

    I'm confused as to what you mean by LinEst(A,B) separate. rng1y and rng1x are two arguments passed to it not returned from it.
    Last edited by Solus Rankin; 07-05-2013 at 06:03 PM. Reason: poorly worded

  9. #9
    Registered User
    Join Date
    06-08-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Range in Variables?

    Well i am bad at this, but will give an example.


    Letīs say i do this: Range("K11:L11").Value = .LinEst(rng1y, rng1x)

    Now i will get 2 results, one written in K11 and the other in L11.

    so K11 is for example , 11.245, and L11 is 23.55


    Now what i want is to not write it in any cell, as i have no reason to do so.
    I would rather have it in 2 variables.

    so x = 11.24 and y = 23.55.

    Hope that explains it.

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,464

    Re: Range in Variables?

    Perhaps you can explain what you mean by "2 variables"? The statement:
    Please Login or Register  to view this content.
    stores the array returned by the linest function into k. Recognizing that k is now an array (or perhaps, more accurately, a variant containing an array), we can use array indices to access the different values in k. Assuming our linest regression is a simple straight line y=mx+b, k(1) contains m, k(2) contains b.

    If this is different enough for you, you can simply leave it at that. If you really want two different variable names, you could simply have a couple of statements after the linest results are stored in k
    Please Login or Register  to view this content.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  11. #11
    Registered User
    Join Date
    06-08-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Range in Variables?

    Great that was the thing i was looking for!

    Variable(1)
    Variable(2)

    So i can choose the result.

    Then i donīt need to separate Variables either!

    Sadly, i am at another problem now thanks to my lack of knowledge.


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

    Here is an example from my code, i dinīt copy everything as it didnīt matter.

    But if if you look at the other sub (ReadData), i am trying to use the v1 variable from the other (UserForm_initialize) sub.
    But it doesnīt work, i guess you canīt get the other variables from other subs just like that.
    I currently have done some very bad shortcuts to solved this, but it has worked.

    But now i canīt do anything, so i would like to know what i am supposed to do to let it use the variable.

    Thanks
    Last edited by zerowalker; 07-06-2013 at 01:57 PM.

  12. #12
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Range in Variables?

    Zerowalker,

    If you original question is answered please mark thread as solved, and ask any further questions in a new post.


    To use the v1 variable in both subs you'll have to make it a public variable by diming it outside a procedure. If declared inside a procedure vba deletes the value when the procedure has run.
    Last edited by Solus Rankin; 07-06-2013 at 02:47 PM.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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