我有一个由SQL查询创建的表,其中每个结果都合并到一个单元格中。我想在不同的行上正确关联每个值。
目前数据设置如下:
+-------+---------+-------------------------------------------+---------------+ | ID | Desc | Users | Functions | +-------+---------+-------------------------------------------+---------------+ | a | a desc | First Last [uname3], First Last [uname45] | abc, def, xyz | +-------+---------+-------------------------------------------+---------------+ | b | b desc | First Last [uname8], First Last [uname72] | lmn, def, xyz | +-------+---------+-------------------------------------------+---------------+
我希望将它呈现为:
+-------+---------+----------------------+---------------+ | ID | Desc | Users | Functions | +-------+---------+----------------------+---------------+ | a | a desc | First Last[uname3] | abc, def, xyz | +-------+---------+----------------------+---------------+ | a | a desc | First Last [uname45] | abc, def, xyz | +-------+---------+----------------------+---------------+ | b | b desc | First Last[uname8] | lmn, def, xyz | +-------+---------+----------------------+---------------+ | b | b desc | First Last [uname72] | lmn, def, xyz | +-------+---------+----------------------+---------------+
我会手动执行此操作,但在同一单元格中列出了多达125个用户的~75行。
感谢您的帮助!
答案 0 :(得分:1)
假设您的数据存储在名为A
的工作表的D
到TheNameOfSheet
列中:
Dim i As Integer, j As Integer
Dim users() As String
Dim wsh As Worksheet
Set wsh = ThisWorkbook.Worksheets("TheNameOfSheet")
i = 1 'starting row
Do
'get the list of users and split it by comma
users = Split(wsh.Range("C" & i), ", ")
'go through the list of users
For j = LBound(users()) To UBound(users())
'insert new row
If j>0 Then wsh.Range("A" & i).EntireRow.Insert(Shift:=xlShiftDown)
'copy original data
wsh.Range("A" & i) = wsh.Range("A" & i)
wsh.Range("B" & i) = wsh.Range("B" & i)
'insert single name
wsh.Range("C" & i) = users(j)
wsh.Range("D" & i) = wsh.Range("D" & i)
'increase counter
i = i + 1
Next j
i = i +1
Loop
有关详细信息,请参阅:
MS Excel: How to use the SPLIT Function (VBA)
Range.Insert Method (Excel)
注意:我没有测试过这段代码!我是直接从我的头上写的;)
答案 1 :(得分:1)