sarmadbari
New Member
- Joined
- Nov 2, 2020
- Messages
- 2
- Office Version
- 365
- 2019
- 2016
- 2013
- Platform
- Windows
Hi, I am currently using the below code to make multiple columns mandatory in case of data being input in first column "A"
============================================================================================
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 4) As String
lngRowCheck(1) = "A"
lngRowCheck(2) = "B"
lngRowCheck(3) = "C"
lngRowCheck(4) = "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 value in cell " & rngCell.Address)
rngCell.Select
Exit Sub
End If
Next
Next i
End Sub
=========================================================================================
However, I am getting issue in case of having a table that it is taking the whole table as a range and keeps on giving pop up messages until all the columns in the table is not filled. I am new to VBA and any help in this regard will be highly appreciated. Please note that I may also add new columns to range or delete any from above code.
============================================================================================
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 4) As String
lngRowCheck(1) = "A"
lngRowCheck(2) = "B"
lngRowCheck(3) = "C"
lngRowCheck(4) = "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 value in cell " & rngCell.Address)
rngCell.Select
Exit Sub
End If
Next
Next i
End Sub
=========================================================================================
However, I am getting issue in case of having a table that it is taking the whole table as a range and keeps on giving pop up messages until all the columns in the table is not filled. I am new to VBA and any help in this regard will be highly appreciated. Please note that I may also add new columns to range or delete any from above code.