+ Reply to Thread
Results 1 to 4 of 4

Changing a code

Hybrid View

  1. #1
    Registered User
    Join Date
    06-09-2014
    Posts
    39

    Changing a code

    Hi,

    Awhile ago a forum member provided me with this code for extracting unique combinations of data entries across three columns.

    I was wondering if someone could help me augment it to perform the same task for four columns of data? Thanks in advance.

    Here is the code from the program, and I have also attached the original file used to provide me with the code when I just needed the problem solved for three columns.

    Sub test()
    
    Dim sh As Worksheet, target_sh As Worksheet
    Dim lrow As Long, data, result, i As Long, n As Long, j As Long, mystr As String
    
    Set sh = Sheets("Compiler")
    
    lrow = sh.Cells(Rows.Count, 2).End(xlUp).Row
    If lrow = 1 Then Exit Sub
    
    Set target_sh = Sheets("Annual Summary")
    
    data = sh.Range("b1:d" & lrow)
    
    ReDim result(1 To lrow, 1 To 3)
    
    For i = 2 To lrow
        If InStr(mystr, "|" & data(i, 1) & data(i, 2) & data(i, 3) & "|") = 0 Then
            mystr = mystr & "|" & data(i, 1) & data(i, 2) & data(i, 3) & "|"
            j = j + 1
            For n = 1 To 3
                result(j, n) = data(i, n)
            Next
        End If
    Next
    
    Application.ScreenUpdating = 0
    
    target_sh.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(j, 3) = result
    target_sh.UsedRange.EntireColumn.AutoFit
    
    Application.ScreenUpdating = 1
    
    End Sub
    Kind regards,
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Changing a code

    data = sh.Range("b1:e" & lrow)

    ReDim result(1 To lrow, 1 To 4)

    If InStr(mystr, "|" & data(i, 1) & data(i, 2) & data(i, 3) & data(i,4) & "|") = 0 Then


    basically amend the arrays to use 4 and the n count to be =1 to 4.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    06-09-2014
    Posts
    39

    Re: Changing a code

    Thank you for the reply.

    If you have a moment, why is it that I do not have to add a fourth variable?

    In the first case with 3 I had i, n & j. I figured a fourth would have to be added as well. I don't think I am quite as clear on how the internal mechanics of this program work as I thought.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Changing a code

    Sorry you will, basically everything to do with the loops/arrays and the input range being 4 columns

+ 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] Changing static code to dynamic code
    By JamesFletcher in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2013, 07:31 AM
  2. Need help changing code to use a different column
    By luke.guthrie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-18-2012, 12:39 PM
  3. changing a code
    By alpertheidiot in forum Excel General
    Replies: 1
    Last Post: 06-15-2009, 05:46 AM
  4. Changing this code
    By Neal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2006, 06:55 PM
  5. [SOLVED] Help with changing code
    By Greg B in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-13-2005, 12:06 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