Custom Table Numbering UDF Failure with CalculateFull on other Sheets

LAXSean

New Member
Joined
Jan 8, 2010
Messages
1
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.


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
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.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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