Write contents of cell from one tab to another after .find

Laavista

Board Regular
Joined
Aug 27, 2009
Messages
79
I'm using Excel 2003. I have two tabs: AllTxt & ExtractInfo
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
In 'Alltxt', I need to find each occurence of ***. If found, I need to write the contents of that cell into tab "ExtractInfo".
<o:p> </o:p>
The *** could be in different columns (A, B, C, or D)
<o:p> </o:p>
I'm using ".find" and it does find it. Now I need to write it to ExtractInfo, but nothing seems to be working.
<o:p> </o:p>
My code:
AllTxtLastRow is a variable (long) and does contact the last row number in tab AllTxt
ExtInfoBlankRow is a variable (long) and does contain the last row in tab ExtractedInfo
<o:p> </o:p>
strTextToFind = "~***"
<o:p> </o:p>
With Sheets("AllTxt").Range("A1":D" & AllTxtLastRow)
<o:p> </o:p>
CheckNext:
<o:p> </o:p>
Set C = .Find(strTxtToFind, LookIn:=xlValues) '(this works)
<o:p> </o:p>
If Not C is Nothing then 'finds the ***
FoundAddress = C.Address 'this is correct, e.g., $B$40
FoundRow = C.Row 'foundrow contains the correct row, e.g., 40
FoundCol = C.Column 'foundcol contains the correct col, e.g, B
<o:p> </o:p>
' ==========================<o:p></o:p>
'I'VE TRIED ABOUT 15+ THINGS, NOTHING WORKS TO WRITE THE CONTENTS INTO EXTRACTINFO TAB<o:p></o:p>
' A FEW FOLLOW<o:p></o:p>
<o:p></o:p>
'this does work--I get 'subscript out of range'<o:p></o:p>
Contents = Sheets("ExtractInfo").Cells(FoundRow,FoundCol) <o:p></o:p>
<o:p> </o:p>
'this does not work--I get Application-defined or object-defined error<o:p></o:p>
Worksheets("ExtractInfo").Range("A" & ExtInfoBlankRow, "A"). value = Worksheets("AllTxt").Range(FoundRow, FoundCol).value<o:p></o:p>
' ==============================<o:p></o:p>
<o:p> </o:p>
ActiveCell.Offset(1,0).select 'move to the next row down
ExtInfoBlankRow = ExtInfoBlankRow + 1
Goto CheckNext
<o:p> </o:p>
Else
Goto GetNextFile
end if
end with
<o:p> </o:p>
GetNextFile:
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
YOUR HELP WOULD BE SO APPRECIATED!
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>

<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
does this help you out? It will look for an instance of *** and if found it will reference the cell address for later,and it will transfer the cell contents to the next avaiable row on ExtractInfo

Then it loops through the find statement checking for all instances, copying the value to the next cell on ExtractInfo by increasing ExtInfoBlankRow by one each time it find it.

Once the loop gets back to the first cell it found it jump to GetNextFile

If it finds no instance it also jump to GetNextFile (which in my code just exits the sub).

Tell me how it goes...if you need any more help let me know :)

Code:
Sub Find_String_Loop()

On Error GoTo GetNextFile

ExtInfoBlankRow = Sheets("ExtractInfo").Range("A1").CurrentRegion.Count + 1
    
Sheets("AllTxt").Select
    Range("A1").Select

strTxtToFind = "~***"

Cells.Find(What:=strTxtToFind, _
                    After:=ActiveCell, _
                    LookIn:=xlValues, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False, _
                    SearchFormat:=False).Activate

TxtFirstAddress = ActiveCell.Address

    Sheets("ExtractInfo").Range("A" & ExtInfoBlankRow).Value = _
        Range(TxtFirstAddress).Value

ExtInfoBlankRow = ExtInfoBlankRow + 1

Do

Cells.Find(What:=strTxtToFind, _
                    After:=ActiveCell, _
                    LookIn:=xlValues, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False, _
                    SearchFormat:=False).Activate

TxtLoopAddress = ActiveCell.Address
                    
