Mandatory Column Validation in Excel

shobi1

New Member
Joined
Aug 31, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hello Everyone,

I used the below code to make few columns mandatory in Excel. However, this works on all the sheets of the workbook but I want this validation only on a particular sheet. Can someone guide me with this
Thanks in advance. Also, please let me know if this can be done without VBA
Shobi

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)


Dim rngCell As Range
Dim lngLstRow As Long, lngTCols As Long
Dim lngRowCheck(1 To 5) As String


lngRowCheck(1) = "A"
lngRowCheck(2) = "B"
lngRowCheck(3) = "C"
lngRowCheck(4) = "D"
lngRowCheck(5) = "E"


lngLstRow = ActiveSheet.UsedRange.Rows.Count


For i = 1 To UBound(lngRowCheck)
For Each rngCell In Range(lngRowCheck(i) & "2:" & lngRowCheck(i) & lngLstRow)
If rngCell.Value = 0 Then
MsgBox ("Please enter a name in cell " & rngCell.Address)
rngCell.Select
End If
Next
Next i


End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this. Replace Sheet1 at the code with your Desired Sheet name:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rngCell As Range, lngLstRow As Long, lngTCols As Long
Dim lngRowCheck(1 To 5) As String, Sh1 as Worksheet
Set Sh1 = Sheets("Sheet1")
lngRowCheck(1) = "A"
lngRowCheck(2) = "B"
lngRowCheck(3) = "C"
lngRowCheck(4) = "D"
lngRowCheck(5) = "E"

lngLstRow = Sh1.UsedRange.Rows.Count

For i = 1 To UBound(lngRowCheck)
For Each rngCell In Sh1.Range(lngRowCheck(i) & "2:" & lngRowCheck(i) & lngLstRow)
If rngCell.Value = 0 Then
MsgBox ("Please enter a name in cell " & rngCell.Address)
rngCell.Select
End If
Next
Next i
End Sub
 
Upvote 0
Try this. Replace Sheet1 at the code with your Desired Sheet name:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rngCell As Range, lngLstRow As Long, lngTCols As Long
Dim lngRowCheck(1 To 5) As String, Sh1 as Worksheet
Set Sh1 = Sheets("Sheet1")
lngRowCheck(1) = "A"
lngRowCheck(2) = "B"
lngRowCheck(3) = "C"
lngRowCheck(4) = "D"
lngRowCheck(5) = "E"

lngLstRow = Sh1.UsedRange.Rows.Count

For i = 1 To UBound(lngRowCheck)
For Each rngCell In Sh1.Range(lngRowCheck(i) & "2:" & lngRowCheck(i) & lngLstRow)
If rngCell.Value = 0 Then
MsgBox ("Please enter a name in cell " & rngCell.Address)
rngCell.Select
End If
Next
Next i
End Sub
Thank you so very much.. it worked..
 
Upvote 0
Or at The Excel Try this:
First select range that you Want then
1. GO to Data Tab
2. Select Data Validation
3. At the windows appears, Select Setting Tab
4. At The Allow dropdown menu Select Custom
5. Uncheck Ignore Blank
6. At the Formula Box input this (Replace A1 with the first cell address at your range)
Excel Formula:
=NOT(ISNUMBER(A1))
7. Go to Error Alert Tab
8. At the Error Message Box input message you want. For Example:
Rich (BB code):
Please Enter Name In The Cell
9. Press OK.

12345.jpg



123456.jpg
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top