+ Reply to Thread
Results 1 to 15 of 15

Looking to speed up modified countif using VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-28-2018
    Location
    New York
    MS-Off Ver
    MS Office 2010
    Posts
    184

    Looking to speed up modified countif using VBA

    Good Afternoon,

    I am looking to speed up a portion of a macro where I am basically looking to create a unique ID for duplicate values. The following formula is currently being used on a dynamic range of ~21k rows and then converted to value. Any help would be greatly appreciated. Thank you!

    Sub ColumnW_Test()
    
    'Enter formula and fill down
    Range("W4").Formula = "=V4&""-""&COUNTIF($V$4:V4,V4)"
    Range("W4:W21364").FillDown
    
    'Copy/paste values to end of data
    Range("W4", "W" & Cells(Rows.Count, 3).End(xlUp).Row).Copy
    Range("W4").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.StatusBar = False
    
    EndTime = Timer
    MsgBox "This took " & EndTime - StartTime & " seconds to run"
    
    End Sub

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,755

    Re: Looking to speed up modified countif using VBA

    How about
    Sub MakeUnique()
       Dim i As Long
       Dim Ary As Variant
       
       Ary = Range("V4", Range("V" & Rows.Count).End(xlUp).Offset(, 1)).value2
       With CreateObject("scripting.dictionary")
          For i = 1 To UBound(Ary)
             .Item(Ary(i, 1)) = .Item(Ary(i, 1)) + 1
             Ary(i, 2) = Ary(i, 1) & "-" & .Item(Ary(i, 1))
          Next i
       End With
       Range("W4").Resize(UBound(Ary)).Value = Application.Index(Ary, 0, 2)
    End Sub

  3. #3
    Forum Contributor
    Join Date
    06-28-2018
    Location
    New York
    MS-Off Ver
    MS Office 2010
    Posts
    184

    Re: Looking to speed up modified countif using VBA

    That is awesome! Works perfectly. I am new to coding so I really appreciate it. Thank you.

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,755

    Re: Looking to speed up modified countif using VBA

    Glad to help & thanks for the feedback.

    Also welcome to the board.

  5. #5
    Forum Contributor
    Join Date
    06-28-2018
    Location
    New York
    MS-Off Ver
    MS Office 2010
    Posts
    184

    Re: Looking to speed up modified countif using VBA

    Hi Fluff13,

    How are you? I hope all is well. I'm having an issue with a vba code you helped me with about 2-3 weeks ago. The code is re-pasted below.

    The original file that was used with this code was updated (meaning we cleared out the previous months data and are now using current month data). I'm getting a type mismatch 13 error on the following line: Ary(i, 2) = Ary(i, 1) & "-" & .Item(Ary(i, 1)).

    I'm not entirely familiar with scripting dictionaries and/or if there is something else that would be causing this error. I assumed I could just keep recycling/reusing the code each month as new data arrives. Any help would be greatly appreciated. Thank you.

    Sub MakeUnique()
       Dim i As Long
       Dim Ary As Variant
       
       Ary = Range("V4", Range("V" & Rows.Count).End(xlUp).Offset(, 1)).value2
       With CreateObject("scripting.dictionary")
          For i = 1 To UBound(Ary)
             .Item(Ary(i, 1)) = .Item(Ary(i, 1)) + 1
             Ary(i, 2) = Ary(i, 1) & "-" & .Item(Ary(i, 1))  'type mismatch error occurs here
          Next i
       End With
       Range("W4").Resize(UBound(Ary)).Value = Application.Index(Ary, 0, 2)
    End Sub

  6. #6
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513
    Quote Originally Posted by nickytraps View Post
    Hi Fluff13,

    How are you? I hope all is well. I'm having an issue with a vba code you helped me with about 2-3 weeks ago. The code is re-pasted below.

    The original file that was used with this code was updated (meaning we cleared out the previous months data and are now using current month data). I'm getting a type mismatch 13 error on the following line: Ary(i, 2) = Ary(i, 1) & "-" & .Item(Ary(i, 1)).

    I'm not entirely familiar with scripting dictionaries and/or if there is something else that would be causing this error. I assumed I could just keep recycling/reusing the code each month as new data arrives. Any help would be greatly appreciated. Thank you.

    Sub MakeUnique()
       Dim i As Long
       Dim Ary As Variant
       
       Ary = Range("V4", Range("V" & Rows.Count).End(xlUp).Offset(, 1)).value2
       With CreateObject("scripting.dictionary")
          For i = 1 To UBound(Ary)
             .Item(Ary(i, 1)) = .Item(Ary(i, 1)) + 1
             Ary(i, 2) = Ary(i, 1) & "-" & .Item(Ary(i, 1))  'type mismatch error occurs here
          Next i
       End With
       Range("W4").Resize(UBound(Ary)).Value = Application.Index(Ary, 0, 2)
    End Sub
    Maybe version your excel until excel 2013
    If using transpose and index Only 65.000 row
    Try fluff code

    Sub MakeUnique()
       Dim i As Long
       Dim Ary As Variant
       Dim arr()
       
       Ary = Range("V4", Range("V" & Rows.Count).End(xlUp).Offset(, 1)).value
       Redim arr(1 to ubound(Ary),1 to 1)
       With CreateObject("scripting.dictionary")
          For i = 1 To UBound(Ary)
             .Item(Ary(i, 1)) = .Item(Ary(i, 1)) + 1
             Arr(i, 1) = Ary(i, 1) & "-" & .Item(Ary(i, 1))  'type mismatch error occurs here
          Next i
       End With
       Range("W4").Resize(UBound(Ary)).Value = Arr
    End Sub

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,755

    Re: Looking to speed up modified countif using VBA

    When it fails what is the value of i?
    Add 3 to that value & then look in col V for that row number, what is in the cell?
    For instance if i is 5 what is in V8

  8. #8
    Forum Contributor
    Join Date
    06-28-2018
    Location
    New York
    MS-Off Ver
    MS Office 2010
    Posts
    184

    Re: Looking to speed up modified countif using VBA

    Quote Originally Posted by Fluff13 View Post
    When it fails what is the value of i?
    Add 3 to that value & then look in col V for that row number, what is in the cell?
    For instance if i is 5 what is in V8
    Sorry I just realized that there may be issues with the data. The first error occurs at the 7,162nd item which is #N/A. That should not be. Let me look into this further. Thank you.

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,755

    Re: Looking to speed up modified countif using VBA

    It's the #N/A that's causing the problem.

  10. #10
    Forum Contributor
    Join Date
    06-28-2018
    Location
    New York
    MS-Off Ver
    MS Office 2010
    Posts
    184

    Re: Looking to speed up modified countif using VBA

    This code works, however I am trying to reuse this code in another workbook. I thought it would be as simple as updating the range(s) but apparently not. Would anyone happen know what is causing this? Thank you!


    Option Explicit
    
    Sub Unique_Id()
    
       Dim i As Long
       Dim Ary As Variant
       
       
    
       Ary = Range("J2", Range("J" & Rows.Count).End(xlUp).Offset(, 1)).Value2
     
       With CreateObject("scripting.dictionary")
          For i = 1 To UBound(Ary)
             .Item(Ary(i, 1)) = .Item(Ary(i, 1)) + 1
           Ary(i, 2) = Ary(i, 1) & "-" & .Item(Ary(i, 1))
          Next i
       End With
       Range("K2").Resize(UBound(Ary)).Value = Application.Index(Ary, 0, 2) ' Run time error 13 Type mismatch
    
    
    End Sub

  11. #11
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,755

    Re: Looking to speed up modified countif using VBA

    How many rows of data do you have?

  12. #12
    Forum Contributor
    Join Date
    06-28-2018
    Location
    New York
    MS-Off Ver
    MS Office 2010
    Posts
    184

    Re: Looking to speed up modified countif using VBA

    Went from originally about 21k to now 130k in the new workbook.

  13. #13
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,755

    Re: Looking to speed up modified countif using VBA

    In that case try the code daboho supplied in post#9

  14. #14
    Forum Contributor
    Join Date
    06-28-2018
    Location
    New York
    MS-Off Ver
    MS Office 2010
    Posts
    184

    Re: Looking to speed up modified countif using VBA

    I think that works. Testing now. Thanks again Fluff13, you too daboho.

  15. #15
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,755

    Re: Looking to speed up modified countif using VBA

    You're welcome

+ 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. Don't Save When Workbook Wasn't Modified but Save When the Workbook is Modified
    By RXcel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-21-2018, 10:09 PM
  2. How to avoid cell SELECTION but still format cells to speed up the macro running speed
    By BeefyBerts in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-05-2018, 08:18 AM
  3. [SOLVED] Is there any way to speed up this macro.. COUNTIF
    By qokalp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-08-2015, 02:46 AM
  4. Everage Speed km/time (european speed)
    By GerryZucca in forum Excel General
    Replies: 3
    Last Post: 02-23-2015, 03:02 PM
  5. [SOLVED] Any way to speed up countif for range?
    By JP Romano in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-08-2012, 11:40 AM
  6. Last Modified
    By Steve in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-20-2006, 01:35 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