Results 1 to 12 of 12

Thread: [RESOLVED] Do While Loop Statement

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    49

    Resolved [RESOLVED] Do While Loop Statement

    Hello everyone,
    I started with making an macro and I need help.
    At the bottom of the post is an image which represent what I want to do. I actually want to check value with every next two cells until condition become true.
    Code:
    Sub Workbook()
    
    
    Sheets("sheet1").Activate
    Range("F2:F11").Select
    Selection.ClearContents
    
    Dim i As Integer
    Dim a As Double
    Dim a1 As Double
    Dim a2 As Double
    
    For i = 2 To 11
    a1 = Cells(i, 5).Value
    a2 = Cells(i + 1, 5).Value
    a = (Cells(i, 1).Value - Cells(i, 2).Value) / (Cells(i, 3).Value - Cells(i, 4).Value)
    
        If a1 < a < a2 Then
        Cells(i, 6).Value = a
        Else
            Do
            a = (Cells(i, 1).Value - Cells(i + 1, 2).Value) / (Cells(i, 3).Value - Cells(i + 1, 4).Value)
            Cells(i, 6).Value = a
            i = i + 1
            Loop While a1 > a And a> a2
        End If
    Next
    
    End Sub
    Attached Images Attached Images   
    Last edited by blueye89; Oct 26th, 2015 at 06:28 AM. Reason: Replacing image

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Do While Loop Statement

    Well for starters you posted in the wrong forum. This is the .NET forum... there's a separate one for VBA help (Office DEvelopment) ... I'll ask the mods to move it for you so you don't need to create a new one.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    49

    Re: Do While Loop Statement

    Quote Originally Posted by techgnome View Post
    Well for starters you posted in the wrong forum. This is the .NET forum... there's a separate one for VBA help (Office DEvelopment) ... I'll ask the mods to move it for you so you don't need to create a new one.

    -tg
    Thank you very much.

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Do While Loop Statement

    Moved to Office.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [RESOLVED] Do While Loop Statement

    Is it resolved, or not? If not, what issue are you having exactly?

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    49

    Re: [RESOLVED] Do While Loop Statement

    Quote Originally Posted by vbfbryce View Post
    Is it resolved, or not? If not, what issue are you having exactly?

    Hello vbfbryce,
    It is not yet resolved. I don't know who added in topic title "[RESOLVED]"

    I have uploaded workbook that I am trying to create macro.
    Attached Files Attached Files

  7. #7
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [RESOLVED] Do While Loop Statement

    Hmm...looked at the workbook, but I'm still fuzzy on what you're trying to do. I see how you're calculating A2', but don't see where A2 and A3 are coming from.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    49

    Re: [RESOLVED] Do While Loop Statement

    Quote Originally Posted by vbfbryce View Post
    Hmm...looked at the workbook, but I'm still fuzzy on what you're trying to do. I see how you're calculating A2', but don't see where A2 and A3 are coming from.
    First five columns hava fixed values. I am getting from another source and values can't be changed.
    There are too many rows but I used 10 rows for simplicity. When I realize that it works on few rows I will adapt on all other. The best way to realize what I want to do is to see the image named "equations".
    Regards
    Danijel

  9. #9
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [RESOLVED] Do While Loop Statement

    Try stepping through something like this. Note that I'm not sure what you'd want to have happen if a row which is not the last row never meets the condition.

    Code:
    Sub loopCheck()
        Dim ws As Worksheet
        Dim j As Long
        Dim k As Long
        Dim lr As Long
        Dim a2prime As Double
        Dim Met As Boolean
        
        Met = False
        Set ws = ActiveSheet
        With ws
            lr = ws.Range("a" & Rows.Count).End(xlUp).Row
            For j = 2 To lr
                If j = lr Then
                    a2prime = (.Range("a" & j).Value - .Range("b" & j).Value) / (.Range("c" & j).Value - .Range("d" & j).Value)
                    If .Range("e" & j).Value < a2prime And a2prime < .Range("e" & j).Value Then
                        .Range("f" & j).Value = a2prime
                        GoTo endIt
                    Else
                        GoTo endIt
                    End If
                End If
                k = j - 1
                Met = False
                a2prime = (.Range("a" & j).Value - .Range("b" & j).Value) / (.Range("c" & j).Value - .Range("d" & j).Value)
                
                While Met = False
                    k = k + 1
                    If k > lr Then
                        MsgBox "Condition not met for row " & j & ".  Exiting"
                        GoTo endIt
                    End If
                    If .Range("e" & k).Value < a2prime And a2prime < .Range("e" & k + 1).Value Then
                        .Range("f" & j).Value = a2prime
                        Met = True
                    End If
                Wend
                
            Next j
        End With
    endIt:
        Set ws = Nothing
    End Sub

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    49

    Re: [RESOLVED] Do While Loop Statement

    Quote Originally Posted by vbfbryce View Post
    Try stepping through something like this. Note that I'm not sure what you'd want to have happen if a row which is not the last row never meets the condition.

    Code:
    Sub loopCheck()
        Dim ws As Worksheet
        Dim j As Long
        Dim k As Long
        Dim lr As Long
        Dim a2prime As Double
        Dim Met As Boolean
        
        Met = False
        Set ws = ActiveSheet
        With ws
            lr = ws.Range("a" & Rows.Count).End(xlUp).Row
            For j = 2 To lr
                If j = lr Then
                    a2prime = (.Range("a" & j).Value - .Range("b" & j).Value) / (.Range("c" & j).Value - .Range("d" & j).Value)
                    If .Range("e" & j).Value < a2prime And a2prime < .Range("e" & j).Value Then
                        .Range("f" & j).Value = a2prime
                        GoTo endIt
                    Else
                        GoTo endIt
                    End If
                End If
                k = j - 1
                Met = False
                a2prime = (.Range("a" & j).Value - .Range("b" & j).Value) / (.Range("c" & j).Value - .Range("d" & j).Value)
                
                While Met = False
                    k = k + 1
                    If k > lr Then
                        MsgBox "Condition not met for row " & j & ".  Exiting"
                        GoTo endIt
                    End If
                    If .Range("e" & k).Value < a2prime And a2prime < .Range("e" & k + 1).Value Then
                        .Range("f" & j).Value = a2prime
                        Met = True
                    End If
                Wend
                
            Next j
        End With
    endIt:
        Set ws = Nothing
    End Sub
    Hello vbfbryce,
    This is usefull, I just need to clear all code lines that you wrote.
    Thank you so much for this.
    Best regards
    blueye89

  11. #11
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [RESOLVED] Do While Loop Statement

    You need to clear all code lines? What do you mean?

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    49

    Re: [RESOLVED] Do While Loop Statement

    Quote Originally Posted by vbfbryce View Post
    You need to clear all code lines? What do you mean?
    I thought to make it clear for me. I can't understand every line that you wrote cause I am not programmer.
    Thank you very much.
    Regads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width