VBA Help for Beginner

SharonVobs

New Member
Joined
Apr 17, 2018
Messages
5
Hi

I am new here and to VBA and am looking to make some cells in my spreadsheet mandatory if another cell is completed,

If column B is complete then there must be something in C,E,F and this would be for rows 1:17

Thanks in advance

Sharon
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Welcome to the board.

When should the code check to see if the cells are filled? Before saving/closing workbook? And will Column B always have something in it from 1 to 17? Can it just be 1 to 5, 10, etc?
 
Upvote 0
The code should check when saving\closing the workbook and column B will not always be complete it could be just 1-5, etc
 
Upvote 0
Ok, with that being said, I imagine you want a message box or warning to popup telling the user they missed some data entry and not save/close until the columns have all the data?
 
Upvote 0
Place these 2 macros in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macros into the empty window that opens up. Close the window to return to your sheet. Save the workbook as a macro-enabled file.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim rng As Range, rng2 As Range
    For Each rng In Sheets("Sheet1").Range("B1:B17")
        If rng <> "" Then
            If rng.Offset(0, 1) = "" Then
                MsgBox ("Cell " & rng.Offset(0, 1).Address(0, 0) & " has no data.  Please complete.")
                rng.Offset(0, 1).Select
                Cancel = True
                Exit Sub
            End If
            For Each rng2 In Range("E" & rng.Row & ":F" & rng.Row)
                If rng2 = "" Then
                    MsgBox ("Cell " & rng2.Address(0, 0) & " has no data.  Please complete.")
                    rng2.Select
                    Cancel = True
                    Exit Sub
                End If
            Next rng2
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim rng As Range, rng2 As Range
    For Each rng In Sheets("Sheet1").Range("B1:B17")
        If rng <> "" Then
            If rng.Offset(0, 1) = "" Then
                MsgBox ("Cell " & rng.Offset(0, 1).Address(0, 0) & " has no data.  Please complete.")
                rng.Offset(0, 1).Select
                Cancel = True
                Exit Sub
            End If
            For Each rng2 In Range("E" & rng.Row & ":F" & rng.Row)
                If rng2 = "" Then
                    MsgBox ("Cell " & rng2.Address(0, 0) & " has no data.  Please complete.")
                    rng2.Select
                    Cancel = True
                    Exit Sub
                End If
            Next rng2
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Since mumps has provided you a solution, I will leave you with that.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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