Selection.End(xldown).Select variable number of times

Will85

Active Member
Joined
Apr 26, 2012
Messages
253
Office Version
  1. 365
Platform
  1. Windows
In column A, I have strategically placed the value of x in specific rows.

I use a macro that goes to column A and then executes selection.end(xldown).Select a certain number of times depending on which row I want it to land on.

For example if I want it to select the third row with an x I use:

Range("A1").Select
selection.end(xldown).Select
selection.end(xldown).Select
selection.end(xldown).Select


I would prefer to be able to tell it to go down a specific # of times based on the value in a cell on another sheet.

Additionally, I need it to look up this value, instead of pointing to a specific cell reference.

For example, Id want to index match the value based on an account number, the account number its looking up can be hardcoded inside the macro itself.

Sheet2 has account numbers in column A and the value I am seeking in column B.
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe
Code:
Sub Will85()
   Dim Fnd As Range
      
   Set Fnd = Sheets("Sheets2").Range("A:A").Find("ABC123", , , xlWhole, , , False, , False)
   If Fnd Is Nothing Then Exit Sub
   Range("A1").Resize(Fnd.Offset(, 1)).Select
End Sub
 
Upvote 0
I get a subscript out of range error.

In the code you provided: Set Fnd = Sheets("Sheets2").Range("A:A").Find("ABC123", , , xlWhole, , , False, , False)

where doe sit say find abc123 in column A and return the corresponding value in column B? column b holds the value (ie number of times I need to xldown)




Maybe
Code:
Sub Will85()
   Dim Fnd As Range
      
   Set Fnd = Sheets("Sheets2").Range("A:A").Find("ABC123", , , xlWhole, , , False, , False)
   If Fnd Is Nothing Then Exit Sub
   Range("A1").Resize(Fnd.Offset(, 1)).Select
End Sub
 
Last edited:
Upvote 0
You need to change the sheet name & the ABC123 to match your sheet & the value you want to find.
 
Upvote 0
I did, it doesnt work.

I am now trying to use .Value = Application.WorksheetFunction.VLookup(("ABC123"), Range("account_position"), 2, 0)

This returns 2, meaning xldown two times, Im just trying to figure out how to put it all together.
 
Upvote 0
What error did you get & what line was highlighted?
 
Upvote 0
Subscript out of range
Set Fnd = Sheets("Sheets2").Range("A:A").Find("ABC123", , , xlWhole, , , False, , False)


I created a brand new workbook.

Sheet1 A8="x", A15="x", A22="x"
Sheet2 A1="abc123", B1=3


After running the macro, I would expect Sheet1 A22 to be selected


What error did you get & what line was highlighted?
 
Upvote 0
That error means you don't have a sheet called "Sheet2" in the active workbook.
Check the name of the sheet on the Tab & make sure that there are no leading/trailing spaces.

You also need to explain exactly what you are trying to achieve, because it's not very clear to me.
 
Upvote 0
Your right, you put "Sheets2", through me off, excel defaults to "Sheet2". The macro no longer throws an error, but it does not function as intended.

I currently use

On Sheet1 I have put the value of "x" in cells A8, A15, and A22.

I want to select range A22 in VBA, so I use:

Range("A1").Select
selection.end(xldown).Select
selection.end(xldown).Select
selection.end(xldown).Select

Instead of using the above, Id like to write some vba that could xldown a specific number of times.

Something like selection.end(xldown)3.Select


The value of 3 is based of a lookup. I am currently trying to loop the xldown as such, but it doesnt work:

Sub xldown_variable_number_of_times()
Dim position As Integer
Dim iCtr As Long
Dim DestCell As Range


position = Application.WorksheetFunction.VLookup(("ABC123"), Range("account_position"), 2, 0)


Set DestCell = Range("a1")


For iCtr = 1 To position
Set DestCell = DestCell.End(xlDown)
Next iCtr




End Sub






That error means you don't have a sheet called "Sheet2" in the active workbook.
Check the name of the sheet on the Tab & make sure that there are no leading/trailing spaces.

You also need to explain exactly what you are trying to achieve, because it's not very clear to me.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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