Glasgowsmile
Active Member
- Joined
- Apr 14, 2018
- Messages
- 280
- Office Version
- 365
- Platform
- 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.
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 | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | |||
1 | Current | Previous record | 2 Records Back | 3 Records Back | |||||||||||
2 | 2/26/2022 | 3/1/2022 | 2/15/2022 | 2/25/2022 | 1/30/2022 | 2/14/2022 | 1/23/2022 | 1/29/2022 | 5/15/2022 | 2/27/2022 | |||||
3 | Col1 | Col1 | Col2 | Col2 | Col3 | Col3 | Col4 | Col4 | NEW DATA | NEW DATA | |||||
4 | Col1 | Col1 | Col2 | Col2 | Col3 | Col3 | Col4 | Col4 | NEW DATA | NEW DATA | |||||
5 | |||||||||||||||
6 | FALSE | ||||||||||||||
7 | |||||||||||||||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
B6 | B6 | =IF(AND(B2>H2,B2>F2,B2<D2),TRUE,FALSE) |