In Column A2 to A30, values are entered but not always the entire range. However for the…
- User
In Column A2 to A30, values are entered but not always the entire range. However for the values entered in column A, each row of A, B and C must have associated values. If there is no value in a row column A, there should not be values in Column B and in column C of that row. I would like to write a vba code that checks if all the entries made in column A2 to A30 have values in the same row of column B and Column C and if not, notify the user that 'All enteries need to be completed'. The preceeding code should therefore not run unless all parameters are true.
- Assistant
Here's a possible VBA code that checks if all the entries in column A have corresponding values in the same row of columns B and C, and displays a message if any incomplete entries are detected:
Sub CheckEntries() Dim lastRow As Long lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'get the last row of data in column A Dim i As Long For i = 2 To lastRow 'start from row 2 assuming there's a header in row 1 If IsEmpty(Range("A" & i)) Then 'check if column A is empty If Not IsEmpty(Range("B" & i)) Or Not IsEmpty(Range("C" & i)) Then MsgBox "Incomplete entry in row " & i Exit Sub 'stop the code if there are incomplete entries End If Else 'if column A is not empty If IsEmpty(Range("B" & i)) Or IsEmpty(Range("C" & i)) Then MsgBox "Incomplete entry in row " & i Exit Sub 'stop the code if there are incomplete entries End If End If Next i 'if all entries are complete, continue with the rest of the code here End Sub - User
I would like to change this code to pop up a yes no message instead of just a message: If Target.CountLarge > 1 Then Exit Sub If Target.Address = "1" And Target.Value = "SUMMARY SAVED" Then MsgBox ("Tasks have already been Allocated to all PMs would you like to change PM entries") Exit Sub. If the answer is Yes then it will call a another code 'Change Entries' if No it will exit the Sub
- Assistant
Here's the modified code that will show a message box with a Yes/No prompt when the condition is met, and call a different subroutine based on the user's response:
Private Sub Worksheet_Change(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub If Target.Address = "$E$1" And Target.Value = "SUMMARY SAVED" Then Dim response As VbMsgBoxResult response = MsgBox("Tasks have already been Allocated to all PMs. Do you want to change PM entries?", vbQuestion + vbYesNo) If response = vbYes Then Call ChangeEntries 'replace with the name of your other subroutine Else Exit Sub End If End If End SubNote that this code needs to be placed in the specific worksheet module (right-click the worksheet tab, choose "View Code", and paste the code there). The
Worksheet_Changeevent will trigger the code whenever a cell is changed in the worksheet, and if the condition is met (cell E1 has the text "SUMMARY SAVED"), it will show the message box and call eitherChangeEntriesor exit the subroutine based on the user's response. - User
How can I refence Active Sheet in this code: ThisWorkbook.Worksheets("Prem PM").Range("C1").Calculate
- Assistant
You can reference the active sheet in VBA by using the
ActiveSheetobject. Therefore, you can modify your code like this:ActiveSheet.Range("C1").CalculateThis will apply the
Calculatemethod to cell C1 of the currently active worksheet. If you want to ensure that the calculation is performed on the "Prem PM" sheet regardless of which sheet is currently active, you can use:ThisWorkbook.Worksheets("Prem PM").Range("C1").CalculateThis explicitly refers to the "Prem PM" worksheet in the workbook, even if a different sheet is active.