Find, Copy,and Paste

JGM705

New Member
Joined
Nov 3, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I'm in need of some assistance. I have a sheet that has data in Columns U through AZ. I am attempting to search each row to find specific text. Once that text is found in a cell, I want to copy that cell value and paste it in the same row but in column BA. I have hodgepodge various things together from various articles. But I seem to be getting nowhere. Any help would be greatly appreciated.

Rich (BB code):
Sub SearchAndCopy

     Dim ws As Worksheet
     Dim searchValue As String
     Dim lastRow As Long
     Dim i As Long


     Set ws = Worksheets("Sheet1") ' Set the worksheet where you want to perform the search

     searchValue = "LNR"        ' Set the search value 
     lastRow = ws.Cells(ws.Rows.Count, "U").End(xlUp).Row    ' Find the last row with data in column U 
 
    For i = 2 To lastRow  ' Loop through each row
        If WorksheetFunction.CountIf(ws.Range("U" & i & ":AZ" & i), searchValue) > 0 Then 'Check if the search value is found in cells U2 to AZ1338
       Set foundCell = ws.Rows(i).Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False  ' Find the  cell with the search value in the row

            If Not foundCell Is Nothing Then
               ws.Cells(i, "58").Value = foundCell.Value     ' Copy the value to column BA in the same row

             End If
        End If
     Next i   
End Sub
 
Last edited by a moderator:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this

VBA Code:
Sub SearchAndCopy()

Dim ws As Worksheet
Dim searchValue As String
Dim lastRow As Long
Dim i As Long


Set ws = Worksheets("Sheet1") ' Set the worksheet where you want to perform the search

searchValue = "LNR" ' Set the search value
lastRow = ws.Cells(ws.Rows.Count, "U").End(xlUp).Row ' Find the last row with data in column U

For i = 2 To lastRow ' Loop through each row
If WorksheetFunction.CountIf(ws.Range("U" & i & ":AZ" & i), searchValue) > 0 Then 'Check if the search value is found in cells U2 to AZ1338
Set foundCell = ws.Rows(i).Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) ' Find the cell with the search value in the row

If Not foundCell Is Nothing Then
ws.Cells(i, 58).Value = foundCell.Value ' Copy the value to column BA in the same row

End If
End If
Next i
Stop
End Sub
 
Upvote 0
Hi @JGM705. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

ws.Cells(i, "58").Value = foundCell.Value '
Just so that you take it into account for the future, in this case the 58 should be without quotes.

But try this macro:

VBA Code:
Sub SearchAndCopy()
  Dim c As Range, r As Range, f As Range, cell As String
  Dim ws As Worksheet
  Dim searchValue As String

  searchValue = "LNR"
  Set ws = Sheets("Sheet1")
  Set r = ws.Range("U:AZ")
  Set f = r.Find(searchValue, , xlValues, xlWhole)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      ws.Range("BA" & f.Row).Value = searchValue
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Solution
@JGM705
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0
Hi @JGM705. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.


Just so that you take it into account for the future, in this case the 58 should be without quotes.

But try this macro:

VBA Code:
Sub SearchAndCopy()
  Dim c As Range, r As Range, f As Range, cell As String
  Dim ws As Worksheet
  Dim searchValue As String

  searchValue = "LNR"
  Set ws = Sheets("Sheet1")
  Set r = ws.Range("U:AZ")
  Set f = r.Find(searchValue, , xlValues, xlWhole)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      ws.Range("BA" & f.Row).Value = searchValue
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Thank you Dante, this is very close to what I'm trying to accomplish. I should have clarified the cells in th U:AZ range will include more than just LNR, such as LNR 0 1236 or LNR 5 4329. So I was searching on just LNR but wanted to capture the full value of the cell and paste to column BA. It appears your code ends up pasting the searchvalue and not the cell value.
 
Upvote 0
@JGM705
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
Thank you Peter, the trials and tribulations of a newbie
 
Upvote 0
Try this

VBA Code:
Sub SearchAndCopy()

Dim ws As Worksheet
Dim searchValue As String
Dim lastRow As Long
Dim i As Long


Set ws = Worksheets("Sheet1") ' Set the worksheet where you want to perform the search

searchValue = "LNR" ' Set the search value
lastRow = ws.Cells(ws.Rows.Count, "U").End(xlUp).Row ' Find the last row with data in column U

For i = 2 To lastRow ' Loop through each row
If WorksheetFunction.CountIf(ws.Range("U" & i & ":AZ" & i), searchValue) > 0 Then 'Check if the search value is found in cells U2 to AZ1338
Set foundCell = ws.Rows(i).Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) ' Find the cell with the search value in the row

If Not foundCell Is Nothing Then
ws.Cells(i, 58).Value = foundCell.Value ' Copy the value to column BA in the same row

End If
End If
Next i
Stop
End Sub
Thanks for the response AC. I had to remove the STOP after the Next i. However, it does not appear to be copying the cell value just pasting the search value. I should have been clearer about the paste into column BA. The cell value will be more than just LNR like LNR 5 1235 or LNR 0 1222222. I figured it would be easier to search for LNR since its common, then copy the full cell value to BA
 
Upvote 0
Thank you Dante, this is very close to what I'm trying to accomplish. I should have clarified the cells in th U:AZ range will include more than just LNR, such as LNR 0 1236 or LNR 5 4329. So I was searching on just LNR but wanted to capture the full value of the cell and paste to column BA. It appears your code ends up pasting the searchvalue and not the cell value.
Please disregard. I changed the search value to LNR* and that appears to produce the desired results
 
Upvote 0
Hi @JGM705
The cell value will be more than just LNR like LNR 5 1235 or LNR 0 1222222.
It would be nice if you wrote these requirements from the original post, that way the solutions delivered will be more complete.

I'm glad to know you solved it.


---------------------
If the value to be searched is found anywhere in the cell, it can also be: "*LNR*"
Or also changing the search parameter to xlPart.

I also changed the result to f.Value so you get the actual value of the cell that contains "LNR"

Rich (BB code):
Sub SearchAndCopy()
  Dim c As Range, r As Range, f As Range, cell As String
  Dim ws As Worksheet
  Dim searchValue As String

  searchValue = "LNR"                       'or "*LNR*" o "LNR*"
  Set ws = Sheets("Sheet1")
  Set r = ws.Range("U:AZ")
  Set f = r.Find(searchValue, , xlValues, xlPart)   'or xlwhole
  If Not f Is Nothing Then
    cell = f.Address
    Do
      ws.Range("BA" & f.Row).Value = f.Value
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
End Sub

Im glad to help you, thanks for the feedback.
 
Upvote 0
Hi @JGM705

It would be nice if you wrote these requirements from the original post, that way the solutions delivered will be more complete.

I'm glad to know you solved it.


---------------------
If the value to be searched is found anywhere in the cell, it can also be: "*LNR*"
Or also changing the search parameter to xlPart.

I also changed the result to f.Value so you get the actual value of the cell that contains "LNR"

Rich (BB code):
Sub SearchAndCopy()
  Dim c As Range, r As Range, f As Range, cell As String
  Dim ws As Worksheet
  Dim searchValue As String

  searchValue = "LNR"                       'or "*LNR*" o "LNR*"
  Set ws = Sheets("Sheet1")
  Set r = ws.Range("U:AZ")
  Set f = r.Find(searchValue, , xlValues, xlPart)   'or xlwhole
  If Not f Is Nothing Then
    cell = f.Address
    Do
      ws.Range("BA" & f.Row).Value = f.Value
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
End Sub

Im glad to help you, thanks for the feedback.
My apologies for not clearly stating the cell value would contain more than just the search value. I appreciate you help and thank you again.
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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