Microsoft 365 and Office | Excel | For business | Windows
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have req.
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
AI answer
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:
To add the macro in Excel:
InsertFormulaInListedSheets.Thisis working fine