OK, this is the code to add the Tables and Formulae:
' Module: mAddTables
' dynamic_ranges_and_sheets_question TMS v3.xlsm
' author: TMShucks Excel Aid
' title: Dynamic Ranges from Different Sheets Question
' thread: http://www.excelforum.com/excel-general/991616-dynamic-ranges-from-different-sheets-question.html
' from: nicoan
Option Private Module
Option Explicit
Sub sAddTables()
Dim sh As Worksheet
Dim lLR As Long
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
On Error GoTo lblFormula
' convert ranges to Tables on Symbol sheets
' loop through each sheet in the workbook sheets collection
For Each sh In ThisWorkbook.Worksheets
' applies to everything except Control sheet
If sh.Name <> "Control" Then
With sh
' determine last row of data on the sheet
lLR = .Range("A" & .Rows.Count).End(xlUp).Row
' insert a Table
' and name it based on the worksheet name (Symbol)
.ListObjects.Add( _
xlSrcRange, _
.Range("$A$1:$F$" & lLR), , _
xlYes) _
.Name = "tab" & sh.Name
End With
End If
Next 'sh
lblFormula:
On Error GoTo 0
On Error GoTo lblExit
' add formulas to Control sheet
With Sheets("Control")
' having named the Tables using the sheet (Symbol) name
' we can use INDIRECT to reference the Symbol
.Range("Tabla1[All Time Max Price]").FormulaR1C1 = _
"=MAX(INDIRECT(""tab"" & Tabla1[[#This Row],[Symbol]] & ""[Close]""))"
.Range("Tabla1[1Y Max Price]").FormulaR1C1 = _
"=MAX(INDEX(INDIRECT(""tab"" & Tabla1[[#This Row],[Symbol]] & ""[Close]"")," & Chr(10) & " MATCH(MAX(INDIRECT(""tab""& Tabla1[[#This Row],[Symbol]]& ""[Date]"")),INDIRECT(""tab"" & Tabla1[[#This Row],[Symbol]] & ""[Date]""),0)-251):" & Chr(10) & " INDEX(INDIRECT(""tab"" & Tabla1[[#This Row],[Symbol]] & ""[Close]"")," & Chr(10) & " MATCH(MAX(INDIRECT(""tab"" & Tabla1[[#This Row],[Symbol]] & ""[Date]"")),INDIRECT(""tab"" & Tabla1[[#This Row],[Symbol]] & ""[Date]""),0)))"
' just add some formatting to the data for consistency
.Range("Tabla1[[All Time Max Price]:[1Y Max Price]]").NumberFormat = "0.00"
End With
lblExit:
On Error GoTo 0
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Please see the attached updated workbook to try it. I don't know how it will perform with 400 sheets ...
Regards, TMS
Bookmarks