【发布时间】:2021-03-08 16:11:05
【问题描述】:
更新 - 底部的最终解决方案
我正在尝试制作这样的数据:
| ID | H Column | I Column |
|---|---|---|
| 1 | bob, joe | tree, apple, dog |
| 2 | tim, tom, tum | cat |
看起来像这样:
| ID | H Column | I Column |
|---|---|---|
| 1 | bob | tree |
| 1 | joe | apple |
| 1 | dog | |
| 2 | tim | cat |
| 2 | tom | |
| 2 | tum |
我确信我的代码有多个问题,但现在我专注于尝试让值在它所说的地方正确迭代
if allcount
但我感谢任何帮助!这是我的代码:
Public Function CountChrInString(Expression As String, Character As String) As Long
''
Dim iResult As Long
Dim sParts() As String
sParts = Split(Expression, Character)
iResult = UBound(sParts, 1)
If (iResult = -1) Then
iResult = 0
End If
CountChrInString = iResult
End Function
Function LastPosition(rCell As Range, rChar As String)
'This function gives the last position of the specified character
Dim rLen As Integer
rLen = Len(rCell)
For i = rLen To 1 Step -1
If Mid(rCell, i - 1, 1) = rChar Then
LastPosition = i - 1
Exit Function
End If
Next i
End Function
Sub Splt()
Dim LR As Long, LC As Long, r As Range
Application.ScreenUpdating = False
LR = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column
Set r = Range("H" & LR)
Set q = Range("I" & LR)
Do While r.Row > 1
rcount = CountChrInString(r.Value, ",")
qcount = CountChrInString(q.Value, ",")
allcount = Application.WorksheetFunction.Max(rcount, qcount)
For i = 1 To allcount
'v = r.Value
'pos = InStr(v, ",")
r.EntireRow.Copy
r.Offset(i).EntireRow.Insert
MsgBox (r)
If allcount > 0 Then
r.Value = Right(r, LastPosition(r, ","))
v = Left(r, LastPosition(r, ",") - 1)
End If
Next
Set r = r.Offset(-1)
Loop
Application.ScreenUpdating = True
End Sub
最终解决方案 - 感谢 SJR 的帮助:
Sub RunIt()
Dim r As Long, ws As Worksheet, wsOrig As Worksheet, vH, vI, vJ, vK, vL, i As Long, j As Long, n As Long
Set wsOrig = Sheets("Paste_Here")
Set ws = Worksheets.Add
For r = 2 To wsOrig.Cells(Rows.Count, "A").End(xlUp).Row 'may need to change sheet reference
On Error Resume Next
vH = Split(wsOrig.Cells(r, "H"), ",")
vI = Split(wsOrig.Cells(r, "I"), ",")
vJ = Split(wsOrig.Cells(r, "J"), ",")
vK = Split(wsOrig.Cells(r, "K"), ",")
vL = Split(wsOrig.Cells(r, "L"), ",")
counth = UBound(vH)
counti = UBound(vI)
countj = UBound(vJ)
countk = UBound(vK)
countl = UBound(vL)
allcount = Application.WorksheetFunction.Max(counth, counti, countj, countk, countl)
n = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1 'whatever column has ID
ws.Cells(n, "A").Resize(allcount + 1).Value = wsOrig.Cells(r, "A").Value
ws.Cells(n, "B").Resize(allcount + 1).Value = wsOrig.Cells(r, "B").Value
ws.Cells(n, "C").Resize(allcount + 1).Value = wsOrig.Cells(r, "C").Value
ws.Cells(n, "D").Resize(allcount + 1).Value = wsOrig.Cells(r, "D").Value
ws.Cells(n, "E").Resize(allcount + 1).Value = wsOrig.Cells(r, "E").Value
ws.Cells(n, "F").Resize(allcount + 1).Value = wsOrig.Cells(r, "F").Value
ws.Cells(n, "G").Resize(allcount + 1).Value = wsOrig.Cells(r, "G").Value
ws.Cells(n, "M").Resize(allcount + 1).Value = wsOrig.Cells(r, "M").Value
ws.Cells(n, "N").Resize(allcount + 1).Value = wsOrig.Cells(r, "N").Value
ws.Cells(n, "H").Resize(UBound(vH) + 1).Value = Application.Transpose(vH)
ws.Cells(n, "I").Resize(UBound(vI) + 1).Value = Application.Transpose(vI)
ws.Cells(n, "J").Resize(UBound(vJ) + 1).Value = Application.Transpose(vJ)
ws.Cells(n, "K").Resize(UBound(vK) + 1).Value = Application.Transpose(vK)
ws.Cells(n, "L").Resize(UBound(vL) + 1).Value = Application.Transpose(vL)
On Error GoTo 0
Next r
End Sub
【问题讨论】:
-
您的问题具体是什么?在任何情况下,只看这个而不设置你的数据和代码来调试它,allcount 总是 > 0 或者如果 rcount 和 qcount = 0,你可能不应该启动循环,因为那样你会循环 For i = 1 to 0。如果没有 Step -1(即 For i = 1 to 0 Step -1),当 rcount 和 qcount 为 0 时,该循环将永远不会执行。
-
有些行的 allcount 将为零 - 其中 I 列和 H 列只有一个值。在这种情况下,我不需要触发“For”,因为没有数据可以爆发。我当前的问题是如果 allcount > 0。我正在尝试做一些事情,比如在最后一个逗号之后取值并将其放在行中,然后从“v”中删除该值,然后在下一行取下一个最后一个值逗号,直到你用完值。如果这更容易的话,我也可以先到最后。谢谢!