Control Shift Down to the next occupied cell

PCRIDE

Well-known Member
Joined
Jan 20, 2008
Messages
902
I am recording a macro, I do a Control Shift down arrow to find the next occupied cell then press up arrow and copy\paste. However the macro is adding in a range of cells.

Since the report can change daily and have a different number rows each time it is run, the range will not work.

Please advise

Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A10").Select
ActiveCell.FormulaR1C1 = "=RC[1]"
Range("A10").Select
Selection.Copy
Range("B10").Select
Selection.End(xlDown).Select
Range("A827").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste


I need to replace all of these sections Range("A827").Select with a command that finds the next occupied cell.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Code:
Range("A10:A827").Select
Selection.Copy
sheets("the sheet name to past to").activate
Range(" the range were you wont to past").select
Selection.Copy
 
Upvote 0
Thanks for the quick reply.

I cannot use a range with specific row numbers, since the amount of rows change each time the report is run.

Anything with Range("A10:A827"). will not work for me.

If I start at the top of the sheet and control shift down to the next occupied cell, it may be 5 rows untill it hits the next occupied cell, on the next run it could be 10 rows until the next occupied cell. This is why the ranges will not work.
 
Upvote 0
try this it will tell you the first and last rows and then select it it can be any number of rows or columns
Code:
Sub test()
Dim rNum As Long
 rNum = Cells.Find(What:="*", After:=Range("a1"), _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
 MsgBox "last row with an entry: " & rNum
 Dim cNum As Integer
 cNum = Cells.Find(What:="*", After:=Range("a1"), _
   SearchOrder:=xlByColumns, _
   SearchDirection:=xlPrevious).Column
   MsgBox "Last column with an entry:  " & cNum
Range(Range("A1"), Cells(rNum, cNum)).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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