VBA to put items in Chronological Order

Glasgowsmile

Active Member
Joined
Apr 14, 2018
Messages
280
Office Version
  1. 365
Platform
  1. Windows
I'm trying to shift out old data when new data is added and then compare the dates to see where the new data should be placed.

Right now my big issue is the first If statement in the VBA. The worksheet has an IF to check those criteria and it's true by time I get to that part of the VBA code but that first IF statement isn't executing and I don't understand why.

VBA Code:
Sub ImportChrono()
   
    NewBA = Sheet1.Range("B2").Value
    PBA = Sheet1.Range("D2").Value
    BA2 = Sheet1.Range("F2").Value
    BA3 = Sheet1.Range("H2").Value
        
        ' Clearing old data to make room for new data
        
        If Not IsEmpty(Sheet1.Range("A2, C2, E2, G2")) Then
            
            ' Delete the oldest record (3 records back)
            Sheet1.Range("G2:H4").ClearContents
        
            ' Copy 2 Records back to 3 Records Back
            Sheet1.Range("E2:F4").Copy Sheet1.Range("G2")
        
            ' Copy Previous Record to 2 Records Back
            Sheet1.Range("C2:D4").Copy Sheet1.Range("E2")
            
            ' Copy Current to Previous Record
            Sheet1.Range("A2:B4").Copy Sheet1.Range("C2")
            
            ' Add New Data
            Sheet1.Range("L2:M4").Copy Sheet1.Range("A2")
            
        End If

With Sheet1
    If NewBA > BA3 And NewBA > BA2 And NewBA < PBA Then
        .Range("A2:B4").Copy Destination:=.Range("Q2")
        .Range("C2:D2").Copy Destination:=.Range("A2")
        .Range("Q2:R4").Copy Destination:=.Range("C2")
    ElseIf NewBA > BA4 And NewBA > BA3 And NewBA <= BA2 And NewBA <= PBA Then
        .Range("L2:M4").Copy Destination:=.Range("E2")
    ElseIf NewBA > BA4 And NewBA <= BA3 And NewBA <= BA2 And NewBA <= PBA Then
        .Range("L2:M4").Copy Destination:=.Range("G2")
    End If
End With

End Sub

TestingChrono.xlsm
ABCDEFGHIJKLM
1CurrentPrevious record2 Records Back3 Records Back
22/26/20223/1/20222/15/20222/25/20221/30/20222/14/20221/23/20221/29/20225/15/20222/27/2022
3Col1Col1Col2Col2Col3Col3Col4Col4NEW DATANEW DATA
4Col1Col1Col2Col2Col3Col3Col4Col4NEW DATANEW DATA
5
6FALSE
7
Sheet1
Cell Formulas
RangeFormula
B6B6=IF(AND(B2>H2,B2>F2,B2<D2),TRUE,FALSE)
 
It have probably failed to explain this correctly. The top data gets placed based on the date, which you have and it's working but the other data sets are also needing to be placed in a specific spot based on where the top data lands.

Better example below I hope. I changed the end date so Data0 got put into Place 2 but Data 0 below is still in place 1, both would need to be in place too.

That's why I thought about approaching this with this formula below but it was skipping the first IF even though the conditions were true and I couldn't figure out why, but that way if the condition was true I would know exactly where to move the data for all 3 sets of data that will be involved.

VBA Code:
        With Sheet1
If NewBA > BA3 And NewBA > BA2 And NewBA < PBA Then
.Range("A2:B4").Copy Destination:=.Range("Q2")
.Range("C2:D2").Copy Destination:=.Range("A2")
.Range("Q2:R4").Copy Destination:=.Range("C2")
ElseIf NewBA > BA4 And NewBA > BA3 And NewBA <= BA2 And NewBA <= PBA Then
.Range("L2:M4").Copy Destination:=.Range("E2")
ElseIf NewBA > BA4 And NewBA <= BA3 And NewBA <= BA2 And NewBA <= PBA Then
.Range("L2:M4").Copy Destination:=.Range("G2")
End If
End With


TestingChrono.xlsm
ABCDEFGHI
1Place1Place 2Place 3Place 4
201/29/2202/05/2201/22/2201/28/2201/15/2201/22/2201/01/2201/08/22
3data3data3data0data0data2data2data1data1
4data3data3data0data0data2data2data1data1
5data3data3data0data0data2data2data1data1
6data3data3data0data0data2data2data1data1
7data3data3data0data0data2data2data1data1
8data3data3data0data0data2data2data1data1
9data3data3data0data0data2data2data1data1
10data3data3data0data0data2data2data1data1
11data3data3data0data0data2data2data1data1
12data3data3data0data0data2data2data1data1
13data3data3data0data0data2data2data1data1
14data3data3data0data0data2data2data1data1
15
16Place 1
17data0data0data0data0data0data0
18data0data0data0data0data0data0
19data0data0data0data0data0data0
20data0data0data0data0data0data0
21data0data0data0data0data0data0
22data0data0data0data0data0data0
23data0data0data0data0data0data0
24data0data0data0data0data0data0
25Place 2
26data1data1data1data1data1data1
27data1data1data1data1data1data1
28data1data1data1data1data1data1
29data1data1data1data1data1data1
30data1data1data1data1data1data1
31data1data1data1data1data1data1
32data1data1data1data1data1data1
33data1data1data1data1data1data1
34Place 3
35data2data2data2data2data2data2
36data2data2data2data2data2data2
37data2data2data2data2data2data2
38data2data2data2data2data2data2
39data2data2data2data2data2data2
40data2data2data2data2data2data2
41data2data2data2data2data2data2
42data2data2data2data2data2data2
43Place 4
44data3data3data3data3data3data3
45data3data3data3data3data3data3
46data3data3data3data3data3data3
47data3data3data3data3data3data3
48data3data3data3data3data3data3
49data3data3data3data3data3data3
50data3data3data3data3data3data3
51data3data3data3data3data3data3
52
Sheet1
[/CODE]
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
so the top, which is sorting correct now (okay?), is the guideline for the other blocks to be sorted vertical, not just moved ?
 
Upvote 0
so the top, which is sorting correct now (okay?), is the guideline for the other blocks to be sorted vertical, not just moved ?
With the above example, if the sorting puts the top data in C2:D14 like above, then the below set of data for data0 should also go into the 'Place 2' or A26:F33. There would be another set of data on another worksheet, something like Data!A3:F1000 that would also need to be sorted based on where that original sort lands.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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