Locking cells If previous cell isnt filled

holy_eti

New Member
Joined
Jun 5, 2018
Messages
38
Hello, I would like to know if there is anyway to "lock" a cell if the previous cell isnt filled. In other words, I have to fill a file with multiple cells. If I accidently forget to fill one cell and pass it up, I want Excel/VBA to tell me that I have forgot to fill a cell. I have a small idea but I dont really know how to properly code it on VBA. My idea is that if the cell "A1" is filled, than the cell "A2" must be filled, so VBA must tell me if I went over cell A2. I can do a small code but Excel keep thinking that when I select a case, the message Box of my empty cell pops before i can write anything. I want it to pop if I go on an empty cell, than leave it without writting anything in it.

THank you
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Copy and paste this macro into the worksheet code module.
Your code allows someone to enter text in cell G7 when cell H12 is blank.
I think this modification to your code fixes the problem...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("G7:H12")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    Dim fEmptyRow As Long
    If Target.Row [B][COLOR="#FF0000"]>=[/COLOR][/B] 7 Then
        Select Case Target.Column
            Case Is = 7
[B][COLOR="#FF0000"]                If Range("H12").Value = "" Then
                    MsgBox ("Please enter data in cell H12.")
                    Target.ClearContents
                    Range("H12").Select
                    Application.EnableEvents = True
                    Exit Sub
                Else
[/COLOR][/B]                    fEmptyRow = Range("G7:G12").Cells.SpecialCells(xlCellTypeBlanks).Row
                    If Target.Row > fEmptyRow Then
                        MsgBox ("Please enter data in cell G" & fEmptyRow & ".")
                        Target.ClearContents
                        Range("G" & fEmptyRow).Select
                        Application.EnableEvents = True
                        Exit Sub
                    End If
 [B][COLOR="#FF0000"]               End If
[/COLOR][/B]            Case Is = 8
                fEmptyRow = Range("H7:H12").Cells.SpecialCells(xlCellTypeBlanks).Row
                If Target.Row > fEmptyRow Then
                    MsgBox ("Please enter data in cell H" & fEmptyRow & ".")
                    Target.ClearContents
                    Range("H" & fEmptyRow).Select
                    Application.EnableEvents = True
                    Exit Sub
                End If
        End Select
    End If
    Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Rick, thank you for picking that up. :)
 
Upvote 0
Code:
Select Case Target.Column
tells the macro to look at the column number of the cell that has been selected for data entry. If you select a cell in column G the column number would be 7, if column H, the column number would be 8. Then
Code:
 Case Is = 7
is the same as
Code:
If Target.Column = 7
I hope this helps.
 
Upvote 0
Thanks I've figured out while waiting for your answer! @RickRothstein Thank you for the modification!!!
 
Last edited:
Upvote 0
You are very welcome. Glad we could help. :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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