+ Reply to Thread
Results 1 to 3 of 3

My program does not stop at the desired time

  1. #1
    Registered User
    Join Date
    02-22-2016
    Location
    Nampa, ID
    MS-Off Ver
    2013
    Posts
    1

    My program does not stop at the desired time

    I have a for loop that is s=o to tf step dt, but the program never stops at the tf value. I just have no idea what I am doing wrong or what I need to add into my program. I have the whole program shown below. The basic concept is one dimensional heat transfer. I have all the right equations and things, but I just need to know how to make it stop at the time I want it to for tf being time final. Thank you!

    Private Sub CommandButton1_Click()
    'Alexis Fesenbek
    '1D Heat Transfer

    'Dimension the variables
    Dim dx, dt, a, TH, TL, tf, k, h As Double
    Dim T(1, 10) As Double
    Dim i, s As Integer

    'Setting values to the variables
    TH = 100
    TL = 0
    dx = 0.1
    dt = 0.1
    a = Worksheets("Sheet1").Cells(13, 3).Value
    tf = Worksheets("Sheet1").Cells(12, 3).Value
    h = Worksheets("Sheet1").Cells(15, 3).Value
    k = Worksheets("Sheet1").Cells(16, 3).Value

    'Setting all the temps to the TH

    'T(0, 0) = TL
    'T(0, 10) = TL
    For i = 0 To 10
    T(0, i) = TH
    Next i



    'The time loop and correction for the ends of the simulation
    For s = 0 To tf Step dt

    T(0, 0) = (T(0, 1) * k / dx - h * TL) / (h + k / dx)
    T(0, 10) = (T(0, 9) * k / dx - h * TL) / (h + k / dx)

    'for loop for the simulation equation
    For i = 1 To 9
    T(1, i) = T(0, i) + (a * dt) / (dx) * (T(0, i - 1) - 2 * T(0, i) + T(0, i + 1))
    Next i

    'setting new values to previous values
    For i = 1 To 9
    T(0, i) = T(1, i)
    Next i

    'writing the values to the worksheet
    For i = 0 To 10
    Worksheets("Sheet1").Cells(4, 3 + i).Value = T(0, i)
    Next i

    Next s

    End Sub

  2. #2
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: My program does not stop at the desired time

    Have you tried stepping through your code, one line at a time, in the DEBUG mode?

    When you do this you can look at the current values of all of the variables, and determine why the FOR NEXT loop isn't stopping.

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: My program does not stop at the desired time

    Hi there,

    Your problem involves the following statements:

    Please Login or Register  to view this content.
    The variable "s" is declared as an integer variable.

    The For loop initialises the variable "s" with a value of zero, and then attempts to increment its value by 0.1 (dt) on each iteration of the loop.

    Because it is declared as an integer variable, "s" can never increase its value by an amount of less than one, and consequently remains at zero regardless of how many times the For loop is executed.

    Declare "s" as a variable of type Double and see if your code works correctly.


    Note: your Dim statements are probably not doing what you intend! The statement:

    Please Login or Register  to view this content.
    declares the variable "i" as being of type VARIANT, and the variable "s" as being of type Integer. You should probably be using:

    Please Login or Register  to view this content.
    or:

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Last edited by Greg M; 02-23-2016 at 05:35 PM. Reason: Minor change

+ 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. Beginners question: how to stop / abort execution of vba program
    By Rainer in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-22-2019, 10:20 AM
  2. [SOLVED] Stop program from finding text in hidden cells
    By jfd456 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-17-2015, 03:57 AM
  3. [SOLVED] need formula to get desired result from between two date and time depends upon condition
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-16-2014, 03:28 AM
  4. Automatic macro execution at on desired date and time.
    By ajt0107 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-13-2014, 01:33 AM
  5. Number of total calls in desired time frames
    By skate1991 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-31-2013, 11:37 AM
  6. [SOLVED] round up time unless already at the desired time value
    By mattgoudey in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-13-2013, 10:20 AM
  7. Add time in a form with desired format
    By Khaos176 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-12-2009, 10:15 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