To run an auto macro, you need to enter the macro in the Workbook object
Open the macro editor. On the left is a list of open workbooks, with your workbook amongst them. DoubleClick on the ThisWorkbook of the workbook where you want to add the auto macro.
The editor will show a blank screen. Just above the cursor you see a dropdown box with '(General)'. Click on it and select 'Workbook'.
A new sub will be written called Workbook_Open()
This will run when the workbook opens.
So now you can do your stuff in here. You want to compare two values, but you need to specify which sheet these values are in, in case someone closed the workbook on a different sheet. Let's assume the data you have is on Sheet1.
Then you say you want to 'Clear the selected cells'. The workbook is opening, so how can you know what the selected cells are? I am assuming you want to clear the cells mentioned in your comparison:
Code:
Private Sub Workbook_Open()
If Left(Worksheets("Sheet1").Range("B2"), 5) >= Left(Worksheets("Sheet1").Range("A35"), 5) Then
Worksheets("Sheet1").Range("B2") = ""
Worksheets("Sheet1").Range("A35") = ""
End If
End Sub
You mention the 'formula is based on dates'. Do these two cells contain dates (as in real Excel dates, not text strings)? then you can just compare the values:
Code:
Private Sub Workbook_Open()
If Worksheets("Sheet1").Range("B2") >= Worksheets("Sheet1").Range("A35") Then
Worksheets("Sheet1").Range("B2") = ""
Worksheets("Sheet1").Range("A35") = ""
End If
End Sub