Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
cdb cdb is offline
external usenet poster
 
Posts: 62
Default Highlighting cells when errors occur

I have the code below in a spreadsheet that is filled in by various people,
and what it is supposed to do is prevent saving if any of the mandatory
fields aren't filled in.

This all works fine, but now I want to change it so it highlights the
individual cells that need filling in, but can't get my head around it. Can
anyone offer any help??

Cheers,

cdb

Code:

Sub ValidationCheck()

Dim myError As String, myMsg As String, Counter As Integer, EndCount As
Integer, myError2 As String

Counter = 3

Range("A3").Select
'Selection.End(xlDown).Select
EndCount = 53

myError = ""
myMsg = ""

While Counter < EndCount

myError2 = myError

If (Range("A" & Counter) = "" And Range("B" & Counter) = "" And
Range("C" & Counter) = "" And Range("D" & Counter) = "" And Range("E" &
Counter) = "" And Range("F" & Counter) = "" And Range("H" & Counter) = "" And
Range("I" & Counter) = "" And Range("J" & Counter) = "" And Range("N" &
Counter) = "" And Range("O" & Counter) = "" And Range("P" & Counter) = "" And
Range("Q" & Counter) = "" And Range("R" & Counter) = "" And Range("S" &
Counter) = "" And Range("W" & Counter) = "" And Range("X" & Counter) = "" And
Range("Z" & Counter) = "" And Range("AA" & Counter) = "" And Range("AB" &
Counter) = "" And Range("AC" & Counter) = "" And Range("AL" & Counter) = ""
And Range("AN" & Counter) = "" And Range("AO" & Counter) = "" And Range("AQ"
& Counter) = "" And Range("AR" & Counter) = "" And Range("AS" & Counter) = ""
And Range("AT" & Counter) = "") = True Then GoTo EndBit
If (Range("A" & Counter) < "" And Range("B" & Counter) < "" And
Range("C" & Counter) < "" And Range("D" & Counter) < "" And Range("E" &
Counter) < "" And Range("F" & Counter) < "" And Range("H" & Counter) < ""
And Range("I" & Counter) < "" And Range("J" & Counter) < "" And Range("N" &
Counter) < "" And Range("O" & Counter) < "" And Range("P" & Counter) < ""
And Range("Q" & Counter) < "" And Range("R" & Counter) < "" And Range("S" &
Counter) < "" And Range("W" & Counter) < "" And Range("X" & Counter) < ""
And Range("Z" & Counter) < "" And Range("AA" & Counter) < "" And Range("AB"
& Counter) < "" And Range("AC" & Counter) < "" And Range("AL" & Counter) <
"" And Range("AN" & Counter) < "" And Range("AO" & Counter) < "" And
Range("AQ" & Counter) < "" And Range("AR" & Counter) < "" And Range("AS" &
Counter) < "" And Range("AT" & Counter) < "") = False Then myError =
myError & "- Not all the mandatory fields have been filled in on Line " &
Counter & vbCrLf
If Range("H" & Counter) = "Mrs" And Range("L" & Counter) = "" Then
myError = myError & "- Previous Surname missing on Line " & Counter & vbCrLf
If Range("AC" & Counter) = "Yes" And Range("AD" & Counter) = "" Then
myError = myError & "- Resident From Date missing on Line " & Counter & vbCrLf
If Range("AC" & Counter) = "Yes" And Range("AE" & Counter) = "" Then
myError = myError & "- Previous Address 1 missing on Line " & Counter & vbCrLf
If Range("AF" & Counter) = "Yes" And Range("AG" & Counter) = "" Then
myError = myError & "- Resident From Date missing on Line " & Counter & vbCrLf
If Range("AF" & Counter) = "Yes" And Range("AH" & Counter) = "" Then
myError = myError & "- Previous Address 2 missing on Line " & Counter & vbCrLf
If Range("AI" & Counter) = "Yes" And Range("AJ" & Counter) = "" Then
myError = myError & "- Resident From Date missing on Line " & Counter & vbCrLf
If Range("AI" & Counter) = "Yes" And Range("AK" & Counter) = "" Then
myError = myError & "- Previous Address 3 missing on Line " & Counter & vbCrLf

If myError2 < myError Then Range("A" & Counter & ":AT" &
Counter).Interior.ColorIndex = 6

Counter = Counter + 1
Wend

EndBit:
If myError < "" Then
myMsg = MsgBox("The following errors have occured while trying to
save this document:" & vbCrLf & vbCrLf & myError, vbCritical + vbOKOnly,
"Recruitment Campaign Request")
If myMsg = vbOK Then Cancel = True
End If


End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Highlighting cells when errors occur

In module for sheet with required input:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c In Range("A1:A3")
If IsEmpty(c) Then
c.Interior.ColorIndex = 3
Else
c.Interior.ColorIndex = 0
End If
Next c
End Sub

In ThisWorkbook module:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim c As Range
For Each c In Range("A1:A3")
If IsEmpty(c) Then Cancel = True
Next c
If Cancel = True Then MsgBox "File can't be saved if blah-blah cells
are empty."
End Sub

Change cell references to suit.

Hth,
Merjet


  #3   Report Post  
Posted to microsoft.public.excel.programming
cdb cdb is offline
external usenet poster
 
Posts: 62
Default Highlighting cells when errors occur

Cheers for this. It gives me a place to start from. The problem I'm going to
have is that not every cell is mandatory, so how do I do this based on a
wider selection?

eg
Columns A, B, C, F, G, I, K, L and T are mandatory

There are also some other columns that are mandatory based on the outputs of
others

eg
Column M is mandatory is Column L = "Yes"

How do I build this in? I also need the error message that was in my
original code to be kept to, so can I do both of these in the same code?

Cheers,

cdb

"merjet" wrote:

In module for sheet with required input:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c In Range("A1:A3")
If IsEmpty(c) Then
c.Interior.ColorIndex = 3
Else
c.Interior.ColorIndex = 0
End If
Next c
End Sub

In ThisWorkbook module:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim c As Range
For Each c In Range("A1:A3")
If IsEmpty(c) Then Cancel = True
Next c
If Cancel = True Then MsgBox "File can't be saved if blah-blah cells
are empty."
End Sub

Change cell references to suit.

Hth,
Merjet



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Highlighting cells when errors occur

Column M is mandatory is Column L = "Yes"

How do I build this in? I also need the error message that was in my
original code to be kept to, so can I do both of these in the same code?


I don't imagine you mean entire columns, so I will assume only one
row. Add the following.

If Range("L1") = "Yes" And IsEmpty(Range("M1")) Then
c.Interior.ColorIndex = 3
Else
c.Interior.ColorIndex = 0
End If

If Range("L1") = "Yes" And IsEmpty(Range("M1")) _
Then Cancel = True

Simply substitute your MsgBox text for mine.

Merjet

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Highlighting cells Rich Blanchard New Users to Excel 0 December 8th 10 03:50 PM
Count cells with numbers and ignore cells with errors WonderingaboutMicrosoft Excel Discussion (Misc queries) 6 December 10th 06 08:03 PM
Why errors occur in wrkbook w/ vba when saved on Shared folder? Shawna Hartman Excel Programming 1 April 10th 06 04:37 PM
Highlighting blanks via GO TO SPECIAL is not highlighting blank cells - HELP, I'm totally stuck. Jamie Furlong Excel Discussion (Misc queries) 6 August 28th 05 09:27 PM
Highlighting 'corresponding' cells R. Todd Miller Excel Programming 2 July 18th 03 07:46 PM


All times are GMT +1. The time now is 12:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"