LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Trying to run macro on the BeforeSave event

First off, I'm new to writing macros in excel. I have a created a
simple macro to check if a cell is blank and if it is to change the
highlighted color to yellow as a required field indicator. The macro
runs fine when I run it manually but I want this to run when the user
tries to save the document so it will not let them save their changes
until all Required fields are filled in. Here is my code. Can you let
me know what I'm doing wrong:


Sub Workbook_BeforeSave()
Dim rowcount As Integer, msg As String, col As String, cnt As
Integer

msg = "The highlighted Fields are required." & vbCrLf & _
"Please fill in all required fields."

lastrow = (Cells(Rows.Count, "A").End(xlUp).Row) - 1

For i = 2 To lastrow
If Cells(i, "A").Value = "" Then
Range("A" & i).Interior.ColorIndex = 6
End If
Next i

For i = 2 To lastrow
If Cells(i, "B").Value = "" Then
Range("B" & i).Interior.ColorIndex = 6
End If
Next i

For i = 2 To lastrow
If Cells(i, "C").Value = "" Then
Range("C" & i).Interior.ColorIndex = 6
End If
Next i


MsgBox msg, vbOKOnly, "Error: Some Required Fields are missing."
End Sub

 
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
Disabling BeforeSave event bren1519 Excel Programming 2 April 17th 06 07:15 PM
BeforeSave Event Question Andibevan[_4_] Excel Programming 0 August 11th 05 11:35 AM
BeforeSave event Carl Bowman Excel Discussion (Misc queries) 4 February 6th 05 12:28 PM
BeforeSave event j23 Excel Programming 0 April 6th 04 11:15 AM
BeforeSave workbook event Cindy Excel Programming 15 February 10th 04 04:28 PM


All times are GMT +1. The time now is 12:31 AM.

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"