繁体   English   中英

在Excel中交换行VBA代码

[英]swapped rows vba code in excel

我试图在excel中生成具有关系的id_users组合。.所以我使用以下示例代码给了我这个结果:

输入是数据透视表:

     ' id_row   id_users
         10      1
                 2
                 3
         66      4
                 11

这是我的输出

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

但这给了我这个结果:

       '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

如你所见,我也不想显示交换的行,如果它显示重复的行也可以..但是我不想显示交换的数据..如:1 2和2 1这并不意味着什么重复的信息,例如:a与b具有关系,然后您看到b与a具有关系..并且它只是信息的重复,首先我拥有它...在我的代码中是显示数据数组sq user_1与sqq user_2中的数据不相等,但我也想显示以前的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

我对其进行了编辑以解释代码中的更多内容。.请问我需要帮助吗? 如果我的Q不清楚,请为我评论...谢谢

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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