Solved

Shortcut key or Macro to swap items in list?

Posted on 2011-03-22
6
467 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:Alex Campbell
[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
  • 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:Alex Campbell
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
Revamp Your Training Process

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

 
LVL 1

Author Comment

by:Alex Campbell
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:Alex Campbell
ID: 35194157
cool
0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
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…

695 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