Dynamic table with different starting point

sowerbyp

New Member
Joined
Mar 1, 2019
Messages
5
I wish to create a dynamic range that works backwards from the last row of the data to a place higher up that is 36 whole weeks long. The table is made up of stock counts done on different dates. The dates are in ascending order however they are not done every week.

Is it possible to create a dynamic table from data pasted onto the end of a list of dates already in place and work back up the rows to the date that is the 36th whole week which is the starting point for the dynamic table to the end of the new pasted data which will then form my new table for my pivot table.

Within column A there will be multiple entries of the same date spread over the last months.

Any ideas?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
dates are in column A and consists of data over 2 years that I keep adding to. I would like to be able to have a rolling 36 weeks worth of data that is selected dynamically as I add more data.

22/01/195200026506PLASTER HOODS (reels 50)240024002019.090
28/01/195200404223CELLULOSE ETHER TYLOSE MH 60005 P640306.29402752019.09-1
28/01/195200404224CELLULOSE ETHER (U/COAT PLASTER) TYLOSE MH 60001 P47400.28474012019.091
01/02/195200404242CARDBOARD 1100 X 1260 BD/43 11270.232112712019.091
03/02/195200404257PLASTER PALLETS 1200 X 10006056052019.090
08/02/195200404300GROUND BLAST FURNACE SLAG (CEMSAVE)002019.090
11/02/195200404304HYDRATED LIME BULK TANKER9888.58798802019.09-1
15/02/195200404313DURAFINISH PAPER BAG (reels)11408114082019.090
28/02/195200404325KERATIN (TYPE A) 1006.7310002019.09-1
28/02/195200404334PERLITE ORE106307.221080002019.091
28/02/195200404389SILIPON FOAMING AGENT RN6031773.2877732019.09-1
28/02/195200404409THISTLE UNIVERSAL ONE COAT BAG (25KG) reels24200242002019.090

<colgroup><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
I think this function will do what you want. (assuming that the values in column A are Excel serial dates)
Code:
Function rngPreviousWeeks(Optional WeeksCount As Long = 36) As Range
    Dim NewCell As Range, OldCell As Range
    Dim i As Long
    
    With Range("A:A")
    
        Set NewCell = .Find("?*", after:=.Cells(1, 1), LookIn:=xlValues, lookAt:=xlWhole, searchdirection:=xlPrevious)
        
        For i = NewCell.Row To 1 Step -1
            With .Cells(i, 1)
                If IsDate(.Value) Then
                    Set OldCell = .Cells
                    If (WeeksCount * 7) < (NewCell.Value - OldCell.Value) Then
                        Set OldCell = OldCell.Offset(1, 0)
                        Exit For
                    End If
                End If
            End With
        Next i
    End With
    
    Set rngPreviousWeeks = Range(OldCell, NewCell)
End Function
 
Upvote 0
thanks Mike. How do I use this function? sorry I'm a bit of a newbie :)

I think this function will do what you want. (assuming that the values in column A are Excel serial dates)
Code:
Function rngPreviousWeeks(Optional WeeksCount As Long = 36) As Range
    Dim NewCell As Range, OldCell As Range
    Dim i As Long
    
    With Range("A:A")
    
        Set NewCell = .Find("?*", after:=.Cells(1, 1), LookIn:=xlValues, lookAt:=xlWhole, searchdirection:=xlPrevious)
        
        For i = NewCell.Row To 1 Step -1
            With .Cells(i, 1)
                If IsDate(.Value) Then
                    Set OldCell = .Cells
                    If (WeeksCount * 7) < (NewCell.Value - OldCell.Value) Then
                        Set OldCell = OldCell.Offset(1, 0)
                        Exit For
                    End If
                End If
            End With
        Next i
    End With
    
    Set rngPreviousWeeks = Range(OldCell, NewCell)
End Function
 
Upvote 0
I'm not sure how you were planning on using range.

To find the range of the most recent 36 weeks (assuming the dates are in column A, sorted oldest to newest)

If you put the UDF in a module. This sub is an example of how it could be used.

