如果你设置你的输入范围到一个数组,过程,它应该是一个相对简单的锻炼。下面的代码是全面的评论,但如果它没有意义,让我知道。
Option Explicit
Sub SortData()
Dim vInData As Variant, vOutData As Variant
Dim ii As Long, lCounter As Long
Dim wkOut As Worksheet
'Read in your data, you could set this as a function and pass it any range
vInData = ActiveSheet.Range("A1:C8").Value2
'Double up the output array just in case every record is valid, we can redim after processing
'Also not we've transposed the array because you can only redim preserve the second bound
ReDim vOutData(LBound(vInData, 2) To UBound(vInData, 2), LBound(vInData, 1) To 2 * UBound(vInData, 1))
'Loop through the input
For ii = LBound(vInData, 1) To UBound(vInData, 1)
'Check for the yes flag first
If vInData(ii, 2) = "Y" Then
'Then check the percentage bounds
If vInData(ii, 3) > 0 And vInData(ii, 3) < 100 Then
'Increase the counter by two since we're adding two lines.
lCounter = lCounter + 2
vOutData(1, lCounter - 1) = vInData(ii, 1) & "_G"
vOutData(2, lCounter - 1) = "Y"
vOutData(3, lCounter - 1) = 100
vOutData(1, lCounter) = vInData(ii, 1) & "_NG"
vOutData(2, lCounter) = "Y"
vOutData(3, lCounter) = 0
End If
End If
Next ii
'Now we have all the outputs redim the array to remove empty elements
ReDim Preserve vOutData(LBound(vOutData, 1) To UBound(vOutData, 1), LBound(vOutData, 2) To lCounter)
'I've just dumped the output onto a fresh sheet, you can set the output array to any range on any worksheet you like
Set wkOut = ThisWorkbook.Worksheets.Add
With wkOut
.Name = "Output"
.Range(.Cells(1, 1), .Cells(UBound(vOutData, 2), UBound(vOutData, 1))).Value2 = Application.WorksheetFunction.Transpose(vOutData)
End With
End Sub
不要在excel中这样做。在生成excel的任何地方都可以这样做...... – 2013-03-12 21:28:30
也许他以excel“报告”格式输出数据,这些格式不再存在于原始数据源中。 – 2013-03-12 21:31:53
第二个表格将位于不同的选项卡/工作簿中。有数百万条记录被收到,其中50万条将以flag_1为'Y'。任何建议来自动化这个过程。我们无法控制源文件中的数据,这种情况下是第一个表格。 – Sonali 2013-03-12 21:32:16