Link to home
Start Free TrialLog in
Avatar of lynnton
lynnton

asked on

change the value of variables

Hi,

We have seven variables namely, ForMon, ForTue, ForWed, ForThu, ForFri, ForSat, ForSun

We need to sort it from lowest to highest. now we have created position (first, second, third, fourth, fifth, six, seventh)

We use the third position as a starting point.

we subtract the third position to the fourth, fifth, sixth, seventh and itself (third) repectively.
-initially, if the third position's value is zero, we use the next position, in this case the fourth position to subtract to the rest.
*I will use this code to run again and again so that i would have all seven variable equals to zero

i.e.
ForMon=2
ForTue=3
ForWed=4
ForThu=1
ForFri=5
ForSat=7
ForSun=6

run code (subtract the values)
change the values of the variables
return is...
ForMon=2
ForTue=0
ForWed=1
ForThu=1
ForFri=2
ForSat=4
ForSun=3



________________________________________________________________
here's the sorting code Idle mind came up
Option Explicit

Dim ForMon As Long, ForTue As Long, ForWed As Long
Dim ForThu As Long, ForFri As Long, ForSat As Long, ForSun As Long

Private Sub Form_Load()
ForMon=2
ForTue=3
ForWed=4
ForThu=1
ForFri=5
ForSat=7
ForSun=6
End Sub

Private Sub Command1_Click()
    MsgBox SortDates
End Sub

Private Function SortDates() As String
    Dim Names As Variant
    Dim Values As Variant
    Dim tempName As String
    Dim tempValue As Long
    Dim i As Integer
    Dim j As Integer
    Dim output As String

    Names = Split("ForMon,ForTue,ForWed,ForThu,ForFri,ForSat,ForSun", ",")
    Values = Array(ForMon, ForTue, ForWed, ForThu, ForFri, ForSat, ForSun)
    For i = (UBound(Values) - 1) To 0 Step -1
        For j = 0 To i
            If Values(j) > Values(j + 1) Then
                ' swap values
                tempValue = Values(j)
                Values(j) = Values(j + 1)
                Values(j + 1) = tempValue
               
                ' swap names
                tempName = Names(j)
                Names(j) = Names(j + 1)
                Names(j + 1) = tempName
            End If
        Next j
    Next i
    For i = 0 To UBound(Names)
        If output = "" Then
            output = Names(i)
        Else
            output = output & "_" & Names(i)
        End If
    Next i
    SortDates = output
End Function
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

>> we subtract the third position to the fourth, fifth, sixth, seventh and itself (third) repectively.
-initially, if the third position's value is zero, we use the next position, in this case the fourth position to subtract to the rest.
*I will use this code to run again and again so that i would have all seven variable equals to zero

The values in first and second place never get changed, so how can you use this code to get all seven variables equal to zero by running it again and again?

Please explain in more detail how the code should work and what you are going to use it for...

~IM
Avatar of lynnton
lynnton

ASKER

Idle Mind,

The values for first and second will change, I run your awsome code, so first and second values are 1 and 2.
while the rest are subtracted.
-i run it again second time
so first and second are still the same.
-now if i run your awsome code again and again, the first and second value will change, this is because the other position,
will hit zero in the long run.
___________________________________________________________________________________________
the first and second position are always used as virtual "Day off"
the third value is the lowest number of possible schedules for the whole week.
subtracting it would result in another set of schedules, until we have completed it (zero for all seven variables).

It will be use for forecasting schedules.

Thanks,
Lynnton
Ah, ok...I'm an idiot.  =)

~IM
This is what you asked for:

Option Explicit

Dim ForMon As Long, ForTue As Long, ForWed As Long
Dim ForThu As Long, ForFri As Long, ForSat As Long, ForSun As Long

Private Sub Form_Load()
ForMon = 2
ForTue = 3
ForWed = 4
ForThu = 1
ForFri = 5
ForSat = 7
ForSun = 6
End Sub

Private Sub Command1_Click()
    MsgBox SortDates
End Sub

Private Function SortDates() As String
    Dim Names As Variant
    Dim Values As Variant
    Dim tempName As String
    Dim tempValue As Long
    Dim i As Integer
    Dim j As Integer
    Dim output As String

    ' sort
    Names = Split("ForMon,ForTue,ForWed,ForThu,ForFri,ForSat,ForSun", ",")
    Values = Array(ForMon, ForTue, ForWed, ForThu, ForFri, ForSat, ForSun)
    For i = (UBound(Values) - 1) To 0 Step -1
        For j = 0 To i
            If Values(j) >= Values(j + 1) Then
                ' swap values
                tempValue = Values(j)
                Values(j) = Values(j + 1)
                Values(j + 1) = tempValue
               
                ' swap names
                tempName = Names(j)
                Names(j) = Names(j + 1)
                Names(j + 1) = tempName
            End If
        Next j
    Next i
    For i = 0 To UBound(Names)
        If output = "" Then
            output = Names(i)
        Else
            output = output & "_" & Names(i)
        End If
    Next i
   
    'subrtract values
    Dim thirdPosition As Long
    Select Case Names(2)
        Case "ForMon"
            thirdPosition = ForMon
        Case "ForTue"
            thirdPosition = ForTue
        Case "ForWed"
            thirdPosition = ForWed
        Case "ForThu"
            thirdPosition = ForThu
        Case "ForFri"
            thirdPosition = ForFri
        Case "ForSat"
            thirdPosition = ForSat
        Case "ForSun"
            thirdPosition = ForSun
    End Select
    ' grab fourth position if third is 0
    If thirdPosition = 0 Then
        Select Case Names(3)
        Case "ForMon"
            thirdPosition = ForMon
        Case "ForTue"
            thirdPosition = ForTue
        Case "ForWed"
            thirdPosition = ForWed
        Case "ForThu"
            thirdPosition = ForThu
        Case "ForFri"
            thirdPosition = ForFri
        Case "ForSat"
            thirdPosition = ForSat
        Case "ForSun"
            thirdPosition = ForSun
    End Select
    End If
    For i = 2 To UBound(Names)
        Select Case Names(i)
            Case "ForMon"
                ForMon = ForMon - thirdPosition
            Case "ForTue"
                ForTue = ForTue - thirdPosition
            Case "ForWed"
                ForWed = ForWed - thirdPosition
            Case "ForThu"
                ForThu = ForThu - thirdPosition
            Case "ForFri"
                ForFri = ForFri - thirdPosition
            Case "ForSat"
                ForSat = ForSat - thirdPosition
            Case "ForSun"
                ForSun = ForSun - thirdPosition
        End Select
    Next i
   
    ' return string
    SortDates = output
End Function
Avatar of lynnton

ASKER

Idle Mind,

only one thing, when run fourth or fifth time, the result is always the same.

results returned
----------------------
formon= 0
fortue=  0
forwed=  0
forthu=  0
forfri=  0
forsat=  2
forsun=  1

*please read your comment section

Thanks,
Lynnton
Your right...while walking the kids to school, I realized there was a mistake in my code.  Gimme a minute and I'll fix it.

~IM
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lynnton

ASKER

Idle Mind,

All I can say "No one else comes close" the song.

Thanks,
Lynnton
Avatar of lynnton

ASKER

Idle Mind,

Can you give guidance on this post?

https://www.experts-exchange.com/questions/21218433/Relate.html

Thanks,
Lynnton
It seems to have been deleted already sorry.

Mike
Avatar of lynnton

ASKER

Mike,

My apology, please try this one.

https://www.experts-exchange.com/questions/21221016/help-modify-to-run-faster.html

Thanks,
Lynnton