Find text and copy row

fari1

Active Member
Joined
May 29, 2011
Messages
362
hi, my below code finds the text "consolidated" and in sheet3 list all the cells that contain consolidated. i want this code to copy the whole instead of that cell which contains consolidated and gives the output in sheet3.e.g


HTML:
      A             B            C                    D                  E
        Consolidated                 sheets             result

with reference to the above example i want to get the complete row copied as a result of this code in sheet3.the code is below

Code:
Sub findTEXT()
Dim f As Range, fa As String, i As Long
Dim src As Worksheet, dst As Worksheet
Set src = Sheets("sheet2") 'sheet to be searched, change as required
Set dst = Sheets("sheet3") 'sheet for output, change as required
i = 1
With dst
    Set f = src.Cells.Find(What:="CONSOLIDATED", After:=src.Cells(1), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
fa = f.Address
Do
    If Len(f.Value) < 50 Then
    .Range("A" & Rows.Count).End(xlUp)(2) = f.Value
    .Range("B" & Rows.Count).End(xlUp)(2) = src.Range("A1").Value
i = i + 1
End If
    Set f = src.Cells.FindNext(f)
    Loop Until fa = f.Address
End If
End With
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I'm not sure I understand what you need to accomplish.

Are you saying that for every row in sheet2 that has "CONSOLIDATED",
you want that row put on sheet3?

Post sheet example of sheet2 and sheet example of what you need to end up with.

Why the "If Len(f.Value) < 50" ?
 
Upvote 0
here's the example of sheet2

<b>Sheet2</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:209px;" /><col style="width:300px;" /><col style="width:568px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td><td >C</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4939</td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4940</td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4941</td><td >CONSOLIDATED FINANCIAL STATEMENTS </td><td > </td><td >Description</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4942</td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4943</td><td >CITIGROUP INC. AND SUBSIDIARIES </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4944</td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4945</td><td >CONSOLIDATED STATEMENT OF INCOME (Unaudited) </td><td > </td><td > </td></tr></table> <br />Excel tables to the web - Excel Jeanie Html 4


in above i want to copy the whole row, in which any cell that contain the word consolidated, in above case row 4941 and row 4945 i want.

and i dun want the rows where len is greater than 50, in the code that i provided, the len is for one particular cell than contains the word consolidated, can u make it work, to get the len of whole row, and see the len of all the cells in that row and then see if it is greater than 50 then dun give that row as output
 
Upvote 0
Do we only need to search fo "consolidated" in Col A?
 
Upvote 0
fari1,

Give the following a try:
Code:
Sub findTEXT()
    
    Static wsSrc As Worksheet: Set wsSrc = ActiveWorkbook.Sheets("Sheet2")
    Static wsDst As Worksheet: Set wsDst = ActiveWorkbook.Sheets("Sheet3")
    
    Dim rngFnd As Range
    Dim rngAll As Range
    Dim allfound As Boolean
    
    Set rngFnd = wsSrc.UsedRange.Find("consolidated")
    While Not rngFnd Is Nothing And allfound = False
        If rngAll Is Nothing Then Set rngAll = rngFnd
        Set rngFnd = wsSrc.UsedRange.Find("consolidated", rngFnd)
        If Intersect(rngAll, rngFnd) Is Nothing Then
            If Intersect(rngAll.EntireRow, rngFnd) Is Nothing Then Set rngAll = Union(rngAll, rngFnd)
        Else
            allfound = True
        End If
    Wend
    If Not rngAll Is Nothing Then rngAll.EntireRow.Copy wsDst.Cells(Rows.Count, "A").End(xlUp).Offset(1)
    
End Sub



Hope that helps,
~tigeravatar
 
Upvote 0
hi, thanks for for the code, u didn't define the LEN in the code, i dont want those rows, where LEN of the total row (where one of the cell contains consolidated) is more than 50.
 
Upvote 0
fari1,

So in a row that contains "consolidated", if all the cells' text combined exceeds 50 characters then you do not want that row?
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,676
Members
452,937
Latest member
Bhg1984

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