Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2014, 09:09 AM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Macro to reverse a test string Windows XP Macro to reverse a test string Office 2007
Competent Performer
Macro to reverse a test string
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default Macro to reverse a test string

I wrote this little macro to reverse a text string. I have 2 questions:



  1. How can get the macro to leave the selection selected?
  2. Does anyone have any comments on the code? Is there a better way?

Code:
Sub MyRevText()
Dim oRange As Range
Dim sString As String

Set oRange = Selection.Range
sString = oRange.Text
sString = StrReverse(sString)
oRange.Text = sString

End Sub
Reply With Quote
  #2  
Old 01-16-2014, 10:26 AM
gmaxey gmaxey is offline Macro to reverse a test string Windows 7 32bit Macro to reverse a test string Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,439
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub MyRevText()
Dim oRng As Range
Set oRng = Selection.Range
oRng.Text = StrReverse(oRng.Text)
oRng.Select
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 01-16-2014, 11:12 AM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Macro to reverse a test string Windows XP Macro to reverse a test string Office 2007
Competent Performer
Macro to reverse a test string
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default

Thank you, that works perfectly.

I wasn't sure if I needed the variable oString.

Is there some reason that you changed my variable name from oRange to oRng?

I just noticed another little problem. The entire reversed string gets assigned the formatting of the first character in the original string.

If my original string is "This is some text", the reversed string becomes "txet emos si sihT". Is there a way to preserve the formatting so that the result is "txet emos si sihT"?
Reply With Quote
  #4  
Old 01-16-2014, 11:16 AM
gmaxey gmaxey is offline Macro to reverse a test string Windows 7 32bit Macro to reverse a test string Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,439
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You're welcome.

I'm an old dog that likes to use the same hydrant. It's just my style.

I don't know an answer to the issue with formatting. Sorry.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 01-16-2014, 11:23 AM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Macro to reverse a test string Windows XP Macro to reverse a test string Office 2007
Competent Performer
Macro to reverse a test string
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
You're welcome.

I'm an old dog that likes to use the same hydrant. It's just my style.

I don't know an answer to the issue with formatting. Sorry.
Yuck! That's an image I'll have to try to get out of my head. That must be one disgusting hydrant. Pity the poor firemen if there's ever a fire near there. Gross! (Pretty funny line, though.)

I bet if I did it in a loop where I built up a new string character by character, it would preserve the formatting. That's more work than it's worth for what I need.
Reply With Quote
  #6  
Old 01-16-2014, 03:19 PM
gmaxey gmaxey is offline Macro to reverse a test string Windows 7 32bit Macro to reverse a test string Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,439
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Never considered the image that quip might conjure

This might work:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oRngBefore As Word.Range
Dim i As Long
Dim bMove As Boolean
Set oRng = Selection.Range.Duplicate
Set oRngBefore = oRng.Duplicate
oRngBefore.Collapse wdCollapseStart
bMove = True
For i = 1 To oRng.Characters.Count
  oRng.Characters(i).Copy
  oRngBefore.Paste
  If bMove Then
    oRng.MoveStart wdCharacter, 1
    bMove = False
  End If
  oRngBefore.Collapse wdCollapseStart
Next
oRng.Delete
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to reverse a test string Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM
Macro to reverse a test string Macro to create new word doc and save the file using String found in the document VBNation Word VBA 2 02-08-2013 07:14 AM
Macro to reverse a test string Call Macro from Input Box String Paulsh Word VBA 1 11-01-2011 03:15 AM
Macro to reverse a test string Macro VBA "Save as" with bookmarks in file name string Dom37 Word VBA 2 10-31-2011 03:28 AM
Macro to reverse a test string Macro or Function to know wether a string is included in a text Eduardo Word VBA 5 06-15-2009 01:55 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:51 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft