VBA Rolling data based on year/month

nburaq

Board Regular
Joined
Apr 2, 2021
Messages
220
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Gents,
I have a table and I need to enter the data manually every month. At the same time,to compare the data with other months, rows need to be moved upwards. So the oldest month entered in the table will dissappear and the newest month will be seen at the bottom. I believe this is only possible with VBA. I have added the data as sample and screenshots before and after. Thanks for your help!

Data:
DateLocation 1Location 2Location 3Location 4Location 5Location 6Location 7Location 8
May/22​
1,1​
0,0​
0,3​
0,5​
0,0​
0,0​
0,0​
0,2​
Jun/22​
1,3​
4,5​
1,1​
1,8​
0,0​
0,8​
0,0​
0,0​
Jul/22​
1,4​
3,9​
1,6​
4,5​
0,0​
2,3​
0,0​
0,1​
Aug/22​
1,7​
0,0​
1,0​
1,6​
0,4​
0,0​
0,0​
20,9​
Sep/22​
2,3​
2,4​
0,0​
0,0​
6,8​
0,1​
0,0​
1,4​
Oct/22​
2,1​
1,2​
1,0​
1,1​
2,7​
0,0​
2,4​
0,6​
Nov/22​
1,9​
0,0​
0,8​
2,3​
0,0​
1,3​
0,5​
2,6​
Dec/22​
7,9​
2,3​
0,7​
1,9​
0,2​
5,5​
0,0​
0,0​
Jan/23​
7,0​
0,0​
1,2​
0,5​
0,2​
0,0​
0,0​
4,1​
Feb/23​
4,1​
0,0​
0,0​
0,7​
0,0​
0,0​
0,0​
1,4​
Mar/23​
4,2​
4,5​
1,4​
4,3​
0,0​
1,1​
0,0​
0,0​
Apr/23​
0,6​
3,0​
0,4​
1,2​
2,0​
0,0​
0,0​
0,8​

Data to be entered for May 23:

Enter New Values:
5,6​
2,3​
0,8​
1,9​
0,2​
5,5​
0,0​
0,0​

Before:
1687166000890.png


After:
1687166062032.png
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
The macro isn't too complicated so you should be able to update as needed (maybe cell references would change if you move things around since this works off fixed cell locations).
VBA Code:
Sub UpdateData()

Dim ws As Worksheet

    Set ws = ActiveSheet

    ws.Range("A2:I2").Delete shift:=xlShiftUp
   
    ws.Range("A13").Value = ws.Range("K1").Value
    ws.Range("A13").NumberFormat = "mmm/yy"
   
    ws.Range("B13:I13").Value = ws.Range("L2:S2").Value
    ws.Range("A13:I13").HorizontalAlignment = xlRight

End Sub

I added a formula in K1 to figure out the month and year if you want that. Otherwise, you can just type it in. Now, just make a form button and link it to the macro to run.

temp macro work.xlsm
ABCDEFGHIJKLMNOPQRS
1DateLocation 1Location 2Location 3Location 4Location 5Location 6Location 7Location 8Jun/23
2May/221,10,00,30,50,00,00,00,2Enter New Values:5,62,30,81,90,25,50,00,0
3Jun/221,34,51,11,80,00,80,00,0
4Jul/221,43,91,64,50,02,30,00,1
5Aug/221,70,01,01,60,40,00,020,9
6Sep/222,32,40,00,06,80,10,01,4
7Oct/222,11,21,01,12,70,02,40,6
8Nov/221,90,00,82,30,01,30,52,6
9Dec/227,92,30,71,90,25,50,00,0
10Jan/237,00,01,20,50,20,00,04,1
11Feb/234,10,00,00,70,00,00,01,4
12Mar/234,24,51,44,30,01,10,00,0
13Apr/230,63,00,41,22,00,00,00,8
Sheet4
Cell Formulas
RangeFormula
K1K1=TEXT(TODAY(),"mmm/yy")
 
Upvote 1
Solution
Thanks for the solution. later on, I realized that there is no need for extensive vba coding. I could solve the problem with macro recorder.
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

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