VBA CODE TO PICK ONLY FIVE OBSERVATIONS IN SHEET1 AND DISPLAY IN SHEET2

Mponda

New Member
Joined
Aug 23, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I have data set to put in sheet1 which has to be added new data after the year is complete. In this data set, I want to display only the last five observations on sheet2 with the newly added data inclusive.
The below is my sample data in which, the last five observations starting 2015 to 2020 have to be displayed on sheet2, and when the year 2021 is complete the display has to change to 2021 from 2016.
How can I write a VBA code that picks only the last observations regardless of the newly added observations?
year
1998​
1999​
2000​
2001​
2002​
2003​
2004​
2005​
2006​
2007​
2008​
2009​
2010​
2011​
2012​
2013​
2014​
2015​
2016​
2017​
2018​
2019​
2020​
x
2,632.60​
2,778.40​
2,958.60​
3,222.20​
3,453.50​
3,668.60​
3,876.40​
4,086.60​
4,341.20​
4,574.20​
4,813.90​
5,062.70​
5,350.00​
5,687.20​
5,969.50​
6,289.50​
6,685.40​
7,086.20​
7,566.00​
8,127.70​
8,703.50​
9,212.30​
9,722.50​
y
33.2​
58.4​
53.2​
81.8​
84.9​
108.3​
117.5​
120.7​
136.9​
159.6​
188.4​
162.6​
171.3​
189​
196​
215.1​
200.3​
203.5​
256.5​
329.2​
323.6​
340.9​
436.2​
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hello MPonda,
you can try this...
VBA Code:
Sub PickRange()

    Dim vWS1 As Worksheet, vWS2 As Worksheet
    Dim vC As Integer, vR As Long
    
    Set vWS1 = Sheets("Sheet1")
    Set vWS2 = Sheets("Sheet2")
    vC = vWS1.Cells(1, Columns.Count).End(xlToLeft).Column
    vR = vWS1.Cells(Rows.Count, "A").End(xlUp).Row
    vWS2.[A1].Resize(vR, 5).Value = _
    vWS1.Cells(1, vC).Offset(, -4).Resize(vR, 5).Value
    
End Sub
 
Upvote 0
Hello MPonda,
you can try this...
VBA Code:
Sub PickRange()

    Dim vWS1 As Worksheet, vWS2 As Worksheet
    Dim vC As Integer, vR As Long
   
    Set vWS1 = Sheets("Sheet1")
    Set vWS2 = Sheets("Sheet2")
    vC = vWS1.Cells(1, Columns.Count).End(xlToLeft).Column
    vR = vWS1.Cells(Rows.Count, "A").End(xlUp).Row
    vWS2.[A1].Resize(vR, 5).Value = _
    vWS1.Cells(1, vC).Offset(, -4).Resize(vR, 5).Value
   
End Sub
Thank you very much. It is working very well and thank you once again for your help.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

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