If TxtLoopAddress = TxtFirstAddress Then GoTo GetNextFile

    Sheets("ExtractInfo").Range("A" & ExtInfoBlankRow).Value = _
        Range(TxtLoopAddress).Value
                    
ExtInfoBlankRow = ExtInfoBlankRow + 1

Loop

Exit Sub

GetNextFile:
Exit Sub

End Sub
 
Upvote 0
WOW--it worked the first time!

I cannot tell you how much I appreciate this. I worked on just this problem for over 2 hours and was so frustrated.

Thank you for taking the time to post this code. You are incredible!
 
Upvote 0
thats great news! thanks for letting me know it worked...

Let me know if you come up against any other 'challenges'!! :)
 
Upvote 0
Another question

(I hope this is not a duplicate post. I posted it last night, but I don't see it displayed...)

May I bother you once more? Your code worked perfectly, but I just found out that the line(s) following the *** line found have to be picked up and concatenated to the contents of the *** line if the following line(s) is not null or if it doesn't begin with ***. I do not know the possible number of lines following the ***, so will probably need to use an array. I've tried to incorporate this, but after hours (and hours) of trying different things, it is not working.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
The scenario:<o:p></o:p>
Data example:<o:p></o:p>
<o:p></o:p>
In tab "AllTxt"<o:p></o:p>
(in cell D2) ***This is a test line<o:p></o:p>
(in cell D3) ***2nd test line<o:p></o:p>
(in cell D4) this line has to be picked up and concatenated with cell D3<o:p></o:p>
(in cell D5) this has to be picked up & concatenated with D3 & D4<o:p></o:p>
(in cell D6) ***WhosOnFirst<o:p></o:p>
(in cell D7) ***BigMac<o:p></o:p>
(in cell D8) this line has to be concatenated with D7<o:p></o:p>
(Cell 9) (blank line)<o:p></o:p>
<o:p></o:p>
Once a cell is found that starts with ***, I need to check if there are subsequent lines that go with it. If the next line is NULL or if the next line begins with ***, then I just need to write the one line to the worksheet (same as the code you gave me previously).<o:p></o:p>
<o:p> </o:p>
However, when a *** is found and the next line is not null or does not start with ***, then I need to concatenate that line with the previous, then check the same thing for the NEXT line. Is the 3rd line null or begin with ***? If yes, then check for a 4th, etc. until the next line is null or starts with an ***. The concatenated line is written to the tab 'ExtractInfo<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Pseudo code:<o:p></o:p>
Do until no more lines with ***<o:p></o:p>
In tab AllTxt:<o:p></o:p>
find line starting with ***<o:p></o:p>
Check next line<o:p></o:p>
If null or contents begins with ***, write contents of line to ExtractInfo tab<o:p></o:p>
Find next line with ***<o:p></o:p>
<o:p></o:p>
<o:p> </o:p>
If not null or contents do not begin with *** then:<o:p></o:p>
do until line is blank or line begins with ***<o:p></o:p>
check next line<o:p></o:p>
If NOT null or contents does not begin with ***, then <o:p></o:p>
concatenate this line to previous line(s)<o:p></o:p>
loop<o:p></o:p>
<o:p></o:p>
========= <o:p></o:p>
For the data listed above, the results would be:
<o:p> </o:p>
The following line would be written in the ExtractInfo tab<o:p></o:p>
***This is a test line (e.g., written in cell E2)<o:p></o:p>
<o:p> </o:p>
The following line would be written in the ExtractInfo tab<o:p></o:p>
***2nd test line this line has to be picked up and concatenated with cell D3 this has to be picked up & concatenated with D3 & D4 (e.g., written in cell E3)<o:p></o:p>
<o:p> </o:p>
The following line would be written in the ExtractInfo tab<o:p></o:p>
***WhosOnFirst (e.g., written in cell E4)
<o:p> </o:p>
The following line would be written in the ExtractInfo tab<o:p></o:p>
***BigMac this line has to be concatenated with D7 (e.g., written in cell E5)<o:p></o:p>
<o:p> </o:p>
If you could help, I'd REALLY appreciate it.<o:p></o:p>
<o:p> </o:p>
Thanks!<o:p></o:p>
<o:p> </o:p>
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top