Solved

Rotating a list

Posted on 2013-12-28
7
216 Views
Last Modified: 2013-12-29
I have a closed loop list like

loop(1)=      19
loop(2)=      33
loop(3)=      32
loop(4)=      31
loop(5)=      6
loop(6)=      10
loop(7)=      11
loop(8)=      19

I would like to have a macro which will find the minimum number (here it is 6) and rotate the list so that the list starts with that number giving

loop(1)=      6
loop(2)=      10
loop(3)=      11
loop(4)=      19
loop(5)=      33
loop(6)=      32
loop(7)=      31
loop(8)=      6
0
Comment
Question by:Saqib Husain, Syed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39744121
Where did you store the numbers in the list?
Where do you want the output list?
0
 
LVL 81

Expert Comment

by:byundt
ID: 39744156
There may be a more elegant way of doing it, but here is code that uses an intermediate array for rewriting the list.
Sub Test()
Dim vLoop(1 To 8) As Variant, vResult As Variant
Dim i As Long
vLoop(1) = 19
vLoop(2) = 33
vLoop(3) = 32
vLoop(4) = 31
vLoop(5) = 6
vLoop(6) = 10
vLoop(7) = 11
vLoop(8) = 19
RotateLoop vLoop
For i = 1 To 8
    Debug.Print i & "    " & vLoop(i)
    Next
End Sub

Sub RotateLoop(vLoop As Variant)
Dim dMin As Double
Dim v As Variant
Dim i As Long, iMin As Long, iFirst As Long, iLast As Long, j As Long, k As Long
iFirst = LBound(vLoop)
iLast = UBound(vLoop)
ReDim v(iFirst To iLast)
dMin = Application.Min(vLoop)
iMin = Application.Match(dMin, vLoop, 0) + iFirst - 1
j = iFirst
For i = iMin To iLast
    v(j) = vLoop(i)
    j = j + 1
Next
If iMin > iFirst Then
    For i = iFirst To iMin - 1
        v(j) = vLoop(i)
        j = j + 1
    Next
End If
For i = iFirst To iLast
    vLoop(i) = v(i)
Next
End Sub

Open in new window

0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 39744370
Thanks, Brad, but the result does not match the desired result. The original loop starts at 19 and ends at 19. The transformed loop should start at 6 and end at 6.

Would it be also possible to have vLoop as a collection object?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 81

Expert Comment

by:byundt
ID: 39744834
I can add the "repeat the starting value at end of list" feature by adding two statements to the original code:
Sub RotateLoop(vLoop As Variant)
Dim dMin As Double
Dim v As Variant
Dim i As Long, iMin As Long, iFirst As Long, iLast As Long, j As Long, k As Long
iFirst = LBound(vLoop)
iLast = UBound(vLoop)
ReDim v(iFirst To iLast)
dMin = Application.Min(vLoop)
iMin = Application.Match(dMin, vLoop, 0) + iFirst - 1
j = iFirst
For i = iMin To iLast - 1
    v(j) = vLoop(i)
    j = j + 1
Next
If iMin > iFirst Then
    For i = iFirst To iMin - 1
        v(j) = vLoop(i)
        j = j + 1
    Next
End If
v(iLast) = v(iFirst)
For i = iFirst To iLast
    vLoop(i) = v(i)
Next
End Sub

Open in new window

Before I embark upon modifying the code to work with a collection, could you be more specific about which property of the objects in the collection should be minimized?
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 39744911
I am not sure about what you mean by
which property of the objects in the collection should be minimized?


I have something like

Sub Test()
Dim vLoop As New Collection
Dim vResult As Variant
Dim i As Long
vLoop.Add 19
vLoop.Add 33
vLoop.Add 32
vLoop.Add 31
vLoop.Add 6
vLoop.Add 10
vLoop.Add 11
vLoop.Add 19
RotateLoop vLoop
For i = 1 To 8
    Debug.Print i & "    " & vLoop(i)
    Next
End Sub
0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39744931
Collection rotating sub:
Sub RotateLoop(vLoop As Collection)
Dim dMin As Double
Dim i As Long, iMin As Long, n As Long
n = vLoop.Count
dMin = 1E+307
For i = 1 To n
    If vLoop(i) < dMin Then
        iMin = i
        dMin = vLoop(i)
    End If
Next
If iMin > 1 Then
    vLoop.Remove n
    For i = 1 To iMin
        vLoop.Add vLoop(1)
        If i < iMin Then vLoop.Remove 1
    Next
End If
End Sub

Open in new window

0
 
LVL 43

Author Closing Comment

by:Saqib Husain, Syed
ID: 39744941
Perfect, thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question