Loop to Value

Leandroarb

Board Regular
Joined
Oct 7, 2014
Messages
157
Hi,

The logical for this code are correct?
I need it to repeat the loop until the value of cont is equal to
Code:
 Int(Left(Prazo, 2))


Code:
Global rng As Range
Global dtData As Range
Global crit As Variant


Public Function PrazoFinal(DatasAbono As Range, vlrData As Variant)  ', Prazo As String, Data_Protocolo As Date)  'DatasAbono = Intervalo com datas não contáveis
Dim cont    As Integer                                                                     'Prazo = Célula com o prazo do serviço
Dim j       As Integer                                                                       'Data_Protocolo = Célula com data e hora do protocol
Dim strVerif As String


Set rng = DatasAbono
cont = 0
j = 0


Do Until cont = Int(Left(Prazo, 2))


    j = j + 1
    vlrData = Format(DateAdd("d", j, Data_Protocolo), "dd/mm/yyyy")
    crit = vlrData
    
        If Verif = "UT" Then
            PrazoFinal = crit
            cont = cont + 1
        End If
Loop
End Function


Public Function Verif() As String '(rng As Range, crit As Variant) As String
Dim rng2 As Range
Dim cel As Range
Set rng2 = rng
Dim strVerif As String
    For Each cel In rng2
        If cel = crit Then
            strVerif = crit
        End If
    Next cel
If strVerif <> "" Then Verif = "UT" Else Verif = "IN"
End Function
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You haven't given Prazo a value, so you should get an error on that line
 
Upvote 0
This worked!!!

Code:
Public Function PrazoFinal(DatasAbono As Range, vlrData As Date, Prazo As String)   ', Prazo As String, Data_Protocolo As Date)  'DatasAbono = Intervalo com datas não contáveis
On Error Resume Next
Dim cont    As Integer                                                                   'Prazo = Célula com o prazo do serviço
Dim j       As Integer                                                                   'Data_Protocolo = Célula com data e hora do protocol
Dim strVerif As String


Set rng = DatasAbono
cont = 0
j = 0


Do
    If Right(Prazo, 1) = "H" Then
        PrazoFinal = DateAdd("h", 4, vlrData)
        Exit Function
    End If
    
    j = j + 1
    crit = Format(DateAdd("d", j, vlrData), "dd/mm/yyyy")
        If Verif = "UT" Then
            PrazoFinal = crit '& " - " & Verif
            cont = cont + 1
        End If
        
Loop While cont < Int(Left(Prazo, 2))
End Function


Public Function Verif() As String '(rng As Range, crit As Variant) As String
On Error Resume Next
Dim rng2      As Range
Dim cel       As Range
Dim strVerif As String


Set rng2 = rng


    For Each cel In rng2.Cells
        If Replace(CLng(cel), " ", "") = Replace(CLng(CVDate(crit)), " ", "") Then
            strVerif = cel
        End If
    Next cel
    
If strVerif = "" Then Verif = "UT" Else Verif = "IN"


End Function
 
Upvote 0
Ok, insert the line in red
Code:
[COLOR=#ff0000]Stop[/COLOR]
Set rng = DatasAbono
When you run the function it should stop on that line, allowing you to step through the code using F8.
This may enable you to find out where things are going wrong.
 
Upvote 0
Ok, insert the line in red
Code:
[COLOR=#ff0000]Stop[/COLOR]
Set rng = DatasAbono
When you run the function it should stop on that line, allowing you to step through the code using F8.
This may enable you to find out where things are going wrong.

Thanks Mr. Fluff !
With your help, I learn more and more!
Regards!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top