Solved

Shortcut key or Macro to swap items in list?

Posted on 2011-03-22
6
462 Views
Last Modified: 2012-05-11
I guess the parameters would be:
item 1 - after the comma up to, but not including the "and"
item 2 - after the "and" up to, but not including the ending punctuation.

before: item 1 and item 2.
after: item 2 and item 1.

Swap thusly in one key stoke instead of two highlight point and drag moves:  it’s all about getting the ‘and’ re-situated after swamping the nouns.  There really ought to be a way considering how often people have to do this.

Before:
In my home made trail mix there are:  pumpkin seeds, walnuts, sugar roasted fennel seeds, raisins, date pieces and sunflower seeds.

After:
In my home made trail mix there are:  pumpkin seeds, walnuts, sugar roasted fennel seeds, raisins, sunflower seeds and date pieces.

 
0
Comment
Question by:Alex972
  • 3
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35193690
Obviously, the principle would be to save the first item somewhere (in a variable, perhaps), delete it from the last, and then add the saved item after the next one.

For more specific advice, I think that we need to know the structure of the list. Is it an item in a VBA ListBox, or perhaps some text in an Excel cell or in a Word document, or something else?
0
 

Author Comment

by:Alex972
ID: 35193825
Just in a Word document in a simple sentence as in the example:

Before:
In my home made trail mix there are:  pumpkin seeds, walnuts, sugar roasted fennel seeds, raisins, date pieces and sunflower seeds.

After:
In my home made trail mix there are:  pumpkin seeds, walnuts, sugar roasted fennel seeds, raisins, sunflower seeds and date pieces.



0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35193939
One example would be to call:


swapand  "In my home made trail mix there are:  pumpkin seeds, walnuts, sugar roasted fennel seeds, raisins, date pieces and sunflower seeds."

which returns

In my home made trail mix there are:  pumpkin seeds, walnuts, sugar roasted fennel seeds, raisins,sunflower seeds and date pieces.

Chris
Function swapAnd(str As String) As String
Dim arr As String
Dim bolStop As Boolean
Dim posAnd As Long
Dim strtemp As String
Dim arrLeft() As String
Dim strLeft As String
Dim strRight As String

    bolStop = CBool(Right(str, 1) = ".")
    If bolStop Then str = Left(str, Len(str) - 1)
    posAnd = InStrRev(str, " and ")
    strRight = Trim(Right(str, Len(str) - posAnd - 4))
    If posAnd = 0 Then
        swapAnd = str
        Exit Function
    End If
    arrLeft = Split(Trim(Left(str, posAnd)), ",")
    strLeft = Trim(arrLeft(UBound(arrLeft)))
    arrLeft(UBound(arrLeft)) = ""
    swapAnd = Join(arrLeft, ",") & strRight & " and " & strLeft
    If bolStop Then swapAnd = swapAnd & "."
    
End Function

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Alex972
ID: 35193974
How would I use it in Word?

I opened the Macro Editor and pasted it, but when i go to run it, swapand does not show up.
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 35194087
Slight change in direction then ... use the following which operates on the selected text.

Chris
Sub swapAnd()
Dim str As String
Dim arr As String
Dim bolStop As Boolean
Dim posAnd As Long
Dim strtemp As String
Dim arrLeft() As String
Dim strLeft As String
Dim strRight As String
Dim newStr As String

    str = Selection.Text
    bolStop = CBool(Right(str, 1) = ".")
    If bolStop Then str = Left(str, Len(str) - 1)
    posAnd = InStrRev(str, " and ")
    strRight = Trim(Right(str, Len(str) - posAnd - 4))
    If posAnd = 0 Then
        Exit Sub
    End If
    arrLeft = Split(Trim(Left(str, posAnd)), ",")
    strLeft = Trim(arrLeft(UBound(arrLeft)))
    arrLeft(UBound(arrLeft)) = ""
    newStr = Join(arrLeft, ",") & strRight & " and " & strLeft
    If bolStop Then newStr = newStr & "."
    Selection.TypeBackspace
    Selection.TypeText Text:=newStr
    
End Sub

Open in new window

0
 

Author Closing Comment

by:Alex972
ID: 35194157
cool
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now