I am trying to add specific text to the beginning of a cell in column U based on the value of the corresponding cell in column BU.
If Column BU contains the text "TAIL LIFT REQUIRED" then add "TL&PT " to the beginning of the corresponding cell in column U.
So If cell BU4 contains the text "TAIL LIFT REQUIRED", then I want cell U4 to start with "TL&PT ". For example, cell U4 starts off saying "open 8am-4pm" and if cell BU4 = "TAIL LIFT REQUIRED", cell U4 changes to "TL&PT open 8am-4pm". If any other cell in column BU does not include the searched for text then no change should be made.
I found the following vba, which finds the text in column BU and inputs the required text in the next column over (BT), but I need this to go to column U and be added to the front of any existing text.
Sub XPO_Amend()
Dim ce As Range, lastrow As Long
lastrow = Range("BU" & Rows.Count).End(xlUp).Row
For Each ce In Range("BU2:BU" & lastrow)
Select Case ce.Value
Case Is = "TAIL LIFT REQUIRED"
ce.Offset(, 1).Value = "TL&PT"
End Select
Next ce
End Sub
Note that this was co-opted from code being asked to check multiple ranges and perform different changes, so may not be the most efficient way of doing this.
A Quick Fix
Sub XPO_Amend_QF()
Dim cell As Range, LastRow As Long
LastRow = Range("BU" & Rows.Count).End(xlUp).Row
For Each cell In Range("BU2:BU" & LastRow).Cells
Select Case cell.Value
Case "TAIL LIFT REQUIRED"
With cell.EntireRow.Columns("U")
.Value = "TL&PT " & .Value
End With
End Select
Next cell
End Sub
An Improvement
Sub XPO_Amend()
' Define constants.
Const SOURCE_FIRST_CELL_ADDRESS As String = "BU2"
Const SOURCE_STRING As String = "TAIL LIFT REQUIRED"
Const TARGET_COLUMN As String = "U"
Const TARGET_PREFIX As String = "TL&PT "
Const MATCH_CASE As Boolean = True
' Reference the worksheet.
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
' Declare variables.
Dim srg As Range, trg As Range, RowsCount As Long
' Reference the (single-column) ranges.
With ws.Range(SOURCE_FIRST_CELL_ADDRESS)
RowsCount = ws.Cells(ws.Rows.Count, .Column).End(xlUp).Row - .Row + 1
If RowsCount < 1 Then
MsgBox "No data found in ""'" & ws.Name & "'!" _
& .Resize(ws.Rows.Count - .Row + 1).Address(0, 0) & """!", _
vbExclamation
Exit Sub
End If
Set srg = .Resize(RowsCount)
Set trg = srg.EntireRow.Columns(TARGET_COLUMN)
End With
' Declare variables.
Dim sData() As Variant, tData As Variant
' Return the values from the ranges in arrays.
If RowsCount = 1 Then
ReDim sData(1 To 1, 1 To 1): sData(1, 1) = srg.Value
ReDim tData(1 To 1, 1 To 1): tData(1, 1) = trg.Value
Else
sData = srg.Value
tData = trg.Value
End If
' Determine the compare method according to the 'MATCH_CASE' constant.
Dim CompareMethod As VbCompareMethod: CompareMethod = _
IIf(MATCH_CASE, vbBinaryCompare, vbTextCompare)
' Declare variables.
Dim sValue As Variant, tValue As Variant, Row As Long
Dim IsSourceMatching As Boolean, IsTargetValid As Boolean
Dim WasAmended As Boolean
' Loop through the rows of the arrays and apply the required logic.
For Row = 1 To RowsCount
IsSourceMatching = False
IsTargetValid = False
sValue = sData(Row, 1)
If VarType(sValue) = vbString Then ' is a string
If StrComp(sValue, SOURCE_STRING, CompareMethod) = 0 Then ' is equal
IsSourceMatching = True
'Else ' is not equal to 'SOURCE_STRING'
End If
End If
If IsSourceMatching Then
tValue = tData(Row, 1)
If Not IsError(tValue) Then ' is no error...
If Len(tValue) > 0 Then ' is not blank..., doesn't begin with...
If InStr(1, tValue, TARGET_PREFIX, CompareMethod) <> 1 Then
IsTargetValid = True
'Else ' begins with 'TARGET_PREFIX'; previously amended
End If
End If
End If
End If
If IsTargetValid Then
tData(Row, 1) = TARGET_PREFIX & tValue
WasAmended = True
End If
Next Row
' Overwrite the values in the target column
' with the (amended) values from the target array.
If WasAmended Then
With trg
.Value = tData
'.EntireColumn.AutoFit ' adjust width?
End With
End If
' Inform the user.
If WasAmended Then
MsgBox "Column """ & TARGET_COLUMN & """ on sheet """ & ws.Name _
& """ amended.", vbInformation
Else
MsgBox "Column """ & TARGET_COLUMN & """ on sheet """ & ws.Name _
& """ was not amended!", vbExclamation
End If
End Sub