ctstewart308
New Member
- Joined
- Jan 10, 2019
- Messages
- 4
Greetings! I'm a real newbie to VBA and I have been stumped by this formatting of cells. I have Excel files (lists for gages due calibration) that we post for production. These lists are Excel files generated daily by our gage management software. I have a macro that opens each file and saves it as an HTML file. During the time the Excel file is open I want to date and time stamp the file in cell G1. I want to examine column D and format the cells based on the following criteria, if the date in the cell is on or before today, fill the cell red. If there is no date I want to fill the cell yellow. I have the code below and for some reason the date code isn't working. I've tested it in another Excel macro without all the other code and it works. Also, i get an error "End if without block if" on the cell color fill code. Help please.
Code:
Private Sub Workbook_Open()
ChDir "C:\Users\UIDP3005\Documents\Macros"
Workbooks.Open Filename:="C:\Users\UIDP3005\Documents\Macros\TESTCOLOR.xls"
Dim G1 As Date
With Selection
.Value = Now
.NumberFormat = "m/d/yyyy h:mm:ss AM/PM"
End With
Dim D As Date
Columns(D).Font.Color = vbBlack
For D = 1 To Rows.Count
Next D
If Cells(D).Value <= ("today") And Not IsEmpty(Cells(D)) Then Cells(D).Font.Color = vbRed
If Cells(D).IsEmpty(Cells(D)) Then Cells(D).Font.Cell = vbYellow
End If
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\UIDP3005\Documents\Macros\TESTCOLOR.htm", FileFormat:=xlHtml, _
ReadOnlyRecommended:=False, CreateBackup:=False
Application.Wait Time + TimeSerial(0, 0, 2)
ActiveWorkbook.Close
End Sub