VBA to find the first and last cell in a Range = a value?

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
Hello again :)

I have an Excel sheet with over 30,000 rows of data in it. Presently it has several Autofilters, one of which, in row C, sorts the records according to username.

I am wanting to then select a range that covers the first entry and the last entry that matches the users name.

The reason for this is that once sorted by the autofilter the range varies every day depending on the user. For example user1 that I just checked, under the autofilter they have 112 entries with in a range

Code:
Range("C14163:C22567").Select

To make dealing with the data easier, the goal was to run a macro from a new workbook, that opens the large sheet, uses the autofilter to sort by the user, then copy the entire range displayed.

I've got it opening, sorting by user but I'm not sure how to automate determning the first row in which the users name appears and the last. So that the macro/vba can do this each day without someone having to choose the range.

Any suggestions?

Sincerely,
WageSlave101
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

tactps

Well-known Member
Joined
Jan 20, 2004
Messages
3,460
If you have a filter on and copy and paste all of the data in the sheet via macro, it will ignore the hidden rows.

So just choose the entire sheet, or range, and copy and paste
 

ravishankar

Well-known Member
Joined
Feb 23, 2006
Messages
3,566
HI
paste the following codes in the macro window (Alt F11)

Code:
Sub bb()
x = Cells(Rows.Count, 3).End(xlUp).Row
what = InputBox("enter search name")
For a = 1 To x
If Cells(a, 3) = what Then
First = a
Exit For
End If
Next a
For c = x To 1 Step -1
If Cells(c, 3) = what Then
last = c
End If
Next c
MsgBox "your range is C" & First & ":C" & x - last
End Sub
run the macro
Enter the search name and continue
Ravi
 

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
Thank you very much :)

Now I adjusted the formula slightly because it was stopping short to, and added in a name directly to remove the input

Sub RepRange()
x = Cells(Rows.Count, 4).End(xlUp).Row
rep = "csr101"
For a = 1 To x
If Cells(a, 4) = rep Then
first = a
Exit For
End If
Next a
For c = x To 1
If Cells(c, 4) = rep Then
last = c
End If
Next c
MsgBox " The first is D" & first & " and the last is D" & x - last
End Sub

The next question is how can I insert these values directly into a range select so that I can automate the copy. Let's say that we've found that the the that first = 11463 and last = 22567 from the formula how do I insert those into a Range("").Select?

Manually it shows up as

Range("C11463:C22567").Select
Selection.Copy

Thank you very much again for you help :)

WageSlave101
 

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
Ah... worked it out :)

Thank you again for your help everyone :D

Code:
Range("D" & first, "D" & x - last).Select

WageSlave101
 

Forum statistics

Threads
1,181,373
Messages
5,929,571
Members
436,679
Latest member
helly123

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
Top