+ Reply to Thread
Results 1 to 5 of 5

Strange loop...well, it appears to be.

  1. #1
    Registered User
    Join Date
    05-19-2013
    Location
    Thailand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Strange loop...well, it appears to be.

    Can anyone explain why this seems to loop back to the beginning of the "Sub" and refuses to go to the next line. The problem occurs at the highlighted line. Once executed, at the end of the offending line< it "loops" back to the "Private Sub Worksheet_Change" line. I accept my coding is not the best, but am teaching/learning by myself.
    I am using Excel 2007.


    Private Sub Worksheet_Change(ByVal Target As Range)


    Dim Mon_TTH As Date, TDecimal
    Dim Result As Single
    Rem Dim Ftime As Integer
    Rem Dim Stime As Integer
    'Do nothing if more than one cell is changed or content deleted

    If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub



    If Target.Address = "$G$7" Then

    'Ensure target is a number before multiplying by 2

    If IsNumeric(Target) Then

    'Stop any possible runtime errors and halting code

    On Error Resume Next

    'Turn off ALL events so the Target * 2 does not put the code into a loop.

    Application.EnableEvents = False

    Target = Target * 2

    'Turn events back on

    Application.EnableEvents = True

    'Allow run time errors again

    On Error GoTo 0

    End If

    End If





    Monday:
    Mon_Stime = Worksheets("Timesheet_Entry").Range("E7").Value
    Mon_Ftime = Worksheets("Timesheet_Entry").Range("G7").Value

    Mon_TTH = TimeValue(Mon_Ftime) - TimeValue(Mon_Stime)

    Rem If Mon_TTH > #2:00:00 AM# Then Mon_TTH = #2:00:00 AM#
    Mon_TTH = Format(Mon_TTH, "hh:mm")
    TDecimal = Split(Mon_TTH, ":")
    Result = TDecimal(0) + ((TDecimal(1) * 100) / 60) / 100
    Result = Format(Result, "##0.00")
    MsgBox Result

    Worksheets("Timesheet_Entry").Range("H7").Value = Result

    If Worksheets("Timesheet_Entry").Range("H7").Value > "8" Then Worksheets("Timesheet_Entry").Range("I7").Value = "8.0"

    End If
    Last edited by LaurieH; 12-14-2014 at 05:47 AM. Reason: extra info

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Strange loop...well, it appears to be.

    You are changing something on the sheet, therefore the sheet's change event will be called.

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    05-19-2013
    Location
    Thailand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Strange loop...well, it appears to be.

    Wow, a very quick reply...thnx. What you say makes sense, now I need to find a work around.

    Once again...thanks

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Strange loop...well, it appears to be.

    You've kind of got the solution in the first part of the code where you turn off events.

  5. #5
    Registered User
    Join Date
    05-19-2013
    Location
    Thailand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Strange loop...well, it appears to be.

    Thanx Norie, you are correct, have turned "off" events, then when values have been updated, have turned them back on.... all is good, I have so much to learn.

+ 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. [SOLVED] Stopping a Loop in a macro when a keyword appears.
    By andrew93hughes in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-02-2013, 01:56 PM
  2. [Solved] Loop Through values until new value Appears, then Subtract
    By sgillette01 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-26-2012, 01:06 PM
  3. Program appears to loop endlessly, have to force quit
    By Danexcel in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-18-2009, 12:09 PM
  4. strange loop problem
    By TxRaistlin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-31-2005, 03:05 PM
  5. Do Until Loop strange behavior
    By dht in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2005, 12:06 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