Find cells with a given value and copy the two cells to the left...

Phil Smith

Active Member
Joined
Aug 13, 2004
Messages
285
Office Version
  1. 365
Platform
  1. Mobile
On my worksheet "Roster", I need to find which cells in columns I, M, Q, U & Y contain "SP".

At the moment I am looping through I2:I71, M2:M71, Q2:Q71, U2:U71 and Y2:Y71.

For example, the first cell that contains "SP" is I9. I would then need to copy G9:H9 and paste those values to cells AG4:AH4 on worksheet "Data".

The next instance would then be pasted to AG5:AH5 and so on.

Is there a quicker way rather than looping through each of the 70 rows in each column? Once I have that syntax, I think I can work the rest out.

Thanks in advance!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Sub Solution()
On Error Resume Next
r=4
With Sheets("Roster").Range("I2:I71,M2:M71,Q2:Q71,U2:U71,Y2:Y71")
Set cel=.Find("SP",Lookat:=xlwhole)
Set First=cel
Do
If err.number<>0 then exit sub
cel.offset(,-2).resize(1,2).copy Sheet("Data").cells(rw,33)
rw=rw+1
Set cel=.Findnext(after:=cel)
If cel.address=First.address then exit sub
Loop
End With
End Sub
 
Upvote 0
Thanks for your solution, however I had to change r=4 to rw=4 to get it to work.

It does what I need with the exception that it copies the formatting as well, whereas I just need the values.

Can that be achieved?
 
Upvote 0
The r=4 was a typo, sorry. Yes, it should be rw=4.
The solution is change
cel.offset(,-2).resize(1,2).copy Sheets("Data").cells(rw,33)

to

cel.offset(,-2).resize(1,2).copy
Sheets("Data").Cells(rw,33).Pastespecial xlpastevalues
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,273
Members
448,559
Latest member
MrPJ_Harper

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