Tweek this VB?

jdash

New Member
Joined
Mar 14, 2011
Messages
18
I found the following here.

I just want to extract as a separate function the part that opens the print dialog box and inserts the page number into a cell.

Can it be done?

Code:
Sub CreateTableOfContents()
    ' Determine if there is already a Table of Contents
    ' Assume it is there, and if it is not, it will raise an error
    ' if the Err system variable is > 0, you know the sheet is not there
    Dim WST As Worksheet
    On Error Resume Next
    Set WST = Worksheets("TOC")
    If Not Err = 0 Then
        ' The Table of contents doesn't exist. Add it
        Set WST = Worksheets.Add(Before:=Worksheets(1))
        WST.Name = "TOC"
    End If
    On Error GoTo 0
    
    ' Set up the table of contents page
    WST.[A2] = "Table of Contents"
    With WST.[A6]
        .CurrentRegion.Clear
        .Value = "Subject"
    End With
    WST.[B6] = "Page(s)"
    WST.Range("A1:B1").ColumnWidth = Array(36, 12)
    TOCRow = 7
    PageCount = 0

    ' Do a print preview on all sheets so Excel calcs page breaks
    ' The user must manually close the PrintPreview window
    Msg = "Excel needs to do a print preview to calculate the number of pages. "
    Msg = Msg & "Please dismiss the print preview by clicking close."
    MsgBox Msg
    ActiveWindow.SelectedSheets.PrintPreview

    ' Loop through each sheet, collecting TOC information
    For Each S In Worksheets
        If S.Visible = -1 Then
            S.Select
            ThisName = ActiveSheet.Name
            HPages = ActiveSheet.HPageBreaks.Count + 1
            VPages = ActiveSheet.VPageBreaks.Count + 1
            ThisPages = HPages * VPages

            ' Enter info about this sheet on TOC
            Sheets("TOC").Select
            Range("A" & TOCRow).Value = ThisName
            Range("B" & TOCRow).NumberFormat = "@"
            If ThisPages = 1 Then
                Range("B" & TOCRow).Value = PageCount + 1 & " "
            Else
                Range("B" & TOCRow).Value = PageCount + 1 & " - " & PageCount + ThisPages
            End If
        PageCount = PageCount + ThisPages
        TOCRow = TOCRow + 1
        End If
    Next S
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
It's possible I've missed setting some variables between the two, but are you wanting something like this?

Code:
Sub CreateTableOfContents()
' Determine if there is already a Table of Contents
' Assume it is there, and if it is not, it will raise an error
' if the Err system variable is > 0, you know the sheet is not there
Dim WST As Worksheet
On Error Resume Next
Set WST = Worksheets("TOC")
If Not Err = 0 Then
    ' The Table of contents doesn't exist. Add it
    Set WST = Worksheets.Add(Before:=Worksheets(1))
    WST.Name = "TOC"
End If
On Error GoTo 0

' Set up the table of contents page
WST.[A2] = "Table of Contents"
With WST.[A6]
    .CurrentRegion.Clear
    .Value = "Subject"
End With
WST.[B6] = "Page(s)"
WST.Range("A1:B1").ColumnWidth = Array(36, 12)
TOCRow = 7
PageCount = 0

End Sub

Code:
Sub InsertPageNumbers()
Dim Msg As String, ThisName As String
Dim S As Worksheet
Dim HPages As Long, VPages As Long, ThisPages As Long
Dim PageCount As Long, TOCRow As Long

    ' Do a print preview on all sheets so Excel calcs page breaks
' The user must manually close the PrintPreview window
Msg = "Excel needs to do a print preview to calculate the number of pages. "
Msg = Msg & "Please dismiss the print preview by clicking close."
MsgBox Msg
ActiveWindow.SelectedSheets.PrintPreview

TOCRow = 7
PageCount = 0

