|
楼主 |
发表于 2010-3-10 10:55
|
显示全部楼层
此问题已经解决,把代码贴在这里,供有兴趣的朋友参考
下面是处理一个文件的代码,处理多个文件可在外面套一个循环
(附件没有更新,data文件即为待处理文件,data.txt为最初的代码,现已作废)
Workbooks.Open ("C:\DUAN\FLUID\CFD For UltraSonic FlowMeter\XY Files\Pipe_with_2_elbows\v0_5")
'***********************************************
Dim i_delt As Integer 'index of delt value
For i_delt = 4 To Range("A65536").End(xlUp).Row
If TypeName(Cells(i_delt, 1).Value) = "Double" Then
Cells(i_delt, 3).Value = Cells(i_delt, 2).Value - Cells(i_delt + 1, 2).Value
End If
Next i_delt
'column D is product of x*delt y
Dim i_pro As Integer
For i_pro = 1 To Range("A65536").End(xlUp).Row
If TypeName(Cells(i_pro, 1).Value) = "Double" Then
Cells(i_pro, 4).Value = Cells(i_pro, 1).Value * Cells(i_pro, 3).Value
End If
Next i_pro
'column E is the result: average value
Dim i_h_f As Integer 'index of every loop of single data group
Dim i_paste As Integer 'index of result paste destination
Dim sum As Double
Dim head As Integer
Dim foot As Integer
head = 4
foot = 4
i_paste = 1
Windows("shuju.xls").Activate
Sheets("two_elbows").Cells(i_paste, 1).Value = "v0_5"
i_paste = i_paste + 1
Windows("v0_5").Activate
Do While head < Range("A65536").End(xlUp).Row
Cells(head, 1).Activate
ActiveCell.CurrentRegion.Select
head = Selection.Row 'get the first row number of the selected range
foot = head + Selection.Rows.Count - 1
sum = 0
For i_h_f = head To foot
sum = sum + Cells(i_h_f, 4).Value
Next i_h_f
Cells(head, 5).Value = sum / (Cells(head + 1, 2).Value - Cells(foot - 1, 2).Value)
'%%%%%%%%%%%%%%%%%%%%%
Range(Cells(head, 1), Cells(head, 5)).copy
Windows("shuju.xls").Activate
Rows(i_paste).Select
ActiveSheet.Paste
Windows("v0_5").Activate
i_paste = i_paste + 1
'%%%%%%%%%%%%%%%%%%%%%
head = foot + 2
Loop
'***********************************************
Workbooks("v0_5").Close SaveChanges:=True |
|