需要VB脚本进行excel数据转换

问题描述 投票:-3回答:1

源数据:

Order# Qty UOM
Ord1    1   CS
Odr1    2   CS
Ord2    3   EA
Ord2    7   CS

在excel中预期的转换是:

Order# Qty1(CS) Qty2(EA) 
Ord1     1       0
Ord1     2       0
Ord2     0       3
Ord2     7       0

基本上根据Cases(CS)和Eaches(EA)等计量单位转换订单项需要您的VB脚本专业知识。

excel vba transformation
1个回答
0
投票

您可以尝试这个。我假设所有源数据都在A到C列中,数据是连续的(在A到C列中一直没有空白行,并且目的地在E到G单元格中。)>

Sub transform()
   dim r as long 'declare r as long incase there are more rows than an integer can handle
   dim tempArr(2) as variant 'declare array to hold the transformed data

   For r = 2 to WorksheetFunction.CountA([A:A]) 'Perform a loop from 2 to the end of the continuous dataset
      tempArr(0) = cells(r,1).Value2 'Store the name of Order number in the first position of the temp array
      If Cells(r,3) = "CS" Then 'If the order is CS store the value from the 2nd column in the second position in the temp array and a 0 in the 3rd position
         tempArr(1) = Cells(r,2)
         tempArr(2) = 0
      Else 'Else the order is not CS store the value from the 2nd column in the 3rd position in the temp array and a 0 in the second
         tempArr(1) = 0
         tempArr(2) = Cells(r,2)
      End If
      cells(r,5) = tempArr 'Put the array values in the new data set in column E
   next r
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.