+ Reply to Thread
Results 1 to 7 of 7

Simplify the VBA Code

  1. #1
    Registered User
    Join Date
    01-29-2022
    Location
    INDIA
    MS-Off Ver
    OFFICE 365
    Posts
    5

    Simplify the VBA Code

    Hello Everyone, I have been assigned the work of Inventory management of a nearby super shoppe where I have to work on around 18000 SKU's for which the first job I have to do is to remove duplication of these 18000 line items which can be due to spelling error or such other mistakes. I have written the code but this code is taking around 20 minutes to run on 18000 line items. I want this code to be fast and if possible simpler.

    Eg.
    DOVE CREAM BAR 100G
    DOVE CREAM BAR 100GM

    DOVE PINK BAR 50G
    DOVE PINK BAR 50GM

    PARLE 20-20 GOLD CASHEW ALMOND COOKIES 600 GM
    PARLE 20-20 GOLD CASHEW ALOMND COOKIES 600 GM

    All these are duplicates

    Private Sub Worksheet()

    Dim i As Long, j As Long, k As Long
    Dim count As Long
    Dim item As String, temp1 As String, temp2 As String
    Dim compare As String
    Dim Len_Item As Long
    Dim Len_Compare As Long
    Dim x As Long
    Dim R As Integer, G As Integer, B As Integer

    R = 10
    G = 40
    'B = 140
    For i = 2 To Rows.count
    Cells(i, 8).Interior.ColorIndex = 6
    'Cells(i, 8).Interior.Color = RGB(50, 255, 0)
    Next i
    x = 3
    For i = 2 To Rows.count

    If (R < 255) Then
    G = G + 15
    End If

    'B = B + 5

    If (G > 255) Then
    R = R + 10
    G = 40
    End If

    'count = 0
    If (Cells(i, 8).Interior.ColorIndex = 6) Then GoTo Line2 Else i = i + 1


    Line2: x = x + 1
    temp1 = Cells(i, 8).Value
    item = UCase(temp1)

    Len_Item = Len(item)

    For j = i + 1 To Rows.count - 1
    count = 0
    temp2 = Cells(j, 8).Value
    compare = UCase(temp2)

    Len_Compare = Len(compare)

    For k = 1 To Len_Item
    If (Mid(item, k, 1) = Mid(compare, k, 1)) Then
    count = count + 1
    End If
    Next k

    If (count >= 0.75 * Len_Item) Then
    If (x < 56) Then
    Cells(i, 8).Interior.ColorIndex = x
    Cells(j, 8).Interior.ColorIndex = x
    'Cells(i, 8).Interior.ColorIndex = RGB(0, 0, 10)
    Else
    Cells(i, 8).Interior.Color = RGB(R, G, 0)
    Cells(j, 8).Interior.Color = RGB(R, G, 0)
    End If
    End If
    Next j
    Next i
    End Sub

  2. #2
    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,425

    Re: Simplify the VBA Code

    When you refer to Rows.count, that is ALL THE ROWS ON THE SHEET. That is, over one million rows, not 18,000. You need to limit the counter to the actual rows of data.
    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


  3. #3
    Registered User
    Join Date
    01-29-2022
    Location
    INDIA
    MS-Off Ver
    OFFICE 365
    Posts
    5

    Re: Simplify the VBA Code

    Oh! Thank you for the suggestion.But still the code is very slow and excel is going in not-responding mode

  4. #4
    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,425

    Re: Simplify the VBA Code

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file.

  5. #5
    Registered User
    Join Date
    01-29-2022
    Location
    INDIA
    MS-Off Ver
    OFFICE 365
    Posts
    5

    Re: Simplify the VBA Code

    Please find attached some of the manually selected duplicates
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-29-2022
    Location
    INDIA
    MS-Off Ver
    OFFICE 365
    Posts
    5
    Quote Originally Posted by HITESH TAKRANI View Post
    Please find attached some of the manually selected duplicates
    I have re-written the code please tell me if anything else can be done apart from this to optimize further and make it fast.




    Private Sub Worksheet()

    Dim i As Long, j As Long, k As Long
    Dim count As Long
    Dim item As String, temp1 As String, temp2 As String
    Dim compare As String
    Dim Len_Item As Long
    Dim Len_Compare As Long
    Dim x As Long
    Dim R As Integer, G As Integer, B As Integer

    R = 10
    G = 40
    'B = 140
    For i = 2 To 500
    Cells(i, 8).Interior.ColorIndex = 3
    'Cells(i, 8).Interior.Color = RGB(50, 255, 0)
    Next i
    x = 3
    For i = 2 To 500

    If (R < 255) Then
    G = G + 15
    End If

    'B = B + 5

    If (G > 255) Then
    R = R + 10
    G = 40
    End If

    'count = 0
    If (Cells(i, 8).Interior.ColorIndex = 3) Then GoTo Line2 Else GoTo Line5
    Line5: i = i + 1
    If (Cells(i, 8).Interior.ColorIndex = 3) Then GoTo Line2 Else GoTo Line5
    Line2: x = x + 1
    temp1 = Cells(i, 8).Value
    item = UCase(temp1)

    Len_Item = Len(item)

    For j = i + 1 To 500
    If (Cells(j, 8).Interior.ColorIndex = 3) Then GoTo Line3 Else GoTo Line4
    Line4: j = j + 1
    If (Cells(j, 8).Interior.ColorIndex = 3) Then GoTo Line3 Else GoTo Line4
    Line3: count = 0
    temp2 = Cells(j, 8).Value
    compare = UCase(temp2)
    Len_Compare = Len(compare)

    Line3: For k = 1 To Len_Item

    If (Mid(item, k, 1) = Mid(compare, k, 1)) Then
    count = count + 1
    End If
    Next k

    If (count >= 0.75 * Len_Item) Then
    If (x < 56) Then
    Cells(i, 8).Interior.ColorIndex = x
    Cells(j, 8).Interior.ColorIndex = x
    'Cells(i, 8).Interior.ColorIndex = RGB(0, 0, 10)
    Else
    Cells(i, 8).Interior.Color = RGB(R, G, 0)
    Cells(j, 8).Interior.Color = RGB(R, G, 0)
    End If
    End If
    Next j
    Next i
    End Sub

  7. #7
    Registered User
    Join Date
    01-29-2022
    Location
    INDIA
    MS-Off Ver
    OFFICE 365
    Posts
    5

    Re: Simplify the VBA Code

    Is there anything else required TMS?

+ 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. [SOLVED] Can I simplify this code?
    By pbexcel in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-23-2019, 03:55 AM
  2. [SOLVED] Simple VBA code to convert text to number (simplify/fix current code)
    By kenenthpaul0401 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-26-2018, 10:47 AM
  3. [SOLVED] Simplify VBA Code
    By Sky188 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-28-2012, 02:31 PM
  4. Simplify my Code
    By wrathastorm in forum Excel - New Users/Basics
    Replies: 6
    Last Post: 04-26-2012, 02:23 PM
  5. hi can anyone simplify this old bit of code
    By khalid79m in forum Excel General
    Replies: 3
    Last Post: 12-28-2006, 01:04 PM
  6. Simplify this code
    By Scott in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-08-2006, 12:00 AM
  7. [SOLVED] simplify code
    By matt in forum Excel General
    Replies: 3
    Last Post: 09-28-2005, 07:05 PM

Tags for this Thread

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