VBA to Add Data to Existing Data in a Cell

ssmith3156

New Member
Joined
Apr 11, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I need a VBA code that will add the letter "S" to the existing data in column G (Invoice) IF the value in column B (Phase) is "8" or "9". I appreciate any help offered.

1684243046744.png
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try this:
VBA Code:
Sub InsertCode()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Loop through all rows starting in row 2
    For r = 2 To lr
'       Check to see if column B is 8 or 9
        If Cells(r, "B") = 8 Or Cells(r, "B") = 9 Then
'           Add "S" to end of value in column G
            Cells(r, "G") = Cells(r, "G") & "S"
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 1
Solution
Dim lr As Long Dim r As Long Application.ScreenUpdating = False ' Find last row in column B with data lr = Cells(Rows.Count, "B").End(xlUp).Row ' Loop through all rows starting in row 2 For r = 2 To lr ' Check to see if column B is 8 or 9 If Cells(r, "B") = 8 Or Cells(r, "B") = 9 Then ' Add "S" to end of value in column G Cells(r, "G") = Cells(r, "G") & "S" End If Next r Application.ScreenUpdating = True End Sub
That worked perfectly! Thank you so much!
 
Upvote 0
Try this on a copy of your data.

VBA Code:
Public Sub subAddAnS()
Dim rng As Range

    ActiveWorkbook.Save
    
    For Each rng In ActiveSheet.Range("A1").CurrentRegion.Columns(7).Cells
        If rng.Offset(0, -5).Value = 8 Or rng.Offset(0, -5).Value = 9 Then
            rng.Value = rng.Value & "S"
        End If
    Next rng

End Sub
 
Upvote 0
Try this on a copy of your data.

VBA Code:
Public Sub subAddAnS()
Dim rng As Range

    ActiveWorkbook.Save
   
    For Each rng In ActiveSheet.Range("A1").CurrentRegion.Columns(7).Cells
        If rng.Offset(0, -5).Value = 8 Or rng.Offset(0, -5).Value = 9 Then
            rng.Value = rng.Value & "S"
        End If
    Next rng

End Sub
Somebody else already gave me a working code. But, thank you!
 
Upvote 0

Forum statistics

Threads
1,223,996
Messages
6,175,851
Members
452,675
Latest member
duongtruc1610

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