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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Welcome to the MrExcel board!

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are and do not have to manually type out sample data. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Try this with a copy of your workbook.

VBA Code:
Sub Clear_Data()
  Dim c As Long
  
  For c = Cells(2, Columns.Count).End(xlToLeft).Column - 2 To 3 Step -3
    With Cells(2, c)
      If IsDate(.Value) And Cells(2, 1).Value <> .Value And Cells(2, 2).Value <> .Value Then .Resize(, 3).ClearContents
    End With
  Next c
End Sub
 
Upvote 0
@Peter_SSs your solution is very elegant!! Mine is a bit rougher, but I allow for multiple rows.

@*Jhonatan321 as Peter suggests try the below on a copy of your workbook

VBA Code:
Sub RemoveData()
    Dim i As Integer
    Dim j As Integer
    Dim D1 As Date
    Dim D2 As Date
    Dim a As Range
    Dim LR As Long
    Dim LC As Long
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    LC = Cells(1, Columns.Count).End(xlToLeft).Column
    
   For i = 2 To LR
        D1 = Cells(i, 1).Value
        D2 = Cells(i, 2).Value
        For j = 3 To LC - 2
            If Cells(i, j).Value <> D1 Then
                If Cells(i, j).Value <> D2 Then
                    Range(Cells(i, j), Cells(i, j + 2)).ClearContents
                End If
            End If
            j = j + 2
        Next j
    Next i
                
End Sub
 
Upvote 0
@Peter_SSs your solution is very elegant!! Mine is a bit rougher, but I allow for multiple rows.

@*Jhonatan321 as Peter suggests try the below on a copy of your workbook

VBA Code:
Sub RemoveData()
    Dim i As Integer
    Dim j As Integer
    Dim D1 As Date
    Dim D2 As Date
    Dim a As Range
    Dim LR As Long
    Dim LC As Long
   
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    LC = Cells(1, Columns.Count).End(xlToLeft).Column
   
   For i = 2 To LR
        D1 = Cells(i, 1).Value
        D2 = Cells(i, 2).Value
        For j = 3 To LC - 2
            If Cells(i, j).Value <> D1 Then
                If Cells(i, j).Value <> D2 Then
                    Range(Cells(i, j), Cells(i, j + 2)).ClearContents
                End If
            End If
            j = j + 2
        Next j
    Next i
               
End Sub



Thank you very much Crystallizer, it worked perfectly, Some heroes wear capes, mine knows how to program very well in VBA, well my next challenge in vba I hope you can help me on this one :)

now that I have only the date and time specified in this line, I have added a column in cell B1 "time 1" and in cell E1 "time 2" and next to the blank fields close time 1, close time 2 and to the right of them close value 1, close value 2

Below is what I need to happen

1) check in this line all the same dates A2 and E2 and bring the time closest to B2 and paste in cell C2 and time closest to F2 paste in cell G2

2) Bring the closest time in this line starting at I2, the criteria of this time must be less than or equal, bring only the less than or equal time comparing the specified dates

3) Bring the cell next to the nearest time to cell D2 and G2

follow an example of before and after
1626395179610.png

116cf2a5-5a85-409d-b405-ef7a2df01b44 (1).xlsx
ABCDEFGHIJKLMNOPQRST
1day 1hora 1close time 1close value 1day 1hora 2close time 2close value 2entry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_id
218/06/202112:00:0013/06/202115:00:0018/06/202110:00:00P-2-E135G37513/06/202112:30:00P-2-E124G23418/06/202115:00:00ts_ps_NED13/06/202114:00:00ts_ps_NED
DFFS
 
Upvote 0
It seems you have removed some columns and added others from the original request. It would be best if you had all columns you want (original, columns you want added and columns you want deleted) laid out so that the solution can do everything you want in one pass. Please post ALL the columns and indicate if they are to be added or deleted or left as is.

