+ Reply to Thread
Results 1 to 13 of 13

TC Counter -- identify and count number of instances in a selected column.

Hybrid View

mrodrigues TC Counter -- identify and... 10-16-2012, 04:58 PM
mike7952 Re: TC Counter -- identify... 10-16-2012, 05:20 PM
mrodrigues Re: TC Counter -- identify... 10-16-2012, 05:32 PM
mike7952 Re: TC Counter -- identify... 10-16-2012, 05:44 PM
Cutter Re: TC Counter -- identify... 10-17-2012, 03:56 PM
mrodrigues Re: TC Counter -- identify... 10-18-2012, 05:11 PM
mrodrigues Re: TC Counter -- identify... 10-18-2012, 05:09 PM
mike7952 Re: TC Counter -- identify... 10-18-2012, 05:17 PM
mrodrigues Re: TC Counter -- identify... 10-18-2012, 09:58 PM
mike7952 Re: TC Counter -- identify... 10-18-2012, 10:06 PM
mike7952 Re: TC Counter -- identify... 10-18-2012, 10:16 PM
jindon Re: TC Counter -- identify... 10-19-2012, 12:54 AM
mrodrigues Re: TC Counter -- identify... 10-29-2012, 03:13 PM
  1. #1
    Registered User
    Join Date
    10-16-2012
    Location
    US
    MS-Off Ver
    Excel 1997
    Posts
    16

    Question TC Counter -- identify and count number of instances in a selected column.

    Hello,

    I am trying to creat a VBA macro to locate cells in a manually selected column that have the text value "TC" and if this value is found then the cell value will be changed to TC1, then TC2, TC3, TC4, etc... (up to a max count of 1024).

    I'm starting out small (beginner to VBA) and trying to get TC to increment across a small population of a predefined column. This is what I have so far (though each of the 66 TC values in the column are renamed TC1 and do not increment):

    Sub TC_Count()
    
    
    For N = 1 To 66
    For Count = 1 To 66
    
        If Range("C" & N).Value = "TC" Then
    
                Range("C" & N).Select
                ActiveCell.Value = ActiveCell.Text & Count
    
    
            Else
        
        End If
        
    
    Next Count
    Next N
    
    End Sub
    Any help is appreciated. Thank you.
    Last edited by Cutter; 10-17-2012 at 03:56 PM. Reason: Added code tags

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: TC Counter -- identify and count number of instances in a selected column.

    Something like this.

    Sub TC()
    Dim FoundCell As Range
    Dim LastCell As Range
    Dim FirstAddr As String
    Dim WhatColumn As String
    Dim iCount As Long
    
        If Selection.Columns.Count > 1 Then Exit Sub
        WhatColumn = Mid(ActiveCell.Address, 2, InStr(2, ActiveCell.Address, "$") - InStr(1, ActiveCell.Address, "$") - 1)
        With Range(WhatColumn & ":" & WhatColumn)
            Set LastCell = .Cells(.Cells.Count)
        End With
        Set FoundCell = Range(WhatColumn & ":" & WhatColumn).Find(What:="TC", After:=LastCell)
        
        If Not FoundCell Is Nothing Then
            FirstAddr = FoundCell.Address
        End If
        Do Until FoundCell Is Nothing
            iCount = iCount + 1
            FoundCell.Value = FoundCell.Value & iCount
            Set FoundCell = Range(WhatColumn & ":" & WhatColumn).FindNext(After:=FoundCell)
            If FoundCell.Address = FirstAddr Then
                Exit Do
            End If
        Loop
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Registered User
    Join Date
    10-16-2012
    Location
    US
    MS-Off Ver
    Excel 1997
    Posts
    16

    Re: TC Counter -- identify and count number of instances in a selected column.

    Hello Mike,

    Yes. That does seem to get the job done. I'll try and wrap my head around your solution... I'm not sure what the $ means.

    Thank you.
    Michael

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: TC Counter -- identify and count number of instances in a selected column.

    It just takes the $ sign out of cell address and drops the number, so we get the Column Letter of the activecell. Test this

    Sub ActiveCellColumnLetter()
     MsgBox ActiveCell.Address
     MsgBox Split(ActiveCell.Address, "$")(1)
    End Sub

  5. #5
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: TC Counter -- identify and count number of instances in a selected column.

    @ mrodrigues

    Welcome to the forum.

    Please notice that [CODE] tags have been added to your post(s). The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at the top of the page and read Rule #3.
    Thanks.

  6. #6
    Registered User
    Join Date
    10-16-2012
    Location
    US
    MS-Off Ver
    Excel 1997
    Posts
    16

    Re: TC Counter -- identify and count number of instances in a selected column.

    Is there a way to make the code a little more discerning so that ONLY a TC value in a cell is picked up, and not a value that contains TC? For example if there were some cells values that were TC and others that were TCS and you would want to ignore the TCS values and only apply this to TC.

  7. #7
    Registered User
    Join Date
    10-16-2012
    Location
    US
    MS-Off Ver
    Excel 1997
    Posts
    16

    Re: TC Counter -- identify and count number of instances in a selected column.

    OK. Thanks Cutter.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: TC Counter -- identify and count number of instances in a selected column.

    Try this

    Sub TC_v2()
     Dim FoundCell As Range
     Dim LastCell As Range
     Dim FirstAddr As String
     Dim WhatColumn As String
     Dim iCount As Long
    
        If Selection.Columns.Count > 1 Then Exit Sub
        WhatColumn = Split(ActiveCell.Address, "$")(1)
        With Range(WhatColumn & ":" & WhatColumn)
            Set LastCell = .Cells(.Cells.Count)
        End With
        Set FoundCell = Range(WhatColumn & ":" & WhatColumn).Find(What:="TC", After:=LastCell)
        
        If Not FoundCell Is Nothing Then
            FirstAddr = FoundCell.Address
        End If
        Do Until FoundCell Is Nothing
            If Not Left$(FoundCell.Value, 3) = "TCS" Then
                iCount = iCount + 1
                FoundCell.Value = FoundCell.Value & iCount
                Set FoundCell = Range(WhatColumn & ":" & WhatColumn).FindNext(After:=FoundCell)
            End If
            If FoundCell.Address = FirstAddr Then
                Exit Do
            End If
        Loop
    End Sub

  9. #9
    Registered User
    Join Date
    10-16-2012
    Location
    US
    MS-Off Ver
    Excel 1997
    Posts
    16

    Re: TC Counter -- identify and count number of instances in a selected column.

    This seems to take a very long time to execute -- it actually seems to lock up on me (Excel is not responding for a while now), Mike.

    Isn't there a way to qualify the:
    What:="TC"
    So only TC would be selected and anything with characters after TC would not be affected? Is there a way to do that?

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: TC Counter -- identify and count number of instances in a selected column.

    Opps I put the end if in the wrong spot try this, Put the End if above Set FoundCell

    Sub TC_v2()
     Dim FoundCell As Range
     Dim LastCell As Range
     Dim FirstAddr As String
     Dim WhatColumn As String
     Dim iCount As Long
    
        If Selection.Columns.Count > 1 Then Exit Sub
        WhatColumn = Split(ActiveCell.Address, "$")(1)
        With Range(WhatColumn & ":" & WhatColumn)
            Set LastCell = .Cells(.Cells.Count)
        End With
        Set FoundCell = Range(WhatColumn & ":" & WhatColumn).Find(What:="TC", After:=LastCell)
        
        If Not FoundCell Is Nothing Then
            FirstAddr = FoundCell.Address
        End If
        Do Until FoundCell Is Nothing
            If Not Left$(FoundCell.Value, 3) = "TCS" Then
                iCount = iCount + 1
                FoundCell.Value = FoundCell.Value & iCount
            End If
            Set FoundCell = Range(WhatColumn & ":" & WhatColumn).FindNext(After:=FoundCell)
            
            If FoundCell.Address = FirstAddr Then
                Exit Do
            End If
        Loop
    End Sub
    Last edited by mike7952; 10-18-2012 at 10:09 PM.

  11. #11
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: TC Counter -- identify and count number of instances in a selected column.

    Or even this added LookAt:=xlWhole to only look at TC

    Sub TC_v3()
     Dim FoundCell As Range
     Dim LastCell As Range
     Dim FirstAddr As String
     Dim WhatColumn As String
     Dim iCount As Long
    
        If Selection.Columns.Count > 1 Then Exit Sub
        WhatColumn = Split(ActiveCell.Address, "$")(1)
        With Range(WhatColumn & ":" & WhatColumn)
            Set LastCell = .Cells(.Cells.Count)
        End With
        Set FoundCell = Range(WhatColumn & ":" & WhatColumn).Find(What:="TC", After:=LastCell, LookAt:=xlWhole)
        
        If Not FoundCell Is Nothing Then
            FirstAddr = FoundCell.Address
        End If
        Do Until FoundCell Is Nothing
            iCount = iCount + 1
            FoundCell.Value = FoundCell.Value & iCount
            Set FoundCell = Range(WhatColumn & ":" & WhatColumn).FindNext(After:=FoundCell)
            If FoundCell.Address = FirstAddr Then
                Exit Do
            End If
        Loop
    End Sub

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: TC Counter -- identify and count number of instances in a selected column.

    This should be faster than Find method when many rows of data.
    Sub test()
        Dim rng As Range, a, i As Long, ii As Long
        With Selection(1)
            Set rng = Intersect(.EntireColumn, .Parent.UsedRange)
            If Not rng Is Nothing Then
                a = rng.Value
                For i = 1 To UBound(a, 1)
                    If a(i, 1) = "TC" Then
                        ii = ii + 1
                        a(i, 1) = "TC" & ii
                    End If
                Next
                rng.Value = a
            End If
        End With
    End Sub

  13. #13
    Registered User
    Join Date
    10-16-2012
    Location
    US
    MS-Off Ver
    Excel 1997
    Posts
    16

    Re: TC Counter -- identify and count number of instances in a selected column.

    Thank you, jindon.

    That works great.

    Thanks for all your help too, Mike.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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