Select range using Endx1Down

agcoleman

New Member
Joined
Dec 16, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am trying to get a code that will search for a term, highlight the row that term appears in, then select the range between that term and the next time data shows up in that column.
Below is what I came up with so far. I know through recent research that activate and select aren't the best to use so if there is a better way I am open to it. Everything works up until the last row and then it gives me a run time 1004 error.

VBA Code:
Sheets("Labor Report").Activate
Sheets("Labor Report").Range("A1").EntireColumn.Select
Selection.Find(What:="US Last code", After:=ActiveCell, LookIn:= _
        xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Select
Selection.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
 
Last edited by a moderator:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this


VBA Code:
Sub select_range()
  Dim f As Range
  
  Sheets("Labor Report").Activate
  Set f = Range("A:A").Find("US Last code", , xlValues, xlPart, xlByRows, xlNext, False)
  If Not f Is Nothing Then
    Range(f, f.End(xlDown)).Select
  End If
End Sub

Before run the macro:
1702678903434.png

After:
1702678944268.png

Comment if it is what you need, otherwise, describe with examples what you have and what you expect as a result.


I hope to hear from you very soon.
Cordially.
Dante Amor
🧙‍♂️
 
Upvote 0
Try this


VBA Code:
Sub select_range()
  Dim f As Range
 
  Sheets("Labor Report").Activate
  Set f = Range("A:A").Find("US Last code", , xlValues, xlPart, xlByRows, xlNext, False)
  If Not f Is Nothing Then
    Range(f, f.End(xlDown)).Select
  End If
End Sub

Before run the macro:

After:

Comment if it is what you need, otherwise, describe with examples what you have and what you expect as a result.


I hope to hear from you very soon.
Cordially.
Dante Amor
🧙‍♂️
Thanks for the reply unfortunately this didn't seem to get what i was looking for. I pasted a picture below for better reference. Once I get to the correct sheet I want to search in column A until i find "US Last code". I then want to highlight the entire row that term is on and then highlight all of the rows below it until the next time US Last code appears. Once I do that I am going to copy that selection and then paste it under the send line that US Last code appears on.

1702693979457.png
 
Upvote 0
I still don't understand what you need. That is why it is important that you put an image of what you have before running the macro and another image with the result you want.

Based on your image, what do you want to copy and where do you want to paste it?

Forget that you first want to select, simply explain, from your image, what you want to copy and where to paste.

It would help if you explain it in detail and with pictures.

:unsure:
 
Upvote 0
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. 😊

My guess is that the code shown (generally better to show the whole code unless extremely long) is in a worksheet module (not the 'Labor Repot' sheet's module) as that would cause that last line to give a '1004' error.

If that code needs to be in that other worksheet's module then this should fix that error.

Rich (BB code):
Sheets("Labor Report").Activate
Sheets("Labor Report").Range("A1").EntireColumn.Select
Selection.Find(What:="US Last code", After:=ActiveCell, LookIn:= _
        xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Select
Selection.EntireRow.Select
Sheets("Labor Report").Range(Selection, Selection.End(xlDown)).Select

Having said that, there is rarely a need to activate/select items to work with them in vba and doing so generally slows your code considerably.
If you wanted to look at alternatives to activating/selecting then please provide more details about just what you have and what you are trying to do as suggested by Dante.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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