Solved

Shortcut key or Macro to swap items in list?

Posted on 2011-03-22
6
464 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
 
LVL 1

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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

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
 
LVL 1

Author Closing Comment

by:Alex972
ID: 35194157
cool
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VB Help 4 51
Generating a graph via Excel 3 38
Macro 6 58
microsoft word short cuts 3 27
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…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…

785 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