Add text to cell on condition across the workbook

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi everyone

I want to add a text 's' to column G if the value of cell in column J is 'New'. And I want to apply the vba across the entire workbook.

I have done the code below but I have no idea how to apply it to the whole workbook. Also I may have done the code wrong as there is nothing happened when I run the macro. Please could anyone help please.

Code:
Sub Add_S()
 Application.ScreenUpdating = False
 
 Dim ws As Worksheet
 Dim x As Long, LastRow As Long
 
 LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For x = LastRow To 1
    
        If Cells(x, 10).Value = "New" Then
            Cells(x, 7).Value = "s"
        End If
       
    Next x
 
 End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try:
Code:
Sub Add_S()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Dim x As Long, LastRow As Long
    For Each ws In Sheets
        LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
        For x = 1 To LastRow
            If ws.Cells(x, 10).Value = "New" Then
                ws.Cells(x, 7).Value = "s"
            End If
        Next x
    Next ws
    Application.ScreenUpdating = True
 End Sub
 
Upvote 0
I assume you need to count the rows in column J not column A

Loop through the sheets.

Code:
Sub Button1_Click()
    Dim sh As Worksheet
    Dim LstRw As Long
    Dim c As Range, Rng As Range

    For Each sh In Sheets
        With sh
            LstRw = .Cells(.Rows.Count, "J").End(xlUp).Row
            Set Rng = .Range("J1:J" & LstRw)
            For Each c In Rng.Cells
                If UCase(c) = "NEW" Then .Cells(c.Row, "G").Value = "s"
            Next c
        End With
    Next sh

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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