根据标准确定范围内的唯一值

时间:2015-11-26 08:51:24

标签: excel vba excel-vba

我想根据每行中多列中的值来识别唯一流。行和列都是动态的。我的数据如下:

Record1,Record2,Record3,Record 4,Record 5
A,B,C,D,E
A,B,C,F,G
A,B,C,F,I
A,B,J,K,H
A,M
X,Y,Z
X,Y,A

我必须根据这些值导出流程图。所以Flowdiagram会是这样的。 例如A,B,C是常见的,因此不会重复,C将有2个孩子,D和F,F将有两个孩子,G和I:

A-> B -> C -> D -> E
           -> F -> G
                -> I
      -> J -> K -> H
  -> M
X -> Y -> Z
       -> A

1 个答案:

答案 0 :(得分:0)

Sub CreateFlowChart()

Application.Worksheets("Record_sorting").Select
Dim arrTwoD()
Dim intRows
Dim intCols
Dim ShapeText
Dim ShapeType As MsoAutoShapeType
ShapeType = msoShapeFlowchartProcess

Dim i As Integer
Dim t, k, l As Integer
Dim shp As Shape
t = 0
k = 0
l = 0
intRows = ActiveSheet.UsedRange.Rows.Count
intCols = ActiveSheet.UsedRange.Columns.Count

Application.Worksheets("FlowChart").Select
If ActiveSheet.Shapes.Count > 2 Then
    DeleteAllAutoShapes
End If
ReDim Preserve arrTwoD(1 To intRows, 1 To intCols)

For i = 2 To UBound(arrTwoD, 1)
    t = 12
    k = k + l + 72
    l = 72
    For j = 1 To UBound(arrTwoD, 2)
        arrTwoD(i, j) = ActiveSheet.Cells(i, j)
        Application.Worksheets("Record_sorting").Select
        ShapeText = ActiveSheet.Cells(i, j)
        If ShapeText <> "" Then

            Application.Worksheets("FlowChart").Select
            Set m_Worksheet = ActiveSheet
            Randomize

            Set shp = ActiveSheet.Shapes.AddShape(ShapeType, k, t, 130, l)

            AddFormattedTextToShape shp, (ShapeText)

            If CInt(Application.Version) >= 12 Then
                If i = 25 Then
                     ' Treat as line
                    shp.ShapeStyle = msoLineStylePreset1
                Else
                     ' Randomly select a style
                    shp.ShapeStyle = Int(Rnd() * 42 + 1)
                 End If
             End If
             t = t + 85
        End If
    Next
Next

Application.Worksheets("FlowChart").Select

End Sub

Public Sub AddFormattedTextToShape(oShape As Shape,sText As String)     如果Len(sText)&gt; 0然后         使用oShape.TextFrame             .Characters.Text = sText             .Characters.Font.Name =“Calibri(Body)”             .Characters.Font.Size = 9             .Horizo​​ntalAlignment = xlHAlignCenter             .VerticalAlignment = xlVAlignCenter         结束     万一 结束子