VBA Apply Formula if Certain Criteria is Met

rsmeyerson

Board Regular
Joined
Nov 29, 2014
Messages
104
Hello. I'm looking for some help. I would like to apply a formula to a specific sheet, column "R" if a specific name appears in column "B". I have tried the code below, but it is inserting the formula into column "R" for all rows, regardless of name.

Code:
With Sheets("RetailBack")      
     Dim a1 As Long, lstrw As Long
      lstrw = .Range("A" & Rows.Count).End(xlUp).Row
      For a1 = 2 To lstrw
           If .Range("B" & a1).Value = "NAME1" Or .Range("B" & a1).Value = "NAME2" Then
               .Range("R2:R" & .Cells(.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 = "XX"
           End If

Your help is appreciated.
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Change:

Code:
.Range("R2:R" & .Cells(.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 = "XX"

to

Code:
.Range("R" & a1).FormulaR1C1 = "XX"
 
Upvote 0
Hello Rmeyerson,

See if the following modification works for you (untested):-

Code:
With Sheets("RetailBack")
     Dim a1 As Long, lstrw As Long
     lstrw = .Range("A" & Rows.Count).End(xlUp).Row
     
      For a1 = 2 To lstrw
           If .Range("B" & a1).Value = "NAME1" Or .Range("B" & a1).Value = "NAME2" Then
               .Range("B" & a1).Offset(, 16).FormulaR1C1 = "XX"
                   End If
               Next
       End With

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Thanks for your reply. My formula is only being placed in cell "R2" even though the specified name is present in cells other than "B2".
 
Upvote 0
Try this:

Code:
With Sheets("RetailBack")
Dim a1 As Long, lstrw As Long
      lstrw = .Range("[COLOR=#ff0000]B[/COLOR]" & Rows.Count).End(xlUp).Row
      For a1 = 2 To lstrw
           If .Range("B" & a1).Value = "NAME1" Or .Range("B" & a1).Value = "NAME2" Then
[COLOR=#ff0000]           .Range("R" & a1).FormulaR1C1 = "XX"[/COLOR]
           End If
Next
End With
 
Last edited:
Upvote 0
Hello RSMeyerson,

Perhaps changing the method may be a better idea:-


Code:
Sub Test()

Dim ws As Worksheet: Set ws = Sheets("RetailBack")
 
Application.ScreenUpdating = False

With ws.Range("B1:B" & ws.Range("B" & ws.Rows.Count).End(xlUp).Row - 1)
        .AutoFilter 1, "NAME1", xlOr, "NAME2"
        .Offset(1, 16).Formula = "XX"
        .AutoFilter
End With

Application.ScreenUpdating = True

End Sub

Cheerio,
vcoolio.
 
Upvote 0
You're welcome RSM.
Good to know that it now works for you and I'm glad to have been able to help out.

Cheerio,
vcoolio.
 
Upvote 0
Can you please help with adding a 2nd filter to this AutoFilter formula? In addition to filtering column "B", I would also like to filter column "E" with criteria "NEW".
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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