tags:

views:

65

answers:

3

Hello: I have a set of cells on a worksheet called "Docs". The cell range is (B13:C23). When users get taken to this page, they are meant to fill out each of these cells with a value from 0 through 6. My Question: Is there some code that I can attach to this sheet where, if a user does not fill in a cell with anything (ie. leaves it blank) and tries to leave the sheet or close the workbook, they are somehow reminded to fill it in? Or is there a way to not let them leave the sheet until it's completed? Thanks.. Allan

A: 

Try writing a vba macro. Alt + F11 opens the VB Editor. Check out this SO post for VBA tutorials.

There are worksheet and workbook events that you can use. For example, Workbook_BeforeClose or Workbook_SheetChange. If you create methods for those events you can put code inside that checks that the required cells are filled.

froadie
+1  A: 

You could give these cells conditional formatting, making them red if empty.

Matchu
+1 for a no-code, user-friendly suggestion
Otaku
I can't see an optin for empty cell. If it's zero, it won't work as this is one of the required values. Thanks..
Allan
+1  A: 

You can attach a macro to the change event of the form. Excel comes with built in validation but it does not work that well. For instance if someone pastes a value into the cell it does not validate what is pasted.

Start by creating a range by selecting the range of cells to be validated, right click and select "Name a Range". Note that I am testing this with Excel 2007. Say you call your range "InputRange".

Then open the VBA editor and create a procedure for the change event.

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim vrange As Range, cell As Range
  Set vrange = Range("InputRange")
  If Intersect(vrange, Target) Is Nothing Then Exit Sub
  For Each cell In Intersect(vrange, Target)
     If cell.Value < 1 Or cell.Value > 6 Then
        MsgBox "Invalid Entry", vbCritical
        Application.EnableEvents = False
        cell.ClearContents
        cell.Activate
        Application.EnableEvents = True

     End If
  Next cell
End Sub

Note you can attach to any event that suits you.

Vincent Ramdhanie
Vincent..I'm on excel 2003. Doesn't have the range on right click. However I can insert it into a module for the worksheet. Will the above code work as follows: I have an object on the sheet which, when pressed copies the values in the range to another location on the workbook. I want to be able to click the object and for the code to check the validity of the cells in the range before proceeding. If something is wrong, I would like a message saying, perhaps "Cell data incorrect..please modify". So they press ok to correct data....try again. Once it's ok the rest of code runs. Possible?
Allan
@Allan. In Excel 2003 things should work pretty much the same as I described. This link http://www.ehow.com/how_2020061_define-ranges-excel.html explains how to create a range in 2003. And yes, you will be able to do what you describe, it is possible. You should look up VBA, it is very powerful tool. Maybe start from here: http://msdn.microsoft.com/en-us/library/aa272254%28office.11%29.aspx.
Vincent Ramdhanie
Vincent: I have a bit of a problem. I have two ranges defined (not just one) as I previously thought I could use. The first range (R1) needs valuse 0 thru 6. The other (R2) needs values 0 or 1 only. I tried stacking up the code one on top of other but desn't work with dual ranges. Probably my lack of knowledge. Works great on jusr (R1). How can I get both ranges working for same sheet? Problen #2). If the cell is left blank (not acceptable) users can move forward. How can I force them to put a value into each cell? Thanks...working great so far.
Allan
@Allan You can declare a second range in the same procedure Set range2 = Range("SecondRange") and then add a second For Each to go over the second range after the first is complete. You can also detect blank cells like this: if isNull(cell) then...
Vincent Ramdhanie
ok Vincent, I will see if I can achieve the result I need by trying as you suggest. Thanks...Allan
Allan
Vincent...I couldn't get Set range2 to work. debug said I had duplicate argument or similar. Guess my lack of knowledge shows. Anyway Thank you for your help.
Allan