VBA: force the user to fill certain cell

lunatu

Board Regular
Joined
Feb 5, 2021
Messages
77
Office Version
  1. 2010
Platform
  1. Windows
  2. Web
Hi,

is it possible to force the user to fill a certain cell before other cell can be filled? For example I want the user to fill cell A before cell B can be filled? Or maybe to display a comment when trying to fill cell B but the cell A is still empty.

Thanks in advance :)
Br Luna
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("B1")
If Cells(Target.Row, "A") <> "" Then Exit Sub ' if B1 is target, A1 is condition
If Not Intersect(Target, rng) Is Nothing Then
   MsgBox "Try to fill cell A"
   Range("A1").Select
End If
End Sub
 
Upvote 0
Solution
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("B1")
If Cells(Target.Row, "A") <> "" Then Exit Sub ' if B1 is target, A1 is condition
If Not Intersect(Target, rng) Is Nothing Then
   MsgBox "Try to fill cell A"
   Range("A1").Select
End If
End Sub
Thanks this is working perfectly! :) But now Im running into another proble as I have never combined multiple private sub worksheet change's. Maybe you could help me with this as well? The other code Im using is:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, [Q:Q]) Is Nothing Then
        If UCase(Target) = UCase("Negotiate") Then
            Target.Offset(, 2) = Int(Now())
        End If
  
    If Not Intersect(Target, [Q:Q]) Is Nothing Then
        If UCase(Target) = UCase("Close (won)") Then
            Target.Offset(, 3) = Int(Now())
        End If
        
     If Not Intersect(Target, [Q:Q]) Is Nothing Then
        If UCase(Target) = UCase("Close (part-won)") Then
            Target.Offset(, 3) = Int(Now())
        End If
        
          If Not Intersect(Target, [Q:Q]) Is Nothing Then
        If UCase(Target) = UCase("Close (lost)") Then
            Target.Offset(, 4) = Int(Now())
        End If

    End If
    End If
    End If
    End If
End Sub
 
Upvote 0
Note that your original code can be simplified. When you are checking the same range for different values, it is more efficient to use CASE statements than multiple and/or nested IF statements. I have done that in the code below.

I also made it so it will apply to all of column B (not just row 1), and will remove the attempted entry in column B if column A is blank.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
'   Exit if multiple cells updated at once
    If Target.CountLarge > 1 Then Exit Sub
   
'   ***ORIGINAL CODE (SIMPLIFIED)***
    If Not Intersect(Target, [Q:Q]) Is Nothing Then
        Select Case UCase(Target)
            Case "NEGOTIATE"
                Target.Offset(, 2) = Int(Now())
            Case "CLOSE (WON)", "CLOSE (PART-WON)"
                Target.Offset(, 3) = Int(Now())
            Case "CLOSE (LOST)"
                Target.Offset(, 4) = Int(Now())
        End Select
    End If
   
'   ***NEW CODE (applies to all of column B)***
    If Not Intersect(Target, [B:B]) Is Nothing Then
'       Check to see if column A is populated
        If Target.Offset(0, -1) = "" Then
            Application.EnableEvents = False
            Target.ClearContents
            Application.EnableEvents = True
            MsgBox "You must populate column A before column B!", vbOKOnly, "ENTRY ERROR!"
        End If
    End If

End Sub
 
Upvote 0
Note that your original code can be simplified. When you are checking the same range for different values, it is more efficient to use CASE statements than multiple and/or nested IF statements. I have done that in the code below.

I also made it so it will apply to all of column B (not just row 1), and will remove the attempted entry in column B if column A is blank.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
'   Exit if multiple cells updated at once
    If Target.CountLarge > 1 Then Exit Sub
  
'   ***ORIGINAL CODE (SIMPLIFIED)***
    If Not Intersect(Target, [Q:Q]) Is Nothing Then
        Select Case UCase(Target)
            Case "NEGOTIATE"
                Target.Offset(, 2) = Int(Now())
            Case "CLOSE (WON)", "CLOSE (PART-WON)"
                Target.Offset(, 3) = Int(Now())
            Case "CLOSE (LOST)"
                Target.Offset(, 4) = Int(Now())
        End Select
    End If
  
'   ***NEW CODE (applies to all of column B)***
    If Not Intersect(Target, [B:B]) Is Nothing Then
'       Check to see if column A is populated
        If Target.Offset(0, -1) = "" Then
            Application.EnableEvents = False
            Target.ClearContents
            Application.EnableEvents = True
            MsgBox "You must populate column A before column B!", vbOKOnly, "ENTRY ERROR!"
        End If
    End If

End Sub
Thanks a lot, this was very good tip for a beginner like me ?
 
Upvote 0
Note that your original code can be simplified. When you are checking the same range for different values, it is more efficient to use CASE statements than multiple and/or nested IF statements. I have done that in the code below.

I also made it so it will apply to all of column B (not just row 1), and will remove the attempted entry in column B if column A is blank.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
'   Exit if multiple cells updated at once
    If Target.CountLarge > 1 Then Exit Sub
  
'   ***ORIGINAL CODE (SIMPLIFIED)***
    If Not Intersect(Target, [Q:Q]) Is Nothing Then
        Select Case UCase(Target)
            Case "NEGOTIATE"
                Target.Offset(, 2) = Int(Now())
            Case "CLOSE (WON)", "CLOSE (PART-WON)"
                Target.Offset(, 3) = Int(Now())
            Case "CLOSE (LOST)"
                Target.Offset(, 4) = Int(Now())
        End Select
    End If
  
'   ***NEW CODE (applies to all of column B)***
    If Not Intersect(Target, [B:B]) Is Nothing Then
'       Check to see if column A is populated
        If Target.Offset(0, -1) = "" Then
            Application.EnableEvents = False
            Target.ClearContents
            Application.EnableEvents = True
            MsgBox "You must populate column A before column B!", vbOKOnly, "ENTRY ERROR!"
        End If
    End If

End Sub
Hi,
still one question regarding my original code. If I also want the value on cell R to be > 0 before the date is displayed. There should not be matter in which order you fill the cells, so there can be first specific text in cell Q and after that the value in cell R increases or the other way (first cell R increase and after that you change the text in cell Q).
 
Upvote 0
Hi,
still one question regarding my original code. If I also want the value on cell R to be > 0 before the date is displayed. There should not be matter in which order you fill the cells, so there can be first specific text in cell Q and after that the value in cell R increases or the other way (first cell R increase and after that you change the text in cell Q).
That is a whole new question (different than your original) and as such, should be posted to a new thread.
Also, by doing that, it will appear in the "Unanswered threads" listing that many people use to look for new unanswered questions to respond to.
 
Upvote 0
That is a whole new question (different than your original) and as such, should be posted to a new thread.
Also, by doing that, it will appear in the "Unanswered threads" listing that many people use to look for new unanswered questions to respond to.
Great I'll do that :)
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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