+ Reply to Thread
Results 1 to 3 of 3

Code wont work in new workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-26-2008
    Location
    Ohio
    MS-Off Ver
    2016
    Posts
    326

    Code wont work in new workbook

    I have some code that works well for a Table of contents on the Workbook Index page, that chooses the heading of each sheet (C5 of each sheet), and puts the headings in a "table of contents list" on the index page (starting in C11) (see code below). It allows a table of contents to "list/show" only "un-hidden sheets".
    The problem I am having is I'm trying to put it into a new Workbook (same types of pages) but I'm not sure how to "change / edit" it to work.
    My new workbook is named "NEWEST NEW" (If it matters).
    BTY, I'm very green at this. Any Help?
    Thanks / Richard


    [Option Explicit

    '=========================================================================================
    '=========================================================================================


    Sub UpdateIndexWorksheet()

    Const sINDEX_SHEETNAME As String = "Workbook Index"
    Const sSHEETNAME_CELL As String = "C5"
    Const sHEADER_CELL As String = "C10"

    Dim rHeaderCell As Range
    Dim wksIndex As Worksheet
    Dim iRowNo As Integer
    Dim wks As Worksheet

    Set wksIndex = ThisWorkbook.Worksheets(sINDEX_SHEETNAME)

    Set rHeaderCell = wksIndex.Range(sHEADER_CELL)

    Call ClearIndexCells(wksIndex:=wksIndex, rHeaderCell:=rHeaderCell)

    iRowNo = 1

    For Each wks In ThisWorkbook.Worksheets

    If wks.Visible = xlSheetVisible Then
    rHeaderCell.Offset(iRowNo, 0).Value = wks.Range(sSHEETNAME_CELL)
    iRowNo = iRowNo + 1
    End If

    Next wks

    End Sub

    '=========================================================================================
    '=========================================================================================

    Private Sub ClearIndexCells(wksIndex As Worksheet, rHeaderCell As Range)

    Dim rFirstCell As Range
    Dim rLastCell As Range
    Dim rLastRow As Range

    Set rFirstCell = rHeaderCell.Offset(1, 0)

    With wksIndex
    Set rLastRow = .UsedRange.Rows(.UsedRange.Rows.Count)
    End With

    Set rLastCell = Intersect(rLastRow, rHeaderCell.EntireColumn)

    Range(rFirstCell, rLastCell).ClearContents

    End Sub]

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,479

    Re: Code wont work in new workbook

    Why not "save as" the old workbook with new workbook name.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,826

    Re: Code wont work in new workbook

    As far as I can see, you need something in cells C5 and C10 on each worksheet, including the workbook index sheet which needs to be called "Workbook Index".

    Works for me.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 01-27-2014, 05:06 PM
  2. Running a code at a specific hour wont work if...
    By Joaoack in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-14-2012, 03:23 PM
  3. [SOLVED] Found GREAT print VBA, but wont work on protected workbook can anyone help me with code?
    By NicksDad in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-30-2012, 02:25 PM
  4. Combo Box wont work when workbook saved as web page.
    By singerbatfink in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-23-2012, 04:30 AM
  5. Unload and placement wont work in user form code
    By vn900 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-29-2011, 06:36 AM
  6. [SOLVED] Anyone know why this wont work?
    By Mark Stephens in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2005, 02:05 AM
  7. Conditional formatting code wont work
    By Robert Hargreaves in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-05-2005, 09:40 PM
  8. [SOLVED] Why wont this code work?
    By Mark Stephens in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-30-2005, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1