Results 1 to 9 of 9

Thread: [RESOLVED] plz help, how can i loop this.

  1. #1

    Thread Starter
    Banned
    Join Date
    Aug 2016
    Location
    https://t.me/pump_upp
    Posts
    30

    Resolved [RESOLVED] plz help, how can i loop this.

    Hi vb masters,
    I need to change the value of text2 as per following condition :-
    Code:
    If Text1.Text = 0.05 Then Text2.Text = 1
    If Text1.Text = 1.05 Then Text2.Text = 1
    If Text1.Text = 2.05 Then Text2.Text = 1
    If Text1.Text = 3.05 Then Text2.Text = 1
    I want to loop this condition from 0.05 to 999.05, how can I do it. Plz help me on the way.
    Regards.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: plz help, how can i loop this.

    Simple
    Code:
        Dim X As Currency
        For X = .05 To 999.05
            If text1.Text = X Then text2.Text = "1"
        Next

  3. #3

    Thread Starter
    Banned
    Join Date
    Aug 2016
    Location
    https://t.me/pump_upp
    Posts
    30

    Re: plz help, how can i loop this.

    Thanks a lot DataMiser. It works fine. [Resolved]

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [RESOLVED] plz help, how can i loop this.

    It is better to add Exit For to speed up the program, because no need to continue the loop after found that Text1.Text = X

    Code:
        Dim X As Currency
    
        For X = 0.05 To 999.05
            If Text1.Text = X Then
                Text2.Text = "1"
                Exit For
            End If
        Next



  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] plz help, how can i loop this.

    True. Exit for would be highly recommended in such a case.

    I would have mentioned that as well had I been thinking. Instead I just duplicated the code shown but in a loop
    You could also add a little code to reduce the length of the loop more for high values by changing the starting value of x.

    For example if text1.text>500 then start x at 500.05 which would relieve the first 500 iterations through the loop.

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [RESOLVED] plz help, how can i loop this.

    Quote Originally Posted by DataMiser View Post
    You could also add a little code to reduce the length of the loop more for high values by changing the starting value of x.

    For example if text1.text>500 then start x at 500.05 which would relieve the first 500 iterations through the loop.
    Good point, but i suggest starting the loop with CInt(Text1.Text) + 0.05

    Code:
    For X = CInt(Text1.Text) + 0.05 To 999.05
    by this way the loop will iterate only 1 if Text1.Text = X, and (999 - Text1.Text) if not



  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] plz help, how can i loop this.

    How about this instead of a loop:

    Code:
        Select Case CDbl(Text1.Text)
        Case 0.05 To 999.05
            If Right(Text1.Text, 2) = "05" Then
                Text2.Text = 1
            else
                Text2.Text = 0
            End If
        Case Else
            Text2.Text = 0
        End Select
    Last edited by SamOscarBrown; Jun 10th, 2017 at 08:13 PM.

  8. #8
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [RESOLVED] plz help, how can i loop this.

    Quote Originally Posted by SamOscarBrown View Post
    How about this instead of a loop:

    Code:
        Select Case CDbl(Text1.Text)
        Case 0.05 To 999.05
            If Right(Text1.Text, 2) = "05" Then
                Text2.Text = 1
            else
                Text2.Text = 0
            End If
        Case Else
            Text2.Text = 0
        End Select
    Your code will validate many incorrect values, e.g. 123.45605, 123.999905 etc...

    Try the following instead
    Code:
        Text2.Text = 0
        Select Case CDbl(Text1.Text)
            Case 0.05 To 999.05
                If Int(Text1.Text) + 0.05 = CDbl(Text1.Text) Then
                    Text2.Text = 1
                End If
        End Select



  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] plz help, how can i loop this.

    Yup...that's one way to avoid incorrect currency entries...of course there are others (which should be done at actual entry time, whether it is from an external file/db/etc or input with the keyboard). OP Might want to check to see if the entry is a valid currency value (like using FormatCurrency() function, limiting number of numbers after decimal to 2. Or can use the Istr() function to find where the decimal resides and make sure only two values have been entered after that.

    My whole point was it seemed strange to want to loop through anything (even though those above suggestions could work in some situations). OP doesn't give up much to go on, so I guess any guidance is good.

Tags for this Thread

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