You are here:

Excel/Alert for Blank values

Advertisement


Question
QUESTION: Hey Tom,

I can see you have replied to 18350 questions. Thanks for contributing to this community.This is my first question and first vb code that i m trying to write.

Logic is :

if column c contains "yes" , column D and E cant be blank
Code should be like:

if ( C1= yes and D1 or E1 or both are blank , it should highlight the blank cell with color say light blue and with message box , these cells cant be blank)


else
do nothing

Till , last row , ie.e the column A contains values.


See if you can help me in this.

Thanks in Advance.

Dhwani

ANSWER: Dhwani,

this code was tested and worked for me according to my understanding of what you describe.
Test it on a copy of your workbook until you are satisfied it works as you desire.

Sub abcd()
Dim i As Long
Dim r As Range
Dim cell As Range
' starting with C1, find the last used cell in column C
Set r = Range("C1", Cells(Rows.Count, "C").End(xlUp))
' loop through those cells in column C
For Each cell In r
 ' check if the cell contains yes
 If LCase(cell.Value) = "yes" Then
  ' check the cell to the right and the second cell
  ' to the right
  
  For i = 1 To 2
   ' see if the cell is empty/blank
   If Len(Trim(cell.Offset(0, i))) = 0 Then
       ' it is so color the interior light blue and put in
       ' a message that it can't be blank
       With cell.Offset(0, i)
         .Value = "Can't be blank"
         .Interior.ColorIndex = 8
       End With
   End If
  Next
 End If
Next
End Sub

--
Regards,
Tom Ogilvy



---------- FOLLOW-UP ----------

QUESTION: Hey Tom,

Based on my explanation you have done it perfectly.

But I think I explained it wrongly. Just one small modification , if there is any blank cell , it should change the color(already done by you)

now there can be 2 possibilities:

1. Any blank cell exists

Msgbox : Highlighted cell must contain a value , please correct

2. If no blank cell exists

Msgbox : good to go


Thanks for helping. Good day to You!!

Answer
Dhwani,

It is unclear where the message "good to go" should be placed.  I will put it in column F


Sub abcd()
Dim i As Long
Dim r As Range
Dim cell As Range
Dim bBlank As Boolean
' starting with C1, find the last used cell in column C
Set r = Range("C1", Cells(Rows.Count, "C").End(xlUp))
' loop through those cells in column C
For Each cell In r
 ' check if the cell contains yes
 If LCase(cell.Value) = "yes" Then
  ' check the cell to the right and the second cell
  ' to the right
  ' use a Boolean variable to track whether
  ' a blank cell is found
  bBlank = False  
  For i = 1 To 2
   ' see if the cell is empty/blank
   If Len(Trim(cell.Offset(0, i))) = 0 Then
       ' it is so color the interior light blue and put in
       ' a message that it can't be blank
       
       With cell.Offset(0, i)
         .Interior.ColorIndex = 8
         bBlank = True
       End With
   End If
  Next
  If bBlank Then
     cell.Offset(0, 3) = "Highlighted cell must contain a value, please correct"
  Else
    cell.Offset(0, 3) = "good to go"
  End If
 End If
Next
End Sub

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.