79209543

Date: 2024-11-21 03:06:58
Score: 1.5
Natty:
Report link

How about a simple loop on column A where we check if the string starts with any of those, and if so, put N and add the rest back on?

Sub Replace_NX_NC_NL()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim originalText As String
    Dim modifiedText As String

    ' Set the worksheet to work on
    Set ws = ThisWorkbook.Sheets(1) ' Change the index or name as necessary
    
    ' Find the last row in column A
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' Loop through each cell in column A
    For i = 1 To lastRow
        originalText = ws.Cells(i, "A").Value
        modifiedText = originalText
        
        ' Check if the string starts with NL, NC, or NX
        If Left(originalText, 2) = "NL" Or Left(originalText, 2) = "NC" Or Left(originalText, 2) = "NX" Then
            ' Replace the first occurrence of NL, NC, or NX with N
            modifiedText = "N" & Mid(originalText, 3)
        End If
        
        ' Paste the modified text into column C
        ws.Cells(i, "C").Value = modifiedText
    Next i

End Sub
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Starts with a question (0.5): How
  • Low reputation (0.5):
Posted by: sillycone