简体   繁体   中英

Display message box in excel

I need to compare two date columns in excel(vba). If the cell values are equal, a message box has to be displayed with "TARGET ACHIEVED", "YES" or "NO" option. Based on the value selected, 2nd column cell color has to be changed - YES - orange - NO - blue

Following code will do it.

Dim Date1 As String
Dim Date2 As String
Dim msgResult As VbMsgBoxResult

Date1 = ThisWorkbook.Sheets(1).Cells(1)
Date2 = ThisWorkbook.Sheets(1).Cells(2)

If IsDate(Date1) And IsDate(Date2) Then
    If CDate(Date1) = CDate(Date2) Then
        msgResult = MsgBox("TARGET ACHIEVED", vbYesNo)

        If vbYes = msgResult Then
            ' code for Yes handling
            ThisWorkbook.Sheets(1).Cells(1).Interior.ColorIndex = 46 'orange
        Else
            ' code for NO handling
            ThisWorkbook.Sheets(1).Cells(1).Interior.ColorIndex = 5 'blue color
        End If
    End If
End If

You can get more Excel colour codes here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM