Add a letter to the end of a value based on values in multiple columns

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
Hello, see below, I need to add the letter "J" to the end of the value in column D, also changed the No to Yes (If J is already present, or if Yes is present, then do not modify these values)
IF these conditions are met: the word "Brevard" is in column M on the same row, the word "sanitary" is in column N, AND the words "riser" or "cone" is present in column K

Capture.PNG


How do I write the VBA code for this? Any help is appreciated !
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this:
What do you want to happen if column D does not have a J suffix and Column E = "Yes" ?
(the code below still adds the J)

VBA Code:
Sub UpdateSelectedData()

    Dim shtData As Worksheet
    Dim rng As Range
    Dim arr As Variant
    Dim rowLast As Long, i As Long
   
    Set shtData = ActiveSheet
    rowLast = shtData.Cells(Rows.Count, "D").End(xlUp).Row
    Set rng = shtData.Range("D2:N" & rowLast)               ' <--- Assuming first row of data starts at row 2 (heading on row 1)
    arr = rng                                               ' Note column D is Column 1 in the array
   
    For i = 1 To UBound(arr)
        If UCase(arr(i, 10)) = UCase("Brevard") _
            And UCase(arr(i, 11)) = UCase("Sanitary") _
            And (InStr(1, arr(i, 8), "Riser", vbTextCompare) <> 0 _
                Or InStr(1, arr(i, 8), "Cone", vbTextCompare) <> 0) Then
           
               
                If Right(arr(i, 1), 1) <> "J" Then arr(i, 1) = arr(i, 1) & "J"  ' Only add "J" suffix if not already there
                arr(i, 2) = "Yes"
        End If
    Next i
   
    ' write back column D & E values
    rng.Columns(1).Value = Application.Index(arr, 0, 1)
    rng.Columns(2).Value = Application.Index(arr, 0, 2)

End Sub

PS:
You will generally get faster & better answers if you provide sample data in a form that can easily be copied for testing:
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

20220808 VBA Update data using criteria zack8576.xlsm
ABCDEFGHIJKLMN
1Col1Col2Col3RefY/NCol6Col7Col8Col9Col10DescriptionCol12TypeProduct Group
2F145536JYesMH,4'dia,Base,8"wBrevardSanitary
3F14616JYesMH,4'dia,Riser,8"wBrevardSanitary
4F14818JYesMH,4'dia,Cone,8"wBrevardSanitary
5F03957AGRU LINER - 2mmBrevardSanitary
Data
 
Last edited:
Upvote 0
Try this:
What do you want to happen if column D does not have a J suffix and Column E = "Yes" ?
(the code below still adds the J)

VBA Code:
Sub UpdateSelectedData()

    Dim shtData As Worksheet
    Dim rng As Range
    Dim arr As Variant
    Dim rowLast As Long, i As Long
  
    Set shtData = ActiveSheet
    rowLast = shtData.Cells(Rows.Count, "D").End(xlUp).Row
    Set rng = shtData.Range("D2:N" & rowLast)               ' <--- Assuming first row of data starts at row 2 (heading on row 1)
    arr = rng                                               ' Note column D is Column 1 in the array
  
    For i = 1 To UBound(arr)
        If UCase(arr(i, 10)) = UCase("Brevard") _
            And UCase(arr(i, 11)) = UCase("Sanitary") _
            And (InStr(1, arr(i, 8), "Riser", vbTextCompare) <> 0 _
                Or InStr(1, arr(i, 8), "Cone", vbTextCompare) <> 0) Then
          
              
                If Right(arr(i, 1), 1) <> "J" Then arr(i, 1) = arr(i, 1) & "J"  ' Only add "J" suffix if not already there
                arr(i, 2) = "Yes"
        End If
    Next i
  
    ' write back column D & E values
    rng.Columns(1).Value = Application.Index(arr, 0, 1)
    rng.Columns(2).Value = Application.Index(arr, 0, 2)

End Sub

PS:
You will generally get faster & better answers if you provide sample data in a form that can easily be copied for testing:
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

20220808 VBA Update data using criteria zack8576.xlsm
ABCDEFGHIJKLMN
1Col1Col2Col3RefY/NCol6Col7Col8Col9Col10DescriptionCol12TypeProduct Group
2F145536JYesMH,4'dia,Base,8"wBrevardSanitary
3F14616JYesMH,4'dia,Riser,8"wBrevardSanitary
4F14818JYesMH,4'dia,Cone,8"wBrevardSanitary
5F03957AGRU LINER - 2mmBrevardSanitary
Data
Thank you Alex, that was super helpful !! Please allow me some time to test this out and I will let you know how it went !
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top