I want the following code to also check Column 2 of the same row and it the value of the…
- User
I want the following code to also check Column 2 of the same row and it the value of the cell does not contain within the text value the word Request then it can run. If Target.Column = 10 And Target.Value <> "" Then
- Assistant
If Target.Column = 10 And Target.Value <> "" And InStr(Target.Offset(0,1).Value, "Request") = 0 Then
- User
The value in Column 2 is a two word text value. The last word is always Request. I want the code to look at value and if the word Request is not present in the value then run the rest of the code
- Assistant
You can use the
InStrRev()function to check if the word "Request" is present in the second word of the cell value. Here's how you can modify the code:If Target.Column = 10 And Target.Value <> "" Then Dim words() As String words = Split(Target.Offset(0, 1).Value, " ") If InStrRev(words(UBound(words)), "Request", , vbTextCompare) = 0 Then ' rest of your code End If End IfThis code splits the second word of the cell value into an array of words, and then checks if the last word of the array (which should be "Request") is present using
InStrRev(). ThevbTextCompareoption makes the search case-insensitive. If the word "Request" is not present, the rest of your code will execute. - User
If I open a sheet, I want a message prompt that asks if I would like to delete the contents of range A2 to E2 if I say yes it will delete the values. If I say no it will not delete the values
- Assistant
You can use the
MsgBoxfunction to display a Yes/No prompt and check the user's response. If the user clicks "Yes", you can clear the contents of the specified range. Here's an example code:Private Sub Workbook_Open() Dim answer As VbMsgBoxResult answer = MsgBox("Do you want to delete the contents of range A2:E2?", vbYesNo + vbQuestion, "Delete contents?") If answer = vbYes Then ThisWorkbook.Sheets("Sheet1").Range("A2:E2").ClearContents End If End SubThis code displays a message box when the workbook is opened asking if the user wants to delete the contents of range A2:E2. If the user clicks "Yes", the contents of the range are cleared using the
ClearContentsmethod. If the user clicks "No", nothing happens. Note that you should replace "Sheet1" with the name of your sheet. - User
I also want the code to run only if cell B2 is not blank
- Assistant
You can add an
Ifstatement to check if cell B2 is not blank before displaying the message box. Here's the modified code:Private Sub Workbook_Open() If Not IsEmpty(ThisWorkbook.Sheets("Sheet1").Range("B2")) Then 'check if B2 is not blank Dim answer As VbMsgBoxResult answer = MsgBox("Do you want to delete the contents of range A2:E2?", vbYesNo + vbQuestion, "Delete contents?") If answer = vbYes Then ThisWorkbook.Sheets("Sheet1").Range("A2:E2").ClearContents End If End If End SubThis code first checks if cell B2 is not blank using the
IsEmptyfunction. If B2 is not empty, it displays the message box and clears the specified range if the user clicks "Yes". If B2 is empty, the code does nothing. - User
If Target.Column = 10 And Target.Value <> “” Then Dim words() As String words = Split(Target.Offset(0, 1).Value, " ") If InStrRev(words(UBound(words)), “Request”, , vbTextCompare) = 0 Then ’ rest of your code End If End If The code checks if the target value in column 10 is not blank. It must also check if the value in column 1 of the same row does not contain the word Request in any part of the value. This code is not working as expected
- Assistant
To check if the value in column 1 of the same row does not contain the word "Request", you can use the
Target.Rowproperty to get the row number of the modified cell, and then check the value in column 1 of that row using theWorksheetFunctionSearch()orFind()method. Here's an updated version of the code:Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 10 And Target.Value <> "" Then Dim words() As String words = Split(Target.Offset(0, 1).Value, " ") If InStrRev(words(UBound(words)), "Request", , vbTextCompare) = 0 And _ WorksheetFunction.Search("Request", Cells(Target.Row, 1).Value, vbTextCompare) = 0 Then ' rest of your code End If End If End SubThis code checks if the modified cell is in column 10 and is not blank. It then splits the second word of the cell value into an array of words and checks if the last word (which should be "Request") is not present. Additionally, it checks if the cell in column 1 of the same row does not contain the word "Request" using the
Searchmethod with thevbTextCompareoption for case-insensitive search. If both conditions are true, the rest of your code will execute. - User
Target.Column = 10 refers to Column J. I want the Target.Offset to refer to Column B
- Assistant
You can modify the
Target.Offsetmethod to specify the row number and column number of the offset cell. Here's the modified code:Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 10 And Target.Value <> "" Then Dim words() As String words = Split(Cells(Target.Row, 2).Value, " ") ' modify here to refer to column B If InStrRev(words(UBound(words)), "Request", , vbTextCompare) = 0 And _ WorksheetFunction.Search("Request", Cells(Target.Row, 1).Value, vbTextCompare) = 0 Then ' rest of your code End If End If End SubThis code now checks if the modified cell is in column J (10) and is not blank. It then splits the second word of the cell value in column B of the same row into an array of words and checks if the last word (which should be "Request") is not present. Additionally, it checks if the cell in column A of the same row does not contain the word "Request" using the
Searchmethod with thevbTextCompareoption for case-insensitive search. If both conditions are true, the rest of your code will execute.