Code:
Sub Test()
    MsgBox "The last 36 weeks of entries are in " & rngPreveiousWeeks.Address
End Sub

Function rngPreviousWeeks(Optional WeeksCount As Long = 36) As Range
    Dim NewCell As Range, OldCell As Range
    Dim i As Long
    
    With Range("A:A")
    
        Set NewCell = .Find("?*", after:=.Cells(1, 1), LookIn:=xlValues, lookAt:=xlWhole, searchdirection:=xlPrevious)
        
        For i = NewCell.Row To 1 Step -1
            With .Cells(i, 1)
                If IsDate(.Value) Then
                    Set OldCell = .Cells
                    If (WeeksCount * 7) < (NewCell.Value - OldCell.Value) Then
                        Set OldCell = OldCell.Offset(1, 0)
                        Exit For
                    End If
                End If
            End With
        Next i
    End With
    
    Set rngPreviousWeeks = Range(OldCell, NewCell)
End Function
Note that you can pass the number of weeks as an argument for the function.
 
Upvote 0
If you want a formula based solution,

=INDEX(A:A, MATCH(9E+99,A:A), 1) is the last cell in column A that holds a date

=INDEX(A:A, MATCH(MAX(A:A)-36*7, A:A) + 1, 1) is the first cell in the holds a date after the last date minus 36 weeks, so

INDEX(A:A, MATCH(9E+99,A:A), 1) : INDEX(A:A, MATCH(MAX(A:A)-36*7, A:A) + 1, 1) is the range of those 36 weeks worth of cells.
 
Upvote 0
I actually want a named range that I can create a Pivot Table from that refers to the last 36 weeks worth of data. I think your function will work as I just need to run the Sub after adding new data.

Your function has highlighted a problem for me in that weeks 31 & 40 are missing from my dates as we did not count stock that week so I am missing some data.

Are we able to make your function adjust for any missing weeks and still give me 36 weeks worth of data regardless of dates?

Thanks for the help so far Mike :)



I'm not sure how you were planning on using range.

To find the range of the most recent 36 weeks (assuming the dates are in column A, sorted oldest to newest)

If you put the UDF in a module. This sub is an example of how it could be used.

Code:
Sub Test()
    MsgBox "The last 36 weeks of entries are in " & rngPreveiousWeeks.Address
End Sub

Function rngPreviousWeeks(Optional WeeksCount As Long = 36) As Range
    Dim NewCell As Range, OldCell As Range
    Dim i As Long
    
    With Range("A:A")
    
        Set NewCell = .Find("?*", after:=.Cells(1, 1), LookIn:=xlValues, lookAt:=xlWhole, searchdirection:=xlPrevious)
        
        For i = NewCell.Row To 1 Step -1
            With .Cells(i, 1)
                If IsDate(.Value) Then
                    Set OldCell = .Cells
                    If (WeeksCount * 7) < (NewCell.Value - OldCell.Value) Then
                        Set OldCell = OldCell.Offset(1, 0)
                        Exit For
                    End If
                End If
            End With
        Next i
    End With
    
    Set rngPreviousWeeks = Range(OldCell, NewCell)
End Function
Note that you can pass the number of weeks as an argument for the function.
 
Upvote 0
Thanks Mike. Any ideas on how to cope with none continuous weeks as there are dates missing in my data?

The formula gets me back to the 12/06/17 (wk 25) however I'm missing 2 weeks of data so it needs to go back to wk 23 to give me 36 weeks worth of data. I also need to be back to the Monday of that week too?

Apologies for all the questions :)


If you want a formula based solution,

=INDEX(A:A, MATCH(9E+99,A:A), 1) is the last cell in column A that holds a date

=INDEX(A:A, MATCH(MAX(A:A)-36*7, A:A) + 1, 1) is the first cell in the holds a date after the last date minus 36 weeks, so

INDEX(A:A, MATCH(9E+99,A:A), 1) : INDEX(A:A, MATCH(MAX(A:A)-36*7, A:A) + 1, 1) is the range of those 36 weeks worth of cells.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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