VBA Select all rows from active cell (selected cell) on table to 2nd last row

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
Office Version
  1. 365
Platform
  1. Windows
I am looking for the most efficient way to select all rows of data from a table from a selected cell to the 2nd last row on a table.

Copy the data and re-paste it as values.

The reason for this is on the original spreadsheet there are a lot of formulas to assist with data entry that bog down the sheet when too much data is entered.
So after an individual keys in the spreadsheet all they would have to do is select the first cell they started keying and run the macro and it would copy all rows from the selection down to the 2nd last row in the table and convert the data to values, clearing the formulas.
I want to leave the last row however to preserve the formulas for when data has to be keyed again.

Example: if cell A21 was selected, the macro would select rows 21 down to row 43, copy and paste values but leave row 44 intact.
Note: the real spreadsheet has a lot more data and formulas.

Thank you to anyone who can help me! :)

Book1
ABCDEFG
1OrderDateRegionRepItemUnitsUnitCostTotal
21/6/2020EastJonesPencil951.99189.05
31/23/2020CentralKivellBinder5019.99999.5
42/9/2020CentralKivellPencil364.99179.64
52/26/2020CentralKivellPen2719.99539.73
63/15/2020WestJardinePencil562.99167.44
74/1/2020EastJonesBinder604.99299.4
84/18/2020CentralKivellPencil751.99149.25
95/5/2020CentralKivellPencil904.99449.1
105/22/2020WestJardinePencil321.9963.68
116/8/2020EastJonesBinder608.99539.4
126/25/2020CentralKivellPencil904.99449.1
137/12/2020EastJonesBinder291.9957.71
147/29/2020EastJonesBinder8119.991,619.19
158/15/2020EastJonesPencil354.99174.65
169/1/2020CentralKivellDesk2125250
179/18/2020EastJonesPen Set1615.99255.84
1810/5/2020CentralKivellBinder288.99251.72
1910/22/2020EastJonesPen648.99575.36
2011/8/2020EastJonesPen1519.99299.85
2111/25/2020CentralKivellPen Set964.99479.04
2212/12/2020CentralKivellPencil671.2986.43
2312/29/2020EastJonesPen Set7415.991,183.26
241/15/2021CentralKivellBinder468.99413.54
252/1/2021CentralKivellBinder87151,305.00
262/18/2021EastJonesBinder44.9919.96
273/7/2021WestJardineBinder719.99139.93
283/24/2021CentralKivellPen Set504.99249.5
294/10/2021CentralKivellPencil661.99131.34
304/27/2021EastJonesPen964.99479.04
315/14/2021CentralKivellPencil531.2968.37
325/31/2021CentralKivellBinder808.99719.2
336/17/2021CentralKivellDesk5125625
347/4/2021EastJonesPen Set624.99309.38
357/21/2021CentralKivellPen Set5512.49686.95
368/7/2021CentralKivellPen Set4223.951,005.90
378/24/2021WestJardineDesk3275825
389/10/2021CentralKivellPencil71.299.03
399/27/2021WestJardinePen761.99151.24
4010/14/2021WestJardineBinder5719.991,139.43
4110/31/2021CentralKivellPencil141.2918.06
4211/17/2021CentralKivellBinder114.9954.89
4312/4/2021CentralKivellBinder9419.991,879.06
4412/21/2021CentralKivellBinder284.99139.72
Sheet1
Cell Formulas
RangeFormula
C21:C44C21=INDEX(Sheet2!$B$1:$B$4,MATCH([@Region],Sheet2!$A$1:$A$4,FALSE))
 

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 something like this.
VBA Code:
Sub DoSomething()
    Dim WS As Worksheet
    Dim rng As Range
   
    Set WS = ActiveSheet
    With WS
        Set rng = .Range("A2:G" & .Range("A" & .Rows.Count).End(xlUp).Row)
    End With
    Set rng = rng.Resize(rng.Rows.Count - 1)
    rng.Value = rng.Value
End Sub
 
Upvote 0
Solution
Maybe something like this.
VBA Code:
Sub DoSomething()
    Dim WS As Worksheet
    Dim rng As Range
  
    Set WS = ActiveSheet
    With WS
        Set rng = .Range("A2:G" & .Range("A" & .Rows.Count).End(xlUp).Row)
    End With
    Set rng = rng.Resize(rng.Rows.Count - 1)
    rng.Value = rng.Value
End Sub

This works. I was worried it would take a long time doing the entire table as it is over 40,000 rows but it worked pretty quick.

Thank you! :)
 
Upvote 0

Forum statistics

Threads
1,223,945
Messages
6,175,555
Members
452,652
Latest member
eduedu

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