将多个逗号分隔列拆分为行

时间:2016-03-28 19:01:14

标签: sql vba access-vba

请帮助。

我正在尝试按客户ID分割几个逗号分隔值,我将其用于单个列,但棘手的部分是将其应用于多个列

e.g。 来源

Cust Id | Value 1 | Value 2
1       | 3,4     | a2,a3
2       | 1,2,3   | a1,a3,a5

必须退出

Cust Id | Value 1 | Value 2
1       | 3       | a2
1       | 4       | a3
2       | 1       | a1 
2       | 2       | a3 
2       | 3       | a5 

我尝试使用一个查找表,其中包含一个从1到100填充的整数列,它应该是最大分隔值。然后我在下面的查询中使用它来分割出工作正常但只适用于一列的值

SELECT ID, TRIM(Mid(',' & Value1 & ',',[Entries]+1,Instr([Entries]+1,',' & Value1 & ',',',')-([Entries]+1))) AS Split
FROM MyTable
WHERE Entries=Instr(Entries,',' & Value1 & ',',',') And Entries<len(',' & Value1)

2 个答案:

答案 0 :(得分:0)

假设Value 1Value 2字段中的逗号计数相同,这是一个想法:

'needs reference to MS ActiveX Data Objects xxx Library
Sub SplitData()

Dim oRst As ADODB.Recordset
Dim sSQL As String, oVars1 As Variant, oVars2 As Variant
Dim i As Integer

On Error GoTo Err_SplitData

sSQL = "SELECT * FROM Customers;"

Set oRst = New ADODB.Recordset
With oRst
    'open recordset
    .Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
    'fill-in recordset
    .MoveLast
    .MoveFirst
    'loop through the records in Customers table
    Do While Not .EOF
        'split data
        oVars1 = Split(.Fields("Value1").Value, ",")
        oVars2 = Split(.Fields("Value2").Value, ",")
        'loop through the splited data
        For i = 0 To UBound(oVars1)
            'improve this piece of code
            '
            'only for example - print data
            Debug.Print .Fields("CustID"), oVars1(i), oVars2(i)

        Next
        .MoveNext
    Loop
    .Close
End With

Exit_SplitData:
    On Error Resume Next
    Set oRst = Nothing
    Exit Sub

Err_SplitData:
    MsgBox Err.Number, vbExclamation, Err.Number
    Resume Exit_SplitData

End Sub

答案 1 :(得分:0)

Maciej的答案看起来不错,但由于我已经为你做了一个答案,我会继续发布它:

Option Explicit

Sub FncCreateOutput()

    Dim rowCount As Long
    Dim custID As Long
    Dim value1Str As Variant
    Dim value2Str As Variant

    Sheets("Input").Select

    'We start at row two as this is a header line
    rowCount = 2

    Do While Range("A" & rowCount).Value <> ""

        'Reselect the Input sheet - it will have been deselected by previous calls to 'FncWriteIDValues()'
        Sheets("Input").Select

        custID = Range("A" & rowCount).Value
        value1Str = Split(Range("B" & rowCount).Value, ",")
        value2Str = Split(Range("C" & rowCount).Value, ",")

        'You need to check that you have the same count of values in the two variant arrays
        If UBound(value1Str) <> UBound(value2Str) Then

            Stop

        Else

            'They match so we can now pass this information to a function that will write to another sheet
            Call FncWriteIDValues(custID, value1Str, value2Str)

        End If
        rowCount = rowCount + 1

    Loop

End Sub

Private Function FncWriteIDValues(id As Long, val1 As Variant, val2 As Variant)

    Dim totalRows As Long
    Dim i As Integer

    Sheets("Output").Select

    'Get the total rows in the 'Ouput' sheet used so far
    totalRows = ActiveSheet.UsedRange.Rows.count

    Do While i <= UBound(val1)

        Range("A" & totalRows + 1).Value = id
        Range("B" & totalRows + 1).Value = val1(i)
        Range("c" & totalRows + 1).Value = val2(i)
        i = i + 1
        totalRows = totalRows + 1
    Loop

End Function

在Excel中创建两个工作表,一个名为“输入”,另一个名为“输出”。在“输入”表中,放置标题和数据,然后在“输出”中,只放入第1行中的标题行。代码将完成剩下的工作。