I am trying to develop a dynamic table numbering UDF that essentially knows what table number it's on based on how many times the UDF is used above it on the same worksheet and before it on previous worksheets.
But that's not all. I want the UDF to take a range as its first argument and then pass the range's address to an array, which will later be used to create range names for each of the ranges. (This is another topic that doesn't need to be addressed but helps explain the code below. )
I have been able to do this for the most part but when I do a Ctrl-Alt-F9 on different sheets, the numbering gets screwed up. Below is my code. The maxtbl array is used to track the highest table number on previous sheets so it can be added to table number of subsequent sheets. Otherwise the function would restart at "Table 1" for each sheet.
The subroutine "show_array" is used to calculate each of the cells using the UDF from first sheet to last sheet, top to bottom so the array is populated in the right order. Obviously, Excel has its own optimal calculation order. The sub also spits out the array onto a worksheet just so I can see it, hence the name.
So again, the problem is that when I do a CalculateFull on different sheets, the numbering gets screwed up even on the first sheet. Any help would be much appreciated.
But that's not all. I want the UDF to take a range as its first argument and then pass the range's address to an array, which will later be used to create range names for each of the ranges. (This is another topic that doesn't need to be addressed but helps explain the code below. )
I have been able to do this for the most part but when I do a Ctrl-Alt-F9 on different sheets, the numbering gets screwed up. Below is my code. The maxtbl array is used to track the highest table number on previous sheets so it can be added to table number of subsequent sheets. Otherwise the function would restart at "Table 1" for each sheet.
Code:
Dim arr() As Variant, maxtbl() As Variant
Sub print_initialize()
Dim shts As Integer, n As Integer
shts = ActiveWorkbook.sheets.count
ReDim arr(1 To 100, 1 To shts)
ReDim maxtbl(1 To shts)
For n = 1 To shts
maxtbl(n) = 1
Next n
End Sub
Function tbl(rng As Range, Optional rptrows As Long, Optional rptcols As String)
Dim sht As Integer, addr As String, lastrow As Long, count As Integer, i As Integer
sht = ActiveSheet.Index
count = 0
For i = 1 To rng.Row
If Left(Cells(i, 1).Formula, 4) = "=tbl" Then
count = count + 1
End If
Next i
arr(count, sht) = rng.address
If sht = 1 Then
tbl = "Table " & count
maxtbl(sht) = Application.Max(maxtbl(sht), count)
Else
tbl = "Table " & count + maxtbl(sht - 1)
maxtbl(sht) = Application.Max(maxtbl(sht), (count + maxtbl(sht - 1)))
End If
End Function
Sub see_array()
Dim arrange As Range, ws As Worksheet, i As Long, lastrow As Long
'shts = ActiveWorkbook.sheets.count
ReDim arr(1 To 100, 1 To shts)
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
lastrow = Range("a1048576").End(xlUp).Row
'lastrow = ActiveSheet.UsedRange.Rows.count
For i = 1 To lastrow
If Left(Cells(i, 1).Formula, 4) = "=tbl" Then
Cells(i, 1).Calculate
End If
Next i
Next ws
sheets("Summary").Activate
Set arrange = Range(Cells(1, 1), Cells(100, shts))
arrange.Value = arr
End Sub
So again, the problem is that when I do a CalculateFull on different sheets, the numbering gets screwed up even on the first sheet. Any help would be much appreciated.