I need a VBA code that will serch column 'B' from row 5 downwards for all the cells that…
- User
I need a VBA code that will serch column 'B' from row 5 downwards for all the cells that contain the text value 'Service'. For each 'Service' value found, it will also check in column 'H' of the same row, the date value. After the search is complete, the earliest date found in column 'H' in the rows that the value 'Service' exists in column 'B', must then be displayed in cell 'H3'. Is this possible?
- Assistant
Yes, it is possible. Below is a VBA code that achieves the described functionality:
Sub FindEarliestDate() Dim ws As Worksheet Dim lastRow As Long Dim rng As Range Dim cell As Range Dim earliestDate As Date ' Set the worksheet to work with Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with your actual sheet name ' Find the last row in column B lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Loop through all the cells in column B from row 5 downwards Set rng = ws.Range("B5:B" & lastRow) For Each cell In rng If cell.Value = "Service" Then ' Check if the date in column H is earlier than the current earliest date If earliestDate = 0 Or cell.Offset(0, 6).Value < earliestDate Then earliestDate = cell.Offset(0, 6).Value End If End If Next cell ' Display the earliest date in cell H3 ws.Range("H3").Value = earliestDate End SubYou need to replace "Sheet1" with the actual name of your sheet. You can also change the cell range to match your data if it starts from a different row.
- User
Please change Sheet1 to Active sheet
- Assistant
Certainly! Here's the modified code that uses the Active Sheet instead:
Sub FindEarliestDate() Dim ws As Worksheet Dim lastRow As Long Dim rng As Range Dim cell As Range Dim earliestDate As Date ' Set the worksheet to the active sheet Set ws = ActiveSheet ' Find the last row in column B lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Loop through all the cells in column B from row 5 downwards Set rng = ws.Range("B5:B" & lastRow) For Each cell In rng If cell.Value = "Service" Then ' Check if the date in column H is earlier than the current earliest date If earliestDate = 0 Or cell.Offset(0, 6).Value < earliestDate Then earliestDate = cell.Offset(0, 6).Value End If End If Next cell ' Display the earliest date in cell H3 ws.Range("H3").Value = earliestDate End SubThis code will use the currently active sheet when executing the search and updating the earliest date in cell H3.
- User
I need a VBA code that will do the following. If the text value of any cell in column 'B' is changed to 'Serviced' it will pop up a message indicating, "Please ensure that an identical Service is created with a Next Service Due Date after changing this to Serviced"
- Assistant
Here's a VBA code that will display a message box if the value in column B is changed to "Serviced". The message will remind the user to ensure that an identical service with a next service due date is created:
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range Dim cell As Range ' Define the range to check for changes Set rng = Range("B:B") ' Check if the changed cells are in the defined range If Not Intersect(Target, rng) Is Nothing Then Application.EnableEvents = False ' Disable event handling ' Loop through each changed cell For Each cell In Intersect(Target, rng) If cell.Value = "Serviced" Then MsgBox "Please ensure that an identical Service is created with a Next Service Due Date after changing this to Serviced", vbExclamation, "Reminder" End If Next cell Application.EnableEvents = True ' Enable event handling End If End SubTo implement this code:
- Right-click on the sheet name in the Excel window and select "View Code".
- Paste the code into the code window that appears.
- Close the VBA editor.