我正在尝试使用其他相关问题(Excel Macro - Rows to Comma Separated Cells (Preserve/Aggregate Column))中的宏,并且收到运行时错误1004。
尝试调试时,它说这行是问题所在: oCell.Offset(-1,1).Value = sResult
这是宏:
Sub GroupMyValues()
Dim oCell As Excel.Range
Dim sKey As String
Dim sResult As String
Set oCell = Worksheets(2).Range("A1")
While Len(oCell.Value) > 0
If oCell.Value <> sKey Then
'If first entry, no rows to be deleted
If sKey <> "" Then
oCell.Offset(-1, 1).Value = sResult
End If
sKey = oCell.Value
sResult = oCell.Offset(0, 1).Value
Set oCell = oCell.Offset(1, 0)
Else
sResult = sResult & ", " & oCell.Offset(0, 1).Value
Set oCell = oCell.Offset(1, 0)
oCell.Offset(-1, 0).EntireRow.Delete
End If
Wend
'Last iteration
**oCell.Offset(-1, 1).Value = sResult**
End Sub
答案 0 :(得分:1)
您不能在行号1上方偏移一行。