' Loop through each sheet, collecting TOC information
For Each S In Worksheets
    If S.Visible = -1 Then
        S.Select
        ThisName = ActiveSheet.Name
        HPages = ActiveSheet.HPageBreaks.Count + 1
        VPages = ActiveSheet.VPageBreaks.Count + 1
        ThisPages = HPages * VPages

        ' Enter info about this sheet on TOC
        Sheets("TOC").Select
        Range("A" & TOCRow).Value = ThisName
        Range("B" & TOCRow).NumberFormat = "@"
        If ThisPages = 1 Then
            Range("B" & TOCRow).Value = PageCount + 1 & " "
        Else
            Range("B" & TOCRow).Value = PageCount + 1 & " - " & PageCount + ThisPages
        End If
    PageCount = PageCount + ThisPages
    TOCRow = TOCRow + 1
    End If
Next S

End Sub
 
Upvote 0
Thanks Kristy,

This helps but is it still referring to a page "TOC"? I'd like to be able to run the macro on the active sheet, have the page number entered into a cell (one that I can choose/adjust if possible) and that's really it. I'm going to use VLOOKUP on my unit identifiers to create the TOC and grab the page number....

I hope that makes sense.

Presently I'm using this, which I think is a throwback to ExcelMacro4 or something?
Code:
<table style="width: 654px; height: 274px;" border="0" cellpadding="0" cellspacing="0"><col style="width: 120pt;" width="160"> <col style="width: 317pt;" width="423"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl77" style="height: 12.75pt; width: 120pt;" height="17" width="160">[SIZE=2]RowAfterpgbrk[/SIZE]</td> <td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td class="xl78" style="width: 317pt;" width="423">[SIZE=2]=GET.DOCUMENT(64)[/SIZE]</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl77" style="height: 12.75pt;" height="17">[SIZE=2]
TotPageCount[/SIZE]</td> <td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td class="xl78" style="width: 317pt;" width="423">[SIZE=2]
=GET.DOCUMENT(50)[/SIZE]</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td> </tr> <tr style="height: 38.25pt;" height="51"> <td class="xl77" style="height: 38.25pt;" height="51">[SIZE=2]PageOfPages[/SIZE]</td> <td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td class="xl79" style="width: 317pt;" width="423">[SIZE=2]="Page   "&IF(ISNA(MATCH(ROW(),RowAfterpgbrk,1)),1,MATCH(ROW(),RowAfterpgbrk,1)+1)&"   of " & totpagecount + 0*now()[/SIZE]</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td> </tr> <tr style="height: 38.25pt;" height="51"> <td class="xl77" style="height: 38.25pt;" height="51">[SIZE=2]LastRow[/SIZE]</td> <td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td class="xl79" style="width: 317pt;" width="423">[SIZE=2]=IF(ISNA(MATCH(ROW(),RowAfterpgbrk,1)),1,MATCH(ROW(),RowAfterpgbrk,1)+1)<>IF(ISNA(MATCH(ROW()+1,RowAfterpgbrk,1)),1,MATCH(ROW()+1,RowAfterpgbrk,1)+1)[/SIZE]</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td> </tr> <tr style="height: 38.25pt;" height="51"> <td class="xl77" style="height: 38.25pt;" height="51">[SIZE=2]
FirstRow[/SIZE]</td> <td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td class="xl79" style="width: 317pt;" width="423">[SIZE=2]

=IF(ISNA(MATCH(ROW(),RowAfterpgbrk,1)),1,MATCH(ROW(),RowAfterpgbrk,1)+1)<>IF(ISNA(MATCH(ROW()-1,RowAfterpgbrk,1)),1,MATCH(ROW()-1,RowAfterpgbrk,1)+1)[/SIZE]</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td> </tr> <tr style="height: 25.5pt;" height="34"> <td class="xl77" style="height: 25.5pt;" height="34">[SIZE=2]

ThisPage[/SIZE]</td> <td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td><td class="xl79" style="width: 317pt;" width="423">[SIZE=2]


=IF(ISNA(MATCH(ROW(),RowAfterpgbrk,1)),1,MATCH(ROW(),RowAfterpgbrk,1)+1)[/SIZE]</td><td style="vertical-align: top;">
</td><td style="vertical-align: top;">
</td> </tr> </tbody></table>
Each name on the left is a defined name with the values on the right. In a cell I can enter =ThisPage for example and it will calculate the page number. The problem here is that it is slow and doesn't automatically recalculate with correct values when page breaks are changed, so I have to re-enter the formula and let it compute-- x 13,000 lines.

Anymore thoughts, please let me know. Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,912
Members
452,949
Latest member
beartooth91

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