+ Reply to Thread
Results 1 to 13 of 13

While loop

Hybrid View

  1. #1
    Registered User
    Join Date
    08-24-2012
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    9

    While loop

    Hello,

    I need some help in writing a while loop for my excel spreadsheet. I have never used VBA/Macros before and cannot find any specific help online for what I want to do. I hope someone can advise the best way of doing this in excel.
    I am trying to do a simple while loop as follows and for the answer to display in cell D58 in my spreadsheet:

    m1=D55;
    t=D15;
    L=C46
    A=D17;
    i=1;

    while i<=t
    m2=m1+(L/A);
    m1=m2;
    i=i+1;
    end
    disp(m1)

    where D55, D15,C46 and D17 are the cell reference in the excel spreadsheet and are integers inputted by the user.

    Any question, just ask!
    Thanks

  2. #2
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: While loop

    Sub help()
         Dim m1 as range, t as range, l as range, a as range
          dim i as integer 
          i=1
          Set m1=Range("D55"): t=Range9"D15
          Set l=Range("C46"): a=Range("D17")
          ??????
          ??????
          ??????
          i=i+1
          ?????
         end sub
    what do you mean by "while i<=t"
    what is m2 ?
    What does "(L/A)" mean?
    what does the "disp" mean?
    after u answer that, i can finish it

    ---------- Post added at 02:58 PM ---------- Previous post was at 02:58 PM ----------

    also, attach the file

  3. #3
    Registered User
    Join Date
    08-24-2012
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: While loop

    Sorry for not being clearer,

    while i is less than or equal to t (i being the iteration and t an integer inputted by the user)

    m2 is a new value I wish to calculate: m1 plus (L divided by A)

    I want the final value displayed in cell D58 to be the final value of m2 calculated

    Thanks for your help

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: While loop

    VBA syntax is nearly the same:

    Sub dural()
    Dim M1 As Integer, t As Integer, L As Integer, A As Integer
    Dim i As Integer
    M1 = [D55]
    t = [D15]
    L = [C46]
    A = [D17]
    i = 1
    While i <= t
        M2 = M1 + (L / A)
        M1 = M2
        i = i + 1
    Wend
    MsgBox M1
    End Sub
    Gary's Student

  5. #5
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Lightbulb Re: While loop

    Sub help()
         Dim m1 as range, t as range, l as range, a as range
          dim i as integer
         dim m2 as variant
          i=1
          Set m1=Range("D55"): t=Range9"D15
          Set l=Range("C46"): a=Range("D17")
          if u.value<=t.value then
               m2=m1.value+(l.value/a.value)
               m1.value=m2.value
               i=i+1
          end if
         msgbox"m1"                           ' (not code: if "msgbox "m1" " doesn't work, do : "msgbox str(m1) " or "msgbox "m1.value" ")
         range("D58").value= m2
    end sub

  6. #6
    Registered User
    Join Date
    08-24-2012
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: While loop

    OK,
    Thanks, you have been a lot of help!
    I just have one more question:

    Say, for instance, C46 contains a formula of which the value is not an integer such as
    =IF(C45>(D18/I12), D18/I12, C45)
    where D18 and I12 are cell references to numbers entered by the user and C45 contains another formula, what would the code change to?

    Al

  7. #7
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: While loop

    Make all variants instead of integers
    pls send the file so i can see what u mean
    i am not sure if i am correct. if i see, i can tell u in more detail

    ---------- Post added at 03:46 PM ---------- Previous post was at 03:44 PM ----------

    no never mind
    nothing changes, i think

    ---------- Post added at 03:46 PM ---------- Previous post was at 03:46 PM ----------

    do NOT change it to variable yet
    let me see file first

  8. #8
    Registered User
    Join Date
    08-24-2012
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: While loop

    Hi,
    Sorry for the delay, I could not upload the file as it was but I have edited it so you can see the relevant information.

    Hopefully this has worked
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    08-24-2012
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: While loop

    I want it to run every time an input changes value

    ---------- Post added at 03:16 PM ---------- Previous post was at 03:08 PM ----------

    Yeah, to loop so that the new value of m2 is added to the previous as

    while i<=t
    m2=m1+(L/A);
    m1=m2;
    i=i+1;
    end

  10. #10
    Registered User
    Join Date
    08-24-2012
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: While loop

    Ok, thanks so much for all the help!!

  11. #11
    Registered User
    Join Date
    08-24-2012
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: While loop

    yeah, most of the cells will be protected so only a few will be able to changed

  12. #12
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: While loop

    Just change the value in D55 to change d58

  13. #13
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: While loop

    i will send u the file
    just change the value in d55 to change the value in d58

    Calculator (3).xlsm

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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