How can I cahnge this VBA code to do the following: When the row B:CP is identified,…
- User
How can I cahnge this VBA code to do the following: When the row B:CP is identified, write 'x' to to column CV on the same row, have the answerMT pop up, and when the row is cleared, also clear the value in column CV of the same row
Dim answerMT As VbMsgBoxResult Dim rowToClearMT As Range Dim cellMT As Range ' Check if the selected cell is in the range B28:B34 If Not Intersect(Target, Me.Range("A41:A47")) Is Nothing Then ' Prompt the user for confirmation answerMT = MsgBox("Do you want to clear all cell values?", vbYesNo + vbQuestion, "Clear Cells") If answerMT = vbYes Then ' Identify the row position Set rowToClearMT = Me.Rows(Target.row).Columns("B:CP") ' Iterate through each cell in the range For Each cellMT In rowToClearMT.Cells cellMT.ClearContents Next cellMT End If End IfEnd Sub
- Assistant
To modify your existing VBA code to meet the specified requirements, you'll need to add the logic to write 'x' to Column CV when a specific range (B:CP) is identified, prompt the user with a message box, and clear the value in Column CV when the row is cleared.
Here's the updated code:
Dim answerMT As VbMsgBoxResult Dim rowToClearMT As Range Dim cellMT As Range Dim targetRow As Long ' Check if the selected cell is in the range A41:A47 If Not Intersect(Target, Me.Range("A41:A47")) Is Nothing Then ' Prompt the user for confirmation answerMT = MsgBox("Do you want to clear all cell values?", vbYesNo + vbQuestion, "Clear Cells") If answerMT = vbYes Then ' Identify the row position targetRow = Target.Row Set rowToClearMT = Me.Rows(targetRow).Columns("B:CP") ' Write 'x' to column CV of the same row before clearing contents Me.Cells(targetRow, "CV").Value = "x" ' Iterate through each cell in the range and clear contents For Each cellMT In rowToClearMT.Cells cellMT.ClearContents Next cellMT ' Clear the value in column CV if the row is cleared Me.Cells(targetRow, "CV").ClearContents End If End If - User
This is how I want it to work. Can you please check and correct the code: Dim answerMT As VbMsgBoxResult Dim rowToClearMT As Range Dim cellMT As Range Dim targetRow As Long
' Check if the selected cell is in the range A41:A47 If Not Intersect(Target, Me.Range("A41:A47")) Is Nothing Then ' Identify the row position targetRow = Target.row Set rowToClearMT = Me.Rows(targetRow).Columns("B:CP") ' Write 'x' to column CV of the same row before clearing contents Me.Cells(targetRow, "CV").Value = "x" ' Prompt the user for confirmation answerMT = MsgBox("Do you want to clear all cell values?", vbYesNo + vbQuestion, "Clear Cells")
If answerMT = vbNo Then ' Clear the value in column CV if No Me.Cells(targetRow, "CV").ClearContents Exit Sub End If If answerMT = vbYes Then ' Iterate through each cell in the range and clear contents For Each cellMT In rowToClearMT.Cells cellMT.ClearContents Next cellMT ' Clear the value in column CV if the row is cleared Me.Cells(targetRow, "CV").ClearContents End IfEnd If
- Assistant
- User
- Assistant
- User
Please can I have a VBA code that will do the following:
Pop up a Yes No Message "You are about to run NEW MONTH UPDATE." line break "Have you run the MONTHLY TRANSFER ?" line break "If you have, TRANSFER TABLE will still" line break "contain previous MAIN TABLE Data." line break "Check all your leave data especially" line break "HOLCARD TABLE and the TRANSFER TABLE." line break "REVERSE TRANSFER can still be run" line break "only before the NEW MONTH UPDATE" line break "Yes to proceed, No to abort & check", vb Quetion, "NEW MONTH UPDATE"
No to Exit Sub Yes to continue with rest of the code
Check if the range B2:CP8 contains any of the following values a, e, m, n, x. If any of the specific values are present in the range, pop up a message: "MONTHLY TRANSFER has not been performed" line space "NEW MONTH UPDATE cannot execute unless" line space "the Main Table is Transfered & Cleared", vbCritical, "MAIN TABLE UPDATE" the exit the sub.
- Assistant
- User
- Assistant