0

在 Excel 中,我有 2 列数据。一个是值,另一个是分号分隔的键字符串列表。我需要打开第二列并将值与单个键关联为单个键值对。

输入 :Value1 "key1;key2"

输出

Value1 Key1 
Value1 Key2
4

2 回答 2

1

试试下面的示例代码

Sub sample()
    Dim strValue As String

      s = 2
    For i = 2 To Range("A" & Rows.Count).End(xlUp).Row

        strValue = Range("B" & i).Value
        retval = Split(strValue, ";")

        For j = LBound(retval) To UBound(retval)

            Range("C" & s) = Range("A" & i).Value
            Range("D" & s) = retval(j)
            s = s + 1
        Next
    Next
End Sub

在此处输入图像描述

于 2013-05-06T19:13:54.397 回答
0

这会将更新/拆分列表放在新工作表上。

Sub SplitKeyVals()    
Dim wsMe As Worksheet: Set wsMe = ActiveSheet
Dim wsNew As Worksheet
Dim rngKeys As Range
Dim cl As Range
Dim keyVals() As String
Dim k As Variant
Dim r As Long: r = 1
Dim myDelimiter as String

Set wsNew = Worksheets.Add(Before:=ActiveSheet) '## Modify as needed ##'
Set rngKeys = wsMe.Range("A2:A10") '## Modify as needed ##'

'## We will use this delimiter value later ##'
myDelimiter = ";"

'## Iterate over each cell in rngKeys.Cells ##'
For Each cl In rngKeys.Cells
    '## Use the Split function to split a delimited string in to
    ' an array, so we can iterate over the values. '
    keyVals = Split(cl.Offset(0, 1).Value, myDelimiter)

    '## Now that the values are in array, iterate over the
    ' array items: '
    For Each k In keyVals
        '## Do something with this information ##'
        wsNew.Cells(r, 1) = cl.Value
        wsNew.Cells(r, 2) = k
        '## Increase the destination row for output ##'
        r = r + 1
    Next
Next

End Sub
于 2013-05-06T19:15:24.870 回答