excel vba clear cell value with if contain specific cell value

jhonatan321

New Member
Joined
Jul 14, 2021
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Good evening everyone, I'm looking for some code in vba that can find specified dates in a line, if the date is not the same I need to delete the 3 columns, I want to leave only dates in the line that correspond to date1 and date 2 as in the example
before

1626298409784.png

later
1626298422045.png
 
your first code removed the different dates in the line and cleared two cells on the side, now I need this line to bring the two cells closest to the times specified in these dates
the date needs to be the same is the time needs to be the closest and I can bring the cell on the closest time side
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The new request is much more complex. I am working on this but most likely will not have a solution before the weekend.
 
Upvote 0
I was able to finish and validate some scenarios. My testing is not exhaustive so there may be scenarios I have not considered.

Here is the final code
VBA Code:
Option Explicit

Sub RemoveData()
    Dim i As Integer
    Dim j As Integer
    Dim D1 As Date
    Dim D2 As Date
    Dim T1 As Double
    Dim T2 As Double
    Dim Day1Col As Long
    Dim Day2Col As Long
    Dim EntDte1Col As Long
    Dim Time1Col As Long
    Dim Time2Col As Long
    Dim CloseTime1Col As Long
    Dim CloseTime2Col As Long
    Dim CloseVal1Col As Long
    Dim CloseVal2Col As Long
    Dim TimeDiff1 As Double
    Dim TimeDiff2 As Double
    Dim LR As Long
    Dim LC As Long
    Dim rng As Range
    Dim ans As VbMsgBoxResult
    