Solutions 20210713.xlsm
ABCDEFGHIJKLMNOPQRST
1Date 1Date 2Entry_DateHoraOld_bin_idEntry_DateHoraOld_bin_idEntry_DateHoraOld_bin_idEntry_DateHoraOld_bin_idEntry_DateHoraOld_bin_idEntry_DateHoraOld_bin_id
21/1/20201/1/20211/1/2021451/1/2020451/1/202155
Sheet2
 
Upvote 0
Bem-vindo ao quadro MrExcel!

MrExcel tem uma ferramenta chamada “XL2BB” que permite postar amostras de seus dados que nos permitirão copiar / colar em nossas planilhas Excel, para que possamos trabalhar com a mesma cópia de dados que você e não tenha que digitar manualmente dados de amostra. As instruções sobre como usar esta ferramenta podem ser encontradas aqui: XL2BB Add-in

Observe que também há um fórum " Teste aqui " neste fórum. Este é um lugar onde você pode testar usando esta ferramenta (ou qualquer outra técnica de postagem que você queira testar) antes de tentar usar essas ferramentas em suas postagens reais.

Tente fazer isso com uma cópia de sua pasta de trabalho.

[CODE = vba]
Sub Clear_Data ()
Dim c As Long

Para c = Cells (2, Columns.Count) .End (xlToLeft) .Column - 2 a 3 Etapa -3
Com células (2, c)
If IsDate (.Value) And Cells (2, 1) .Value <> .Value And Cells (2, 2) .Value <> .Value Then .Resize (, 3) .ClearContents
Terminar com
Próximo c
End Sub

[/CÓDIGO]
me ajudou muito Peter, agradeço sua contribuição, para preencher essa planilha preciso de um ajuste na minha resposta acima ao Crystalyzer detalhei o que preciso que aconteça, usei a ferramenta XL2BB para te ajudar nessa validação, eu conto em seu apoio
 
Upvote 0
in English

it helped me a lot Peter, I am grateful for your contribution, to complete this worksheet I need an adjustment in my answer above to Crystalyzer I detailed what I need to happen, I used the XL2BB tool to help you in this validation, I count on your support
 
Upvote 0
I replied above with the information I need in order to assist you further.
Good morning Crystallizer only columns D2, E2 and H2, I2 will be empty to be filled with the closest time match to the date and time specified in B2, C2 and F2, G2


follow below all the original columns, observe the result before and after is the same objective as the example above

I will do this same process in the lines below I would like the code to allow this


1626443846563.png

116cf2a5-5a85-409d-b405-ef7a2df01b44 (1).xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAV
1personday 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_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_id
2rodosilv15/06/202114:00:0019/06/202109:30:0015/06/202113:44:43tsDMGCRET5019/06/202108:58:36SC021615/06/202114:21:40SC019715/06/202114:21:57SC019715/06/202113:45:12tsDMGCRET5015/06/202114:22:16SC019719/06/202111:26:33SC005919/06/202110:05:16SC021019/06/202110:05:06SC021015/06/202111:44:40SC019719/06/202111:26:32SC005919/06/202111:26:32SC005919/06/202111:26:32SC0059
Planilha3
 
Upvote 0
Good morning Crystallizer only columns D2, E2 and H2, I2 will be empty to be filled with the closest time match to the date and time specified in B2, C2 and F2, G2


follow below all the original columns, observe the result before and after is the same objective as the example above

I will do this same process in the lines below I would like the code to allow this


View attachment 42947
116cf2a5-5a85-409d-b405-ef7a2df01b44 (1).xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAV
1personday 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_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_identry_datehoraold_bin_id
2rodosilv15/06/202114:00:0019/06/202109:30:0015/06/202113:44:43tsDMGCRET5019/06/202108:58:36SC021615/06/202114:21:40SC019715/06/202114:21:57SC019715/06/202113:45:12tsDMGCRET5015/06/202114:22:16SC019719/06/202111:26:33SC005919/06/202110:05:16SC021019/06/202110:05:06SC021015/06/202111:44:40SC019719/06/202111:26:32SC005919/06/202111:26:32SC005919/06/202111:26:32SC0059
Planilha3



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
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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