If Target.Offset(0, -7).Value = "Service" Then I would like to copy the Target Value to…
- User
If Target.Offset(0, -7).Value = "Service" Then I would like to copy the Target Value to copy the target value to Target.Offset(0, 7) before the Message Pops up. Can you write the most efficient way to do this. The code I would like to add this to is below: If IsDate(Target.Value) Then If Target.Offset(0, -7).Value = "Service" Then MsgBox "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as number of days.", vbInformation, "SERVICE SCHEDULE"
- Assistant
Here is the most efficient way to modify your code to copy the value and display the message:
If IsDate(Target.Value) Then If Target.Offset(0, -7).Value = "Service" Then Target.Offset(0, 7).Value = Target.Value MsgBox "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as number of days.", vbInformation, "SERVICE SCHEDULE" End If End IfThis code checks if the value in
Targetis a valid date. If it is, it then checks if the value inTarget.Offset(0, -7)is "Service". If both conditions are true, it copies the value fromTargettoTarget.Offset(0, 7)and displays the message. - User
I would like to have a VBA code that checks the last entry of column K and then moving upwards, if column L on the same row is empty the value in column L on the same row is cleared. This action must then stop if the value in column L of the same row is not empty.
- Assistant
You can use the following VBA code to achieve what you described:
Sub ClearEmptyValues() Dim lastRow As Long Dim i As Long ' Get the last row in column K with data lastRow = Cells(Rows.Count, "K").End(xlUp).Row ' Loop through the rows starting from the last row and moving upwards For i = lastRow To 1 Step -1 ' Check if column L on the same row is empty If Cells(i, "L").Value = "" Then ' Clear the value in column L on the same row Cells(i, "L").ClearContents Else ' Stop the loop if column L on the same row is not empty Exit For End If Next i End SubThis code finds the last row with data in column K. It then loops through the rows starting from the last row and moving upwards. If column L on the same row is empty, it clears the value in column L. The loop stops if column L on the same row is not empty.
- User
I would like this code to run for the active sheet. How can I add this conditon to the code
Public Sub TidyColumnK() Dim lastRow As Long Dim i As Long lastRow = Cells(Rows.Count, "K").End(xlUp).Row For i = lastRow To 1 Step -1 If Cells(i, "J").Value = "" Then Cells(i, "K").ClearContents Else Exit For End If Next i End Sub
- Assistant
To make the code run for the active sheet, you can add the
ActiveSheetobject before the cell references. Here's the modified code:Public Sub TidyColumnK() Dim lastRow As Long Dim i As Long lastRow = ActiveSheet.Cells(Rows.Count, "K").End(xlUp).Row For i = lastRow To 1 Step -1 If ActiveSheet.Cells(i, "J").Value = "" Then ActiveSheet.Cells(i, "K").ClearContents Else Exit For End If Next i End SubWith this modification, the code will only affect the cells in column K and J of the active sheet.
- User
Can this block of code be written to be more effective: Public Sub TidyColumnK() Dim lastRow As Long lastRow = ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Row If lastRow > 1 Then Set cellAbove = ActiveSheet.Cells(lastRow - 1, "K") cellAbove.Copy ActiveSheet.Range("K" & lastRow).PasteSpecial xlPasteValues ActiveSheet.Range("A" & lastRow).Select End If Application.CutCopyMode = False End Sub
- Assistant
Yes, the code can be made more efficient by using the
.Valueproperty instead of copy-pasting the value. Here's the modified version of the code:Public Sub TidyColumnK() Dim lastRow As Long lastRow = ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Row If lastRow > 1 Then ActiveSheet.Cells(lastRow, "K").Value = ActiveSheet.Cells(lastRow - 1, "K").Value ActiveSheet.Cells(lastRow, "A").Select End If Application.CutCopyMode = False End SubIn this code, instead of copying the value with
.Copyand.PasteSpecial, it directly assigns the value from the cell above using the.Valueproperty. It then selects cell A in the last row. The usage ofApplication.CutCopyMode = Falseis to clear the clipboard after the copy operation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.