PDA

View Full Version : Change value of a string based on the number of times a sub has been run



GenuineGin
05-24-2017, 09:13 AM
New question! Still trying to come up with a solution for my last problem and in the process am making new problems (challenges?) for myself.

I was wondering if there's a way to change the value of a string based on the number of times a sub has been run.

Something along the lines of:

Sub 1 calls Sub 2

Sub 2:
If (sub does something) Then
Go back to Sub 1
Else
i = current value +1
Rerun Sub 2
End if

Go back to Sub 1

Sub 1:
If i = 1 then string = "Section A"
If i = 2 then string = "Section B" etc.

Does that make any sense?

gmaxey
05-24-2017, 11:54 AM
Perhaps something like this:


Option Explicit
Dim i As Long
Sub A()
Dim strResult As String
i = 0
B
Select Case i
Case 1: strResult = "Section A"
Case 2: strResult = "Section B"
End Select
End Sub
Function B() As Long
If 1 = 1 Then
'It met the condition.
i = i + 1
B = i
Else
i = i + 1
B
End If
End Function

GenuineGin
05-25-2017, 01:58 AM
Hi Greg,

Thanks very much. This is exactly the sort of thing I was thinking of.

I've run your example code straight just to try to understand how it works.

For Sub A I've 'Msgbox(strResult)' under 'End Select' to see what the result comes out as. At the moment it only ever brings up "Section A" (Case 1). Do I need to put something in where you've written 'It met the condition' to make it work? At the moment I can't see how 1 would ever not = 1?

gmaxey
05-25-2017, 03:17 AM
Yes, If 1 = 1 is standing in for whatever is done being done successfully.