+ Reply to Thread
Results 1 to 4 of 4

Possible feature - increment counter based on string pattern ?

  1. #1
    Registered User
    Join Date
    02-18-2011
    Location
    MY
    MS-Off Ver
    Excel 2003
    Posts
    19

    Possible feature - increment counter based on string pattern ?

    Currently, I have an application ( Add In ) that generate table of content and add a counter for a constant tag. Just feel like replacing the Add In with simple macro.

    I am sure that we can create a macro to generate table of content.

    But, not too sure whether I can create a macro to increment the counter based on a string set by user.

    For example in my document, all the string with TAGID, I need to change it to TAGID1, TAGID2 based on the last counter.

    Is there some feature in Microsoft Word that allow me to do it ? or I need to write a macro to find all words with TAGID and then strip off the number at the end, take the MAX then +1.

    Thanks for input.

  2. #2
    Registered User
    Join Date
    02-18-2011
    Location
    MY
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Possible feature - increment counter based on string pattern ?

    Sharing the simple code I have created.

    Sub InsertTag()
    Dim text1 As String
    Dim counter, maxcounter As Integer

    Dim RowNum As Long, ColNum As Long

    If Selection.Information(wdWithInTable) Then
    RowNum = Selection.Cells(1).RowIndex
    ColNum = Selection.Cells(1).ColumnIndex
    End If

    maxcounter = 0
    For li_row = 1 To 10
    text1 = Trim(Selection.Tables(1).Cell(li_row, 2).Range.Text)
    counter = Right(Trim(Left(text1, Len(text1) - 2)), 1)
    If IsNumeric(counter) Then
    If counter > maxcounter Then
    maxcounter = counter
    End If
    End If
    Next

    maxcounter = maxcounter + 1

    Selection.Tables(1).Cell(RowNum, ColNum).Range.Text = "CSID" & maxcounter



    End Sub

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Possible feature - increment counter based on string pattern ?

    The simplest way to update the counter is to store it in a custom document property that can be referenced throughout your document via DOCPROPERTY fields. That way, you don't need to find the strings in the document itself.

    If you've used Word's heading Style correctly, creating a Table of Contents quite possibly requires nothing more complicated than adding a TOC field to the document.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    Registered User
    Join Date
    02-18-2011
    Location
    MY
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Possible feature - increment counter based on string pattern ?

    Hi Macropod, Good idea. I will explore the DocProperty to store my latest counter.

+ 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. Increment a counter by 1 for every 4 cells the same.
    By sravilob in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-05-2013, 09:22 AM
  2. Calcuations based on string pattern
    By goku4 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-15-2013, 03:45 AM
  3. Macro to increment a counter
    By tleee in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-16-2012, 07:53 PM
  4. Count data to a limit and increment counter
    By Ricker090 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-14-2012, 09:46 AM
  5. [SOLVED] Increment a counter in Excel
    By COSdl in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-14-2005, 11:33 AM

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