How to find specific text and then copy the values below it

RookieExcel22

New Member
Joined
Aug 11, 2023
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have text "Comp43" that should always be in A4, but rarely could be in another cell such as A6 or B5 etc. I am hoping there is a way for VBA to find the cell that this value is in and then copy all of the data below it? For example if it appears in A4, it would copy paste from A5 to the last row. If it appears in B5, it would copy from B6 to the last row, etc.

Thank you!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi .. Try below code
VBA Code:
Sub test()

Dim Rg As Range, Lr&

With Sheets("Sheet1")
   Set Rg = .UsedRange.Find("Comp43", lookat:=xlWhole)
   If Rg Is Nothing Then
      MsgBox "Nohting found in " & .Name & ", macro will stop", vbExclamation
      Exit Sub
   End If
   Lr = Cells(Rows.Count, Rg.Column).End(3).Row
   Rg.Resize(Lr - Rg.Row + 1).Copy
   ' data is copied, you can paste it as desired
End With

End Sub
 
Upvote 0
Hi .. Try below code
VBA Code:
Sub test()

Dim Rg As Range, Lr&

With Sheets("Sheet1")
   Set Rg = .UsedRange.Find("Comp43", lookat:=xlWhole)
   If Rg Is Nothing Then
      MsgBox "Nohting found in " & .Name & ", macro will stop", vbExclamation
      Exit Sub
   End If
   Lr = Cells(Rows.Count, Rg.Column).End(3).Row
   Rg.Resize(Lr - Rg.Row + 1).Copy
   ' data is copied, you can paste it as desired
End With

End Sub
Is there a way to not include the "Comp43" cell? Just everything below it? Also how do I expand the range if I wanted to grab everything below and to another column?
 
Upvote 0
Small change in mse330 code:
VBA Code:
Sub test()

Dim Rg As Range, Lr&

With Sheets("Sheet1")
   Set Rg = .UsedRange.Find("Comp43", lookat:=xlWhole)
   If Rg Is Nothing Then
      MsgBox "Nohting found in " & .Name & ", macro will stop", vbExclamation
      Exit Sub
   End If
   Lr = Cells(Rows.Count, Rg.Column).End(3).Row
   Rg.Offset(1).Resize(Lr - Rg.Row + 1).Copy
   ' data is copied, you can paste it as desired
End With

End Sub
 
Upvote 1
Solution
Use .Resize(,5) to extend your selection by 5 columns (or adjust as needed
Code:
Rg.Offset(1).Resize(Lr - Rg.Row + 1,5).Copy
 
Upvote 1
Small change in mse330 code:
VBA Code:
Sub test()

Dim Rg As Range, Lr&

With Sheets("Sheet1")
   Set Rg = .UsedRange.Find("Comp43", lookat:=xlWhole)
   If Rg Is Nothing Then
      MsgBox "Nohting found in " & .Name & ", macro will stop", vbExclamation
      Exit Sub
   End If
   Lr = Cells(Rows.Count, Rg.Column).End(3).Row
   Rg.Offset(1).Resize(Lr - Rg.Row + 1).Copy
   ' data is copied, you can paste it as desired
End With

End Sub
Perfect, thank you.
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

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