Retry:
    On Error Resume Next
    Set rng = Nothing
    Set rng = Application.InputBox( _
        Title:="Range Selection", _
        Prompt:="Select the range that contains the data to evaluate INCLUDING column headers", _
        Type:=8)
    On Error GoTo 0
    
    If rng Is Nothing Then Exit Sub
    
    LR = rng.Row + rng.Rows.Count - 1
    LC = rng.Columns.Count
    TimeDiff1 = 999999
    TimeDiff2 = 999999
    
    
    On Error Resume Next
    Day1Col = WorksheetFunction.Match("Day 1", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    Day2Col = WorksheetFunction.Match("Day 2", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    Time1Col = WorksheetFunction.Match("Hora 1", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    Time2Col = WorksheetFunction.Match("Hora 2", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    CloseTime1Col = WorksheetFunction.Match("Close Time 1", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    CloseTime2Col = WorksheetFunction.Match("Close Time 2", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    CloseVal1Col = WorksheetFunction.Match("Close Value 1", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    CloseVal2Col = WorksheetFunction.Match("Close Value 2", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    EntDte1Col = WorksheetFunction.Match("Entry Date", Range(Cells(rng.Row, rng.Column), Cells(rng.Row, LC)), 0)
    On Error GoTo 0
    
    If Day1Col = 0 Or Day2Col = 0 Or Time1Col = 0 Or Time2Col = 0 Or CloseTime1Col = 0 Or CloseTime2Col = 0 Or CloseVal1Col = 0 Or CloseVal2Col = 0 Then
        ans = MsgBox("One or more of the following columns are missing from selection " & vbCrLf & vbCrLf _
            & "Day 1, Day 2, Hora 1, Hora 2." & vbCrLf _
            & "Close Time 1, Close Time 2" & vbCrLf _
            & "Close Value 1, Close Value 2", vbRetryCancel, "Range Selection Error!")
        If ans = vbRetry Then
            GoTo Retry
        Else
            Exit Sub
        End If
    End If
    
    If EntDte1Col = 0 Or LC - EntDte1Col < 2 Then
        ans = MsgBox("Either you did not select a column called Entry Date or you did not select enough columns to process." & vbCrLf & vbCrLf _
            & "You must select an Entry Date column with at least two columns to the right of that column to process the data.", vbRetryCancel, "Range Selection Error!")
        If ans = vbRetry Then
            GoTo Retry
        Else
            Exit Sub
        End If
    End If
        
   For i = rng.Row + 1 To LR
        D1 = Cells(i, Day1Col).Value
        D2 = Cells(i, Day2Col).Value
        T1 = Cells(i, Time1Col).Value
        T2 = Cells(i, Time2Col).Value
        TimeDiff1 = 999999
        TimeDiff2 = 999999
        
        For j = EntDte1Col To LC - 2 Step 3
            'Check if neither date matches and delete the data if they do not
            If Cells(i, j).Value <> D1 Then
                If Cells(i, j).Value <> D2 Then
                    Range(Cells(i, j), Cells(i, j + 2)).ClearContents
                    GoTo NxtCol
                End If
            End If
            
            'check if date matches D1
            If Cells(i, j).Value = D1 Then
                If TimeDiff1 <> 0 Then
                    If TimeDiff1 <> (T1 - Cells(i, j + 1).Value) Then
                        If TimeDiff1 > (T1 - Cells(i, j + 1).Value) And (T1 - Cells(i, j + 1).Value) >= 0 Then
                            TimeDiff1 = (T1 - Cells(i, j + 1).Value)
                            Cells(i, CloseTime1Col) = Cells(i, j + 1).Value
                            Cells(i, CloseVal1Col) = Cells(i, j + 2).Value
                        End If
                    End If
                End If
            End If
            
            'check if date matches D2
            If Cells(i, j).Value = D2 Then
                If TimeDiff2 <> 0 Then
                    If TimeDiff2 <> (T2 - Cells(i, j + 1).Value) Then
                        If TimeDiff2 > (T2 - Cells(i, j + 1).Value) And ((T2 - Cells(i, j + 1).Value)) >= 0 Then
                            TimeDiff2 = (T2 - Cells(i, j + 1).Value)
                            Cells(i, CloseTime2Col) = Cells(i, j + 1).Value
                            Cells(i, CloseVal2Col) = Cells(i, j + 2).Value
                        End If
                    End If
                End If
            End If
NxtCol:
        Next j
    Next i
                
End Sub



My Test Data, Expected Results, and Validation Tables
Solutions 20210713.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1Test Scenarios & Data
2PersonDay 1Hora 1Close time 1Close Value 1Day 2Hora 2Close Time 2Close Value 2Entry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin id
3Match D1 time < T1 No D2 match1/1/20211:00:002/1/20213:00:001/1/20210:59:5915/1/20211:00:0026/1/20211:00:0037/1/20211:00:0048/1/20211:00:0059/1/20211:00:006
4Match D2 time < T2 No D1 match1/2/20212:00:002/2/20214:00:004/1/20211:00:0012/2/20213:00:0026/2/20212:00:0037/2/20212:00:0048/2/20212:00:0059/2/20212:00:006
5Match D1 time = T1 No D2 match1/3/20213:00:002/3/20215:00:004/2/20212:00:0015/2/20212:00:0021/3/20213:00:0037/3/20213:00:0048/3/20213:00:0059/3/20213:00:006
6Match D2 time = T2 No D1 match1/4/20214:00:002/4/20216:00:004/3/20213:00:0015/3/20213:00:0026/3/20214:00:0032/4/20216:00:0048/4/20214:00:0059/4/20214:00:006
7Match D1 time > T1 No D2 match1/5/20215:00:002/5/20217:00:004/4/20214:00:0015/3/20214:00:0026/4/20215:00:0037/4/20214:00:0041/5/20215:01:0059/5/20215:00:006
8Match D2 time > T2 No D1 match1/6/20216:00:002/6/20218:00:004/5/20215:00:0015/5/20215:00:0026/5/20216:00:0037/5/20215:00:0048/5/20215:00:0052/6/20219:00:016
9Match mulitple D1 1st time < T1 2nd time = T11/7/20217:00:002/7/20219:00:001/7/20216:00:0011/7/20217:00:0026/7/20217:00:0037/6/20216:00:0048/6/20216:00:0059/6/20216:00:006
10Match mulitple D2 1st time < T2 2nd time = T21/8/20218:00:002/8/202110:00:004/6/20217:00:0015/6/20216:00:0022/8/20219:00:0032/8/202110:00:0048/7/20217:00:0059/7/20217:00:006
11Match mulitple D1 1st time < T1 2nd time > T11/9/20219:00:002/9/202111:00:004/7/20218:00:0011/9/20218:00:0021/9/202110:00:0037/7/20217:00:0048/8/20218:00:0059/8/20218:00:006
12Match mulitple D2 1st time < T2 2nd time > T21/10/202110:00:002/10/202112:00:004/8/20219:00:0015/7/20217:00:0022/10/202111:00:0032/10/202113:00:0048/9/20219:00:0059/9/20219:00:006
13Match mulitple D1 1st time = T1 2nd time < T11/11/202111:00:002/11/202113:00:004/9/202110:00:0015/8/20218:00:0026/8/20218:00:0031/11/202111:00:0041/11/202110:59:0059/10/202110:00:006
14Match mulitple D2 1st time = T2 2nd time < T21/12/202112:00:002/12/202114:00:004/10/202111:00:0015/9/20219:00:0026/9/20219:00:0037/8/20218:00:0042/12/202114:00:0052/12/202113:59:596
15Match mulitple D1 1st time = T1 2nd time = T11/13/202113:00:002/13/202115:00:001/13/202113:00:0015/10/202110:00:0026/10/202110:00:0037/9/20219:00:0048/10/202110:00:0051/13/202113:00:006
16Match mulitple D2 1st time = T2 2nd time = T21/14/202114:00:002/14/202116:00:004/11/202112:00:0012/14/202116:00:0026/11/202111:00:0037/10/202110:00:0042/14/202116:00:0059/11/202111:00:006
17Match mulitple D1 1st time = T1 2nd time > T11/15/202115:00:002/15/202117:00:004/12/202113:00:0011/15/202115:00:0021/15/202115:00:0137/11/202111:00:0048/11/202111:00:0059/12/202112:00:006
18Match mulitple D2 1st time = T2 2nd time > T21/16/202116:00:002/16/202118:00:004/13/202114:00:0015/11/202111:00:0026/12/202112:00:0032/16/202118:00:0042/16/202118:00:0159/13/202113:00:006
19No Matches for D1 or D21/17/202117:00:002/17/202119:00:004/14/202115:00:0015/12/202112:00:0026/13/202113:00:0037/12/202112:00:0048/12/202112:00:0059/14/202114:00:006
20
21Expected Results
22PersonDay 1Hora 1Close time 1Close Value 1Day 2Hora 2Close Time 2Close Value 2Entry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin id
23Match D1 time < T1 No D2 match1/1/20211:00:000:59:5912/1/20213:00:001/1/20210:59:591
24Match D2 time < T2 No D1 match1/2/20212:00:002/2/20214:00:003:00:0022/2/20213:00:002
25Match D1 time = T1 No D2 match1/3/20213:00:003:00:0032/3/20215:00:001/3/20213:00:003
26Match D2 time = T2 No D1 match1/4/20214:00:002/4/20216:00:006:00:0042/4/20216:00:004
27Match D1 time > T1 No D2 match1/5/20215:00:002/5/20217:00:001/5/20215:01:005
28Match D2 time > T2 No D1 match1/6/20216:00:002/6/20218:00:002/6/20219:00:016
29Match mulitple D1 1st time < T1 2nd time = T11/7/20217:00:007:00:0022/7/20219:00:001/7/20216:00:0011/7/20217:00:002
30Match mulitple D2 1st time < T2 2nd time = T21/8/20218:00:002/8/202110:00:0010:00:0042/8/20219:00:0032/8/202110:00:004
31Match mulitple D1 1st time < T1 2nd time > T11/9/20219:00:008:00:0022/9/202111:00:001/9/20218:00:0021/9/202110:00:003
32Match mulitple D2 1st time < T2 2nd time > T21/10/202110:00:002/10/202112:00:0011:00:0032/10/202111:00:0032/10/202113:00:004
33Match mulitple D1 1st time = T1 2nd time < T11/11/202111:00:0011:00:0042/11/202113:00:001/11/202111:00:0041/11/202110:59:005
34Match mulitple D2 1st time = T2 2nd time < T21/12/202112:00:002/12/202114:00:0014:00:0052/12/202114:00:0052/12/202113:59:596
35Match mulitple D1 1st time = T1 2nd time = T11/13/202113:00:0013:00:0012/13/202115:00:001/13/202113:00:0011/13/202113:00:006
36Match mulitple D2 1st time = T2 2nd time = T21/14/202114:00:002/14/202116:00:0016:00:0022/14/202116:00:0022/14/202116:00:005
37Match mulitple D1 1st time = T1 2nd time > T11/15/202115:00:0015:00:0022/15/202117:00:001/15/202115:00:0021/15/202115:00:013
38Match mulitple D2 1st time = T2 2nd time > T21/16/202116:00:002/16/202118:00:0018:00:0042/16/202118:00:0042/16/202118:00:015
39No Matches for D1 or D21/17/202117:00:002/17/202119:00:00
40
41Test Results
42PersonDay 1Hora 1Close time 1Close Value 1Day 2Hora 2Close Time 2Close Value 2Entry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin id
43Match D1 time < T1 No D2 match1/1/20211:00:000:59:5912/1/20213:00:001/1/20210:59:591
44Match D2 time < T2 No D1 match1/2/20212:00:002/2/20214:00:003:00:0022/2/20213:00:002
45Match D1 time = T1 No D2 match1/3/20213:00:003:00:0032/3/20215:00:001/3/20213:00:003
46Match D2 time = T2 No D1 match1/4/20214:00:002/4/20216:00:006:00:0042/4/20216:00:004
47Match D1 time > T1 No D2 match1/5/20215:00:002/5/20217:00:001/5/20215:01:005
48Match D2 time > T2 No D1 match1/6/20216:00:002/6/20218:00:002/6/20219:00:016
49Match mulitple D1 1st time < T1 2nd time = T11/7/20217:00:007:00:0022/7/20219:00:001/7/20216:00:0011/7/20217:00:002
50Match mulitple D2 1st time < T2 2nd time = T21/8/20218:00:002/8/202110:00:0010:00:0042/8/20219:00:0032/8/202110:00:004
51Match mulitple D1 1st time < T1 2nd time > T11/9/20219:00:008:00:0022/9/202111:00:001/9/20218:00:0021/9/202110:00:003
52Match mulitple D2 1st time < T2 2nd time > T21/10/202110:00:002/10/202112:00:0011:00:0032/10/202111:00:0032/10/202113:00:004
53Match mulitple D1 1st time = T1 2nd time < T11/11/202111:00:0011:00:0042/11/202113:00:001/11/202111:00:0041/11/202110:59:005
54Match mulitple D2 1st time = T2 2nd time < T21/12/202112:00:002/12/202114:00:0014:00:0052/12/202114:00:0052/12/202113:59:596
55Match mulitple D1 1st time = T1 2nd time = T11/13/202113:00:0013:00:0012/13/202115:00:001/13/202113:00:0011/13/202113:00:006
56Match mulitple D2 1st time = T2 2nd time = T21/14/202114:00:002/14/202116:00:0016:00:0022/14/202116:00:0022/14/202116:00:005
57Match mulitple D1 1st time = T1 2nd time > T11/15/202115:00:0015:00:0022/15/202117:00:001/15/202115:00:0021/15/202115:00:013
58Match mulitple D2 1st time = T2 2nd time > T21/16/202116:00:002/16/202118:00:0018:00:0042/16/202118:00:0042/16/202118:00:015
59No Matches for D1 or D21/17/202117:00:002/17/202119:00:00
60
61Validation
62PersonDay 1Hora 1Close time 1Close Value 1Day 2Hora 2Close Time 2Close Value 2Entry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin idEntry DateHoraOld bin id
63Match D1 time < T1 No D2 match1/1/20211:00:00TRUETRUE2/1/20213:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
64Match D2 time < T2 No D1 match1/2/20212:00:00TRUETRUE2/2/20214:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
65Match D1 time = T1 No D2 match1/3/20213:00:00TRUETRUE2/3/20215:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
66Match D2 time = T2 No D1 match1/4/20214:00:00TRUETRUE2/4/20216:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
67Match D1 time > T1 No D2 match1/5/20215:00:00TRUETRUE2/5/20217:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
68Match D2 time > T2 No D1 match1/6/20216:00:00TRUETRUE2/6/20218:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
69Match mulitple D1 1st time < T1 2nd time = T11/7/20217:00:00TRUETRUE2/7/20219:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
70Match mulitple D2 1st time < T2 2nd time = T21/8/20218:00:00TRUETRUE2/8/202110:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
71Match mulitple D1 1st time < T1 2nd time > T11/9/20219:00:00TRUETRUE2/9/202111:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
72Match mulitple D2 1st time < T2 2nd time > T21/10/202110:00:00TRUETRUE2/10/202112:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
73Match mulitple D1 1st time = T1 2nd time < T11/11/202111:00:00TRUETRUE2/11/202113:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
74Match mulitple D2 1st time = T2 2nd time < T21/12/202112:00:00TRUETRUE2/12/202114:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
75Match mulitple D1 1st time = T1 2nd time = T11/13/202113:00:00TRUETRUE2/13/202115:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
76Match mulitple D2 1st time = T2 2nd time = T21/14/202114:00:00TRUETRUE2/14/202116:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
77Match mulitple D1 1st time = T1 2nd time > T11/15/202115:00:00TRUETRUE2/15/202117:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
78Match mulitple D2 1st time = T2 2nd time > T21/16/202116:00:00TRUETRUE2/16/202118:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
79No Matches for D1 or D21/17/202117:00:00TRUETRUE2/17/202119:00:00TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
Sheet2
Cell Formulas
RangeFormula
D63:E79,H63:AA79D63=EXACT(D23,D43)
 
Upvote 0
Solution
I just tested it worked perfectly I am deeply grateful for your help, I've been looking forward to it and I've been cheering and now seeing it all working makes me happy to know it will be a very productive job, I will be able to add the filtered data to identify patterns in behavior during investigations. thank you you are too successful for you, i hope i can count on you more often when i need your work was amazing
 
Upvote 0

Forum statistics

Threads
1,215,494
Messages
6,125,139
Members
449,207
Latest member
VictorSiwiide

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