Consulting

Results 1 to 7 of 7

Thread: Macro to replace text with incremental number

  1. #1

    Macro to replace text with incremental number

    Hi all,

    I have a report which lists a number of company details. Some of these companies are not matched in the system, and instead of showing the company name in col. C, the report just displays "Not Listed".

    As I use the report for a number of lookups, I'd need to change each instance of the "Not Listed" text by a unique number, which would allow me to obtain the unique reference to help me with my lookup.

    For instance, say the first instance of this is in C2. "Not Listed" would become "COMPANY001".
    Then in the next instance, displayed say in C15, "Not Listed" would become "COMPANY002".
    And so on for each instance of "Not Listed" until the last row.

    The number of "Not Listed" entries varies in number and rows from week to week, but is always in col. C.

    Is there a simple way to do this in VBA?

    Thanks for any advice

    -Isa

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub NotListed()
    Dim c As Range
    Dim FA As String
    Dim i As Long
    With Columns(3)
    Set c = .Find(What:="Not Listed", LookIn:=xlFormulas)
            If Not c Is Nothing Then
                FA = c.Address
                   Do
                   i = i + 1
                   c.Formula = "COMPANY" & Format(i, "000")
                    Set c = .FindNext(c)
                Loop While Not c Is Nothing
            End If
            End With
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test()
        Dim c As Range
        Dim n As Long
        
        With Range("C1", Range("C1").End(xlDown))
            .AutoFilter
            .AutoFilter 1, "Not Listed"
            For Each c In .SpecialCells(xlCellTypeVisible)
                If n > 0 Then
                    c.Value = "COMPANY" & Format(n, "000")
                End If
                n = n + 1
            Next
            .AutoFilter
        End With
    
    End Sub

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Mana
    Why the two Autofilters?
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Probably, but I always use two autofilters just in case.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Thank you both.

Posting Permissions

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