从系统表中导出的数据,呈现为相同的ID出现在多行,行出现的数量不固定。现用户要将多行变成一行,同ID对应的相应的行数据,要放到同一行中的不同列中。
原始数据如下:
ID ActTyp Form.
10001005 SXM101 ZCA501
10001005 SXL102 ZE_002
10001005 SXL101 ZE_003
10001006 SXM101 ZCA501
10001006 SXL102 ZE_002
10001006 SXL101 ZE_003
10001007 SXM102 ZCA501
10001007 SXL102 ZE_002
10001007 SXL101 ZE_003
原始数据如下:
ID ActTyp Form.
10001005 SXM101 ZCA501
10001005 SXL102 ZE_002
10001005 SXL101 ZE_003
10001006 SXM101 ZCA501
10001006 SXL102 ZE_002
10001006 SXL101 ZE_003
10001007 SXM102 ZCA501
10001007 SXL102 ZE_002
10001007 SXL101 ZE_003
希望转换成如下格式:
ID ActTyp1 Form1. ActTyp2 Form2. ActTyp3 Form3.
10001005 SXM101 ZCA501 SXL102 ZE_002 SXL101 ZE_003
10001006 SXM101 ZCA501 SXL102 ZE_002 SXL101 ZE_003
10001007 SXM102 ZCA501 SXL102 ZE_002 SXL101 ZE_003
使用如下VBA语句,即可实现此功能(未作更多严格测试)ID ActTyp1 Form1. ActTyp2 Form2. ActTyp3 Form3.
10001005 SXM101 ZCA501 SXL102 ZE_002 SXL101 ZE_003
10001006 SXM101 ZCA501 SXL102 ZE_002 SXL101 ZE_003
10001007 SXM102 ZCA501 SXL102 ZE_002 SXL101 ZE_003
Sub Chg_Row_to_col()
Dim Id, ActType, Formula As String
Dim I, J, K As Integer
J = K = 1
For I = 2 To 700 'loop start
from row 2 to 700 .
Id = Sheet1.Cells(I, 1)
ActType = Sheet1.Cells(I,
2
)Fo
r
mula = Sheet1.Cells(I,
3
)If Id = Sheet1.Cells(I - 1, 1) Then
J = J + 2
Else
J = 1
K = K + 1
End If
Sheet2.Cells(K, 1) = Id
Sheet2.Cells(K, J + 1) = ActType
Sheet2.Cells(K, J + 2) = Formula
Next
End Sub
No comments:
Post a Comment