VBA to loop through cells in a row and perform an action when value is found

justme101

Board Regular
Joined
Nov 18, 2017
Messages
67
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a file in the following format where a rows has some headers, let's say Name, Age and Gender. Now every time the code comes across the word "Gender", it fills up a certain formula in the 11 rows below the sub-header "1":

2232.jpg


I have the following code, but doesn't work at all:

VBA Code:
Sub Macro4()
 
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
 
ThisWorkbook.Worksheets("CSD Trend").Activate

Dim x As Integer

For Each Row In Range("M12:SJ12")
        
'Update formulas
  If ActiveCell.Value = "Station" Then
  ActiveCell.Offset(2, 0).Select
 
  Range(Selection, Selection.Offset(10, 0)).FormulaR1C1 = "formula here"
        
End If
      Next



Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True


          
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Change your code loop to the below

VBA Code:
Dim x As Integer
Range("M12").Select

For Each Column In Range("M12:SJ12")


        
'Update formulas
  If ActiveCell.Value = "Station" Then
  ActiveCell.Offset(2, 0).Select
 
  Range(Selection, Selection.Offset(10, 0)).FormulaR1C1 = "formula here"
  ActiveCell.Offset(-2, 0).Select
  ActiveCell.Offset(0, 1).Select
  Else
  ActiveCell.Offset(0, 1).Select
        
End If
      Next
 
Upvote 0
How about
VBA Code:
Sub Macro4()
 
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
 
ThisWorkbook.Worksheets("CSD Trend").Activate

Dim Cl As Range

For Each Cl In Range("M12:SJ12")
        
'Update formulas
  If Cl.Value = "Station" Then
   Cl.Offset(2, 0).Resize(11).FormulaR1C1 = "formula here"
  End If
Next Cl



Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True


          
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,741
Members
449,050
Latest member
excelknuckles

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