MAcro to calculate : (F6=(C6+E6)/2

Chaturvedi, Santosh 450 Reputation points
2026-10-05T08:42:04.4466667+00:00

I have req.

  1. I have one front sheet name "DB Collectives". This sheet have liste of hyperlinked sheet written from cell C4 to its end.
  2. The macro shall go to each sheets name mentioned in "DB Collectives" in step 1
  3. Then, It should enter new column after E of each exiting listed sheet name as mentioned in step 1.
  4. In new column of each sheets: Cell F6 should calculate (F6=(C6+E6)/2. This shall continue till the values in E ends
  5. Please generate teh macro.
Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments

Answer accepted by question author and recommended by moderator

AI answer

2026-10-05T08:42:23.94+00:00

Use a VBA macro that reads the sheet names listed on DB Collectives starting at C4, opens each matching worksheet, inserts a new column at F (immediately after E), writes the formula in F6, and fills it down to the last used row in column E.

Sub InsertFormulaInListedSheets()
    Dim wsList As Worksheet
    Dim ws As Worksheet
    Dim lastRowList As Long
    Dim cell As Range
    Dim sheetName As String
    Dim lastDataRow As Long
    
    Set wsList = ThisWorkbook.Worksheets("DB Collectives")
    lastRowList = wsList.Cells(wsList.Rows.Count, "C").End(xlUp).Row
    
    For Each cell In wsList.Range("C4:C" & lastRowList)
        sheetName = Trim(cell.Value)
        
        If sheetName <> "" Then
            On Error Resume Next
            Set ws = ThisWorkbook.Worksheets(sheetName)
            On Error GoTo 0
            
            If Not ws Is Nothing Then
                ' Insert a new column at F, which is after column E
                ws.Columns("F").Insert Shift:=xlToRight
                
                ' Find the last used row in column E
                lastDataRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
                
                ' Start only if there is data from row 6 downward
                If lastDataRow >= 6 Then
                    ws.Range("F6").Formula = "=(C6+E6)/2"
                    ws.Range("F6").AutoFill Destination:=ws.Range("F6:F" & lastDataRow)
                End If
            End If
            
            Set ws = Nothing
        End If
    Next cell
End Sub

Notes:

  • This inserts a new F column every time the macro runs. If run again, another new column will be added.
  • The formula is filled from F6 down to the last used row in E.
  • Sheet names are taken from the text in DB Collectives!C4:C....

To add the macro in Excel:

  1. Show the Developer tab.
  2. Open Developer > Macros or the Visual Basic Editor.
  3. Paste the code into a standard module.
  4. Run InsertFormulaInListedSheets.

AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Chaturvedi, Santosh 450 Reputation points
    2026-10-05T08:48:08.6933333+00:00

    Thisis working fine

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.