Solved

Shortcut key or Macro to swap items in list?

Posted on 2011-03-22
6
463 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Problem: You created a new custom form in Outlook for your contacts (added fields, deleted fields, changed the layout of fields, whatever) and made it the default form for contacts. The good news is that all new contacts will utilize the new form. T…
In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

912 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

21 Experts available now in Live!

Get 1:1 Help Now