+ Reply to Thread
Results 1 to 5 of 5

Need help adjusting VB macro for appending data to rows with matching index numbers

Hybrid View

summerela Need help adjusting VB macro... 05-15-2013, 03:58 PM
AB33 Re: Need help adjusting VB... 05-15-2013, 04:49 PM
summerela Re: Need help adjusting VB... 05-15-2013, 05:05 PM
AB33 Re: Need help adjusting VB... 05-15-2013, 06:10 PM
summerela Re: Need help adjusting VB... 05-15-2013, 06:20 PM
  1. #1
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Need help adjusting VB macro for appending data to rows with matching index numbers

    Hello-

    I have a macro that was written for me by someone awesome on this forum, however, our data has changed slightly and I cannot figure out how to adjust it.

    We use a program that outputs data onto a spreadsheet. The same mouse is in a box for five minutes, but the program creates a separate row for each minute. Instead, the macro was designed to check the mouse ID in column C, and then move all of the following matching entries into the same row, in order.

    The macro is working, but is not capturing all the data it should- there are five missing entries. I've attached the macro and a copy of the data below.

    It would be really nice if someone could explain how this works a bit in case I need to edit things in the future so I won't have to bother anyone.

    Sub FC3()
    
        
     Dim aNum, b
     Dim i As Long, ii As Long
     With ActiveWorkbook.ActiveSheet
        With .Range("a5:m" & Cells(Rows.Count, "c").End(xlUp).Row)
            aNum = .Value
            .Clear
        End With
        With CreateObject("Scripting.Dictionary")
           .CompareMode = 1
           For indx = LBound(aNum) To UBound(aNum)
               If Not .Exists(aNum(indx, 3)) Then
                   ReDim b(1 To UBound(aNum, 2))
                   For ii = LBound(aNum, 2) To UBound(aNum, 2)
                       b(ii) = aNum(indx, ii)
                   Next
                   .Item(aNum(indx, 3)) = Join(b, ",")
               Else
                   ReDim b(1 To 6)
                   For ii = 1 To 6
                       b(ii) = aNum(indx, ii + 7)
                   Next
                   .Item(aNum(indx, 3)) = .Item(aNum(indx, 3)) & "," & Join(b, ",")
               End If
           Next
           aNum = .Items
        End With
        Application.ScreenUpdating = False
        For i = LBound(aNum) To UBound(aNum)
           x = Split(aNum(i), ",")
           For ii = LBound(x) To UBound(x)
               .Cells(i + 5, ii + 1) = x(ii)
           Next
        Next
        '.Columns("N:N").EntireColumn.Insert
        Application.ScreenUpdating = True
     End With
    End Sub
    Attachment 235963

    Your help is greatly appreciated!
    Last edited by Leith Ross; 05-15-2013 at 05:00 PM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help adjusting VB macro for appending data to rows with matching index numbers

    This code was written by Mike. Infact, Jindon too provided one, so you had two codes working. I have re-run Mike's code and could not see any error, except the first 3 lines which I suspect were added by you, are not working. You also do not say(Show) which lines are the missing rows.
    I have commented the first 3 lines

    Sub Step3()
    'Set r = Range("C5:C200")
    'Set rr = r.SpecialCells(xlCellTypeBlanks)
    'rr.EntireRow.Delete
    
     Dim aNum, b
     Dim i As Long, ii As Long
     With ActiveWorkbook.ActiveSheet
        With .Range("a5:n" & Cells(Rows.Count, "c").End(xlUp).Row)
            aNum = .Value
            .Clear
        End With
        With CreateObject("Scripting.Dictionary")
           .CompareMode = 1
           For indx = LBound(aNum) To UBound(aNum)
               If Not .exists(aNum(indx, 3)) Then
                   ReDim b(1 To UBound(aNum, 2))
                   For ii = LBound(aNum, 2) To UBound(aNum, 2)
                       b(ii) = aNum(indx, ii)
                   Next
                   .Item(aNum(indx, 3)) = Join(b, ",")
               Else
                   ReDim b(1 To 7)
                   For ii = 1 To 7
                       b(ii) = aNum(indx, ii + 7)
                   Next
                   .Item(aNum(indx, 3)) = .Item(aNum(indx, 3)) & "," & Join(b, ",")
               End If
           Next
           aNum = .Items
        End With
        Application.ScreenUpdating = False
        For i = LBound(aNum) To UBound(aNum)
           x = Split(aNum(i), ",")
           For ii = LBound(x) To UBound(x)
               .Cells(i + 5, ii + 1) = x(ii)
           Next
        Next
        '.Columns("N:N").EntireColumn.Insert
        Application.ScreenUpdating = True
     End With
    End Sub
    Last edited by AB33; 05-15-2013 at 04:56 PM.

  3. #3
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need help adjusting VB macro for appending data to rows with matching index numbers

    Thank you so much, the code is working now, although it's adding blank columns between each group but that is something I can easily fix.

    I would really like to know how it works, so that if I need to make changes, I can do so without having to bother all of you. For example, right now there are seven columns of data from A to G that will be the same for each mouse- what would I need to change if I had 6 columns of data instead?

    As always, your quick response was a lifesaver!

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help adjusting VB macro for appending data to rows with matching index numbers

     If Not .exists(aNum(indx, 3)) Then
    
    ReDim b(1 To 7)
                   For ii = 1 To 7
                       b(ii) = aNum(indx, ii + 7)
    I do not know your knowledge of dictionary object.

    If Not .exists(aNum(indx, 3)) Then
    test for the existence of key for each column C rows, so it is testing column C for duplicates.
    If there are duplicates ,the code then goes in to this line
    Else section
    It then append column 8 to last column
    ReDim b(1 To 7)
                   For ii = 1 To 7
                       b(ii) = aNum(indx, ii + 7)
    I have change the above line in to

    ReDim b(8 To UBound(aNum, 2))
                   For ii = 8 To UBound(aNum, 2)
                       b(ii) = aNum(indx, ii)
    This is more flexible as you can see that if there is a duplicate, i.e. if the key exists, then it goes in to else section. In the else section, you can see column 8 to last column is included. You can change to what ever column you want.
    UBound(aNum, 2)) indicates the upper bound(The last column). You can also change this in to fixed column

  5. #5
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need help adjusting VB macro for appending data to rows with matching index numbers

    AB33, thank you very, very much! I really appreciate it!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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