• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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
0
lynnton
Asked:
lynnton
  • 6
  • 5
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> 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
0
 
lynntonAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ah, ok...I'm an idiot.  =)

~IM
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
lynntonAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This the behaviour you looking 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

    ' setup the arrays
    Names = Split("ForMon,ForTue,ForWed,ForThu,ForFri,ForSat,ForSun", ",")
    Values = Array(ForMon, ForTue, ForWed, ForThu, ForFri, ForSat, ForSun)
   
    ' for testing...
    Debug.Print "Before Sort:"
    For i = 0 To UBound(Names)
        Debug.Print Names(i) & " = " & Values(i)
    Next i
    Debug.Print
   
    ' bubble sort 'em
    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
   
    ' build the output string
    For i = 0 To UBound(Names)
        If output = "" Then
            output = Names(i)
        Else
            output = output & "_" & Names(i)
        End If
    Next i
   
    ' find the first nonzero value starting with third position
    Dim subtractValue As Long
    For i = 2 To UBound(Values)
        subtractValue = Values(i)
        If subtractValue > 0 Then
            Exit For
        End If
    Next i
   
    ' starting with third position, if value is greater than zero
    ' then subtract the value from it
    For i = 2 To UBound(Names)
        If Values(i) > 0 Then
            Values(i) = Values(i) - subtractValue
           
            ' update the actual variable with new value
            Select Case Names(i)
                Case "ForMon"
                    ForMon = Values(i)
                Case "ForTue"
                    ForTue = Values(i)
                Case "ForWed"
                    ForWed = Values(i)
                Case "ForThu"
                    ForThu = Values(i)
                Case "ForFri"
                    ForFri = Values(i)
                Case "ForSat"
                    ForSat = Values(i)
                Case "ForSun"
                    ForSun = Values(i)
            End Select
        End If
    Next i
   
    ' for testing...
    Debug.Print "Result:"
    Debug.Print output
    Debug.Print
    Debug.Print "After Sort and Subtraction:"
    For i = 0 To UBound(Names)
        Debug.Print Names(i) & " = " & Values(i)
    Next i
    Debug.Print "-------------------------------------"
   
    ' return string
    SortDates = output
End Function

0
 
lynntonAuthor Commented:
Idle Mind,

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

Thanks,
Lynnton
0
 
lynntonAuthor Commented:
Idle Mind,

Can you give guidance on this post?

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21218433.html

Thanks,
Lynnton
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It seems to have been deleted already sorry.

Mike
0
 
lynntonAuthor Commented:
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now