简体   繁体   中英

swapped rows vba code in excel

I'm trying to generate combinations id_users in excel that they have a relation..so I used this sample code to gave me this result:

the input is a pivot table :

     ' id_row   id_users
         10      1
                 2
                 3
         66      4
                 11

this is my output

     'source target label
       1         2     10
       1         3     10
       2         3     10
       4         11    66

But it gave me this result :

       'source target label
       1         2     10
       1         3     10
       2         1     10
       2         3     10
       3         1     10
       3         2     10
       4         11    66
       11        4     66

as u see i didn't want to show the swapped rows too it's okay if it show duplicate row..but i don't want to show the swapped data..like : 1 2 and 2 1 it doesn't mean anything just a repeated info like : a with b has a relation and then u saw that b with a has a relation ..and it's just a repeated of an info that first i have it... in my code is it's show the data array sq user_1 that not equal with the data in sqq user_2 , but also I want to show the data that are not called in pervious sq user_1 :

            '
              'get the combinationsin the pivot table for this topic
    sq = Range(rTopic, rTopic.End(xlDown).Offset(-1)).Offset(, 1).Resize(, 2).SpecialCells(2, 1).Value

    'get the unique combinations of persons
    sUniq = " "
    For lUser_2 = 1 To UBound(sq, 1)
        If InStr(sUniq, " " & sq(lUser_2, 2) & " ") = 0 Then
            sUniq = sUniq & sq(lUser_2, 2) & " "
        End If
    Next

    sqq = Split(Trim(sUniq))


    'loop over user id's and generate combinations
    For lUser_1 = 1 To UBound(sq, 1)
        For lUser_2 = 0 To UBound(sqq)

            If sq(lUser_1, 2) & "" <> sqq(lUser_2) Then

                'we found a new combination, output to screen
                Range(sStartingCellOutput).Offset(lRowOffset, lColOffset).Resize(1, 3).Value = Array(sq(lUser_1, 2), sqq(lUser_2), rTopic.Value)

                 'increment the counter
                  lRowOffset = lRowOffset + 1
                  If lRowOffset >= Rows.count Then
                  lRowOffset = 0
                  lColOffset = lColOffset + 4
                 End If
            End If



        Next
    Next

I editted it to explain more in my code..just plz i need a samll help ? if my Q is not clear just comment for me...thanks

您需要在要检查的值上添加另一个空格

If InStr(sUniq, " " & sq(lUser_2, 2) & " ") = 0 Then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM