VBA coding assistance to define row number from a cell ref

Data Monkey

New Member
Joined
May 27, 2016
Messages
5
I have a piece of code to delete out rows but I need the range to be dynamic.

Currently my code is as follows but I need to change the "4" to be a cell reference as I have a formula that decides where to start the delete from.

Please help!:confused:

<Code>
Sub Macro1()
'
' Macro1 Macro
'


'
ActiveWorkbook.Worksheets("Position Selection").ListObjects("Table7").Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Position Selection").ListObjects("Table7").Sort. _
SortFields.Add Key:=Range("Table7[[#All],[Customer]]"), SortOn:= _
xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Position Selection").ListObjects("Table7").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With Worksheets("Position Selection")
Rows("4:" & .Rows.Count).Delete
End With
End Sub
</Code>
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I suppose your line of code would change to

Code:
Rows(ActiveCell.Row & ":" & .Rows.Count).Delete

So, all the rows from the selected cell to the end of the sheet will be deleted.

Is this what you intend your code to do?

Regards
Caleeco
 
Upvote 0
It's not necessarily the active cell though.

if cell M2 = 135 then I would want all rows deleting after 134

thanks,
 
Upvote 0
It's not necessarily the active cell though.

if cell M2 = 135 then I would want all rows deleting after 134

thanks,

In which case the line would be

Code:
Rows(Range("M2").Value & ":" & Rows.Count).Delete

This would include the row number listed in cell M2.

Hope that helps
Regards
Caleeco
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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