LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Code copies between key word is in reverse order

I dug this out of my archives, modified it slightly for a Poster.

Using XX and XXX as "key words" multiple times in column A, it does pretty well. Grabs the ranges from the Start Key word to the End Key word (including the Start and End words), puts them in column B, clears column A and returns the data from column B back to column A.

The end result in column A is accurate, however, it is in reverse order of how it was originally listed in A. Would prefer it to be in same order as original.

Another preference would be to only take the data BETWEEN the start and end words and when copied to column B, a blank cell would be between each range.

I'm pretty sure I can just go to column B and remove the start and end words with extra code before bringing column B back to A. Was wondering if it makes better sense to just offset from start word one cell down and from end word one cell up and move that range segment to B, perhaps with an offset(1, 0) to produce the blank between each range in column B.

But I can't figure how to exclude the start and end words.

Any suggestions?
Thanks,
Howard

Option Explicit

Sub Copy_Twixt_Keywords()

Dim rngKeyWordStart As Range, rngKeyWordEnd As Range
Dim strKeyWordStart As String, strKeyWordEnd As String, FirstFound As String

'strKeyWordStart = Range("K1").Value
strKeyWordStart = "XX"

'strKeyWordEnd = Range("K2").Value
strKeyWordEnd = "XXX"

Application.ScreenUpdating = False
With Sheets("Sheet1")
Set rngKeyWordStart = .Range("A:A").Find(What:=strKeyWordStart, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If Not rngKeyWordStart Is Nothing Then

FirstFound = rngKeyWordStart.Address

Set rngKeyWordEnd = .Range("A:A").Find(What:=strKeyWordEnd, _
After:=rngKeyWordStart)

If Not rngKeyWordEnd Is Nothing Then
Do
.Range(rngKeyWordStart, rngKeyWordEnd).Copy
Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).PasteSpecial xlPasteValues

Set rngKeyWordStart = .Range("A:A").Find(What:=strKeyWordStart, _
After:=rngKeyWordEnd)
Set rngKeyWordEnd = .Range("A:A").Find(What:=strKeyWordEnd, _
After:=rngKeyWordStart)

Loop While rngKeyWordStart.Address < FirstFound And _
rngKeyWordEnd.Row rngKeyWordStart.Row
Else
MsgBox "Cannot find a match for the 'End' keyword: " & _
vbLf & """" & strKeyWordEnd & """", _
vbExclamation, "No Match Found"
End If

Else
MsgBox "Cannot find a match for the 'Start' keyword: " & _
vbLf & """" & strKeyWordStart & """", _
vbExclamation, "No Match Found"
End If

End With

Application.CutCopyMode = True
Application.ScreenUpdating = True

Range("A:A").ClearContents
Range("B:B").Copy Range("A1")
Range("B:B").ClearContents
End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reverse Row Order? trvlnmny New Users to Excel 3 May 30th 10 08:22 AM
Reverse name order xp Excel Programming 4 August 12th 09 07:27 PM
Reverse Order PL Excel Discussion (Misc queries) 1 June 17th 09 02:47 AM
Reverse Order PL Excel Discussion (Misc queries) 2 June 15th 09 09:17 AM
reverse order Lamb Chop Excel Discussion (Misc queries) 2 June 20th 06 03:37 PM


All times are GMT +1. The time now is 11:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"