To make multiple columns mandatory based on data in one of mandatory columns

sarmadbari

New Member
Joined
Nov 2, 2020
Messages
2
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. 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.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Are you saying that if the user enters a value in column A, you want to force data entry into columns B, C and E?
 
Upvote 0
Are you saying that if the user enters a value in column A, you want to force data entry into columns B, C and E?
yes, which is all ok as of now with this coding. The only issue that I am facing is that this code keeps on giving the message to enter value in a particular column until the whole table end (lets say, if I have 5 rows in a table starting from row 2 then the pop up message keeps on coming for column B until all 5 rows are not filled and so on. I wanted the pop up message to only appear if Column "C" have any value in a particular row so that user can be forced to fill other mandatory cells with in that row which in my case are columns "C,D,I,L,N,O,P, & T". Furthermore, I wanted this macro to work on a particular sheet not the whole workbook.

Any help in this regard would be highly appreciated. Many thanks.
 
Upvote 0
Try:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim rng As Range, colArr As Variant, i As Long
    colArr = Array("C", "D", "I", "L", "N", "O", "P", "T")
    For Each rng In Range("A2", Range("A" & Rows.Count).End(xlUp))
        If rng <> "" Then
            For i = LBound(colArr) To UBound(colArr)
                If Range(colArr(i) & rng.Row) = "" Then
                    MsgBox ("Please enter a value in cell " & Range(colArr(i) & rng.Row).Address(0, 0))
                    Cancel = True
                    Range(colArr(i) & rng.Row).Select
                    Exit Sub
                End If
            Next i
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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