Copy cells if the specific text is present in the cell

Maruthiveer

New Member
Joined
Apr 12, 2020
Messages
15
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
  2. Mobile
I have an excel sheet with units on Column B, and the dates on the row A1 as headers from "F"., if any cell contain the word apple , the need to copy that respective ID and the date in a new sheet.

For example :
Master sheet.
Sold byApple unitssaleMarketTotal Units
3/1/2020​
3/2/2020​
3/3/2020​
3/4/2020​
Ram
53​
198​
OrangeAppleBananaRose
Ravi
68​
189​
AppleRoseorangeApple

After running the code :

Result should be like:
Apple Unitsdates
53​
3/2/2020​
68​
3/1/2020​
68​
3/4/2020​

can anyone help me with the code, where the cells with text APPLE - relevant apple units and date will pulled on sheet2.

Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
VBA Code:
Sub t()
Dim c As Range, i As Long, sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("Master") 'Edit sheet name-Source sheet
Set sh2 = Sheets("Sheet2") 'Edit sheet name-destination sheet
    With sh1
        For Each c In .Range("F2", .Cells(Rows.Count, "F").End(xlUp))
            For i = 6 To .Cells(c.Row, Columns.Count).End(xlToLeft).Column
                If LCase(.Cells(c.Row, i)) = "apple" Then
                    sh2.Cells(Rows.Count, 1).End(xlUp)(2) = c.Offset(, -4).Value
                    sh2.Cells(Rows.Count, 1).End(xlUp).Offset(, 1) = .Cells(1, i).Value
                End If
            Next
        Next
    End With
End Sub
 
Upvote 0
Wow it's amazing. Thanks a lot

i am just wondering can we also pull the cells even the row containing the word apple has some text in it .

For example :
Sold byApple unitssaleMarketTotal Units3/1/20203/2/20203/3/20203/4/2020
Ram53198 Orange 7:00-15:00
Apple:
40
BananaRose
Ravi681898:00-16:00
Apple:
90
Roseorange10:00-19:00
Apple:
75

can we still get the data ? i tried with the above code but i didn't get the result .
 
Upvote 0
i am just wondering can we also pull the cells even the row containing the word apple has some text in it .

Maybe this

VBA Code:
Sub t2()
Dim c As Range, i As Long, sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("Master") 'Edit sheet name-Source sheet
Set sh2 = Sheets("Sheet2") 'Edit sheet name-destination sheet
    With sh1
        For Each c In .Range("F2", .Cells(Rows.Count, "F").End(xlUp))
            For i = 6 To .Cells(c.Row, Columns.Count).End(xlToLeft).Column
                If InStr(.Cells(c.Row, i).Value, "apple") > 0 Then
                    sh2.Cells(Rows.Count, 1).End(xlUp)(2) = c.Offset(, -4).Value
                    sh2.Cells(Rows.Count, 1).End(xlUp).Offset(, 1) = .Cells(1, i).Value
                End If
            Next
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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