vba 宏获取文件夹中所有excel文件,对slk文件数据每3000个求平均值
来源:互联网 发布:ce源码有c语言 编辑:程序博客网 时间:2024/05/21 13:55
功能:此宏用于处理excel数据,一个文件夹中保存了大约200个slk文件,每个文件中一般是30000个数据。代码获取文件夹中所有slk文件,每
3000个数据求平均值,将平均值保存在一个xls文件中,并自动生成散点图,用于数据分析。
vba源码如下:
Sub average()
Dim myPath$, myFiles() As String, arr() As String, myFile$, xlApp, filelog, n%, timegap As Single, total As Double, Circulation%, group%, current%
group = 3000
group = Application.InputBox(prompt:="请输入每几个数求平均值", Type:=1, Default:=3000)
current = group - 1
total = 0
With Workbooks(1).Sheets.Add
.Name = "临时文件"
End With
Set filelog = Application.FileDialog(msoFileDialogFolderPicker)
If filelog.Show = True Then
myPath = filelog.SelectedItems(1)
Application.ScreenUpdating = False '冻结屏幕,以防屏幕抖动
myFile = Dir(myPath & "/*.slk") '依次找寻指定路径中的*.slk文件
If myFile <> "" Then
n = 1
ReDim arr(1 To 1)
Do While myFile <> "" '将文件名存入数组
If n > 1 Then
For i = 1 To n - 1
arr(i) = myFiles(i)
Next i
End If
arr(n) = myFile
ReDim myFiles(1 To n)
myFiles = arr
n = n + 1
ReDim arr(1 To n)
myFile = Dir
Loop
n = n - 1
If n >= 2 Then '文件名按升序排列
For i = 1 To n - 1
For j = i + 1 To n
If Val(Replace(myFiles(i), "_part", "")) > Val(Replace(myFiles(j), "_part", "")) Then
myFile = myFiles(i)
myFiles(i) = myFiles(j)
myFiles(j) = myFile
End If
Next j
Next i
End If
Cells(1, 1) = 3
Cells(1, 2) = 0
Application.Workbooks.Open (myPath & "/" & myFiles(1))
timegap = Workbooks(2).Sheets(1).Cells(3001, 1) - Workbooks(2).Sheets(1).Cells(1, 1)
Workbooks(2).Close True
For j = 1 To n
Application.Workbooks.Open (myPath & "/" & myFiles(j)) '依次打开SLK文件
Workbooks(2).Sheets(1).Activate
current = group - current
Circulation = current
With Workbooks(1).Sheets("临时文件")
If current <> 1 Then
.Range("a65536").End(xlUp).Offset(1).Value = (total + Application.WorksheetFunction.Sum(Cells(1, 2).Resize(current - 1))) / group
.Range("b65536").End(xlUp).Offset(1).Value = .Range("b65536").End(xlUp).Value + timegap
End If
For i = 1 To (Range("b65536").End(xlUp).row - Circulation + 1) / group
.Range("a65536").End(xlUp).Offset(1).Value = Application.WorksheetFunction.average(Cells(current, 2).Resize(group))
.Range("b65536").End(xlUp).Offset(1).Value = .Range("b65536").End(xlUp).Value + timegap
current = current + group
Next i
End With
If Range("a65536").End(xlUp).row = current - 1 Then
total = 0
current = group - 1
Else
total = Application.WorksheetFunction.Sum(Range("b" & current & ":b" & Range("b65536").End(xlUp).row))
current = Range("a65536").End(xlUp).row - current
End If
Workbooks(2).Close False
Next j
Application.ScreenUpdating = True '冻结屏幕,此类语句一般成对使用
Cells(1, 1).Delete
Range("b65536").End(xlUp).Delete
Charts.Add
With ActiveChart
.ChartType = xlXYScatter
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = Sheets("临时文件").Range("b1:" & Sheets("临时文件").Range("b65536").End(xlUp).Address)
.SeriesCollection(1).Values = Sheets("临时文件").Range("a1:" & Sheets("临时文件").Range("a65536").End(xlUp).Address)
.Location Where:=xlLocationAsObject, Name:="临时文件"
End With
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "蠕变图"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "时间"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "数据"
End With
ActiveChart.HasLegend = False
Application.Workbooks.Add
Application.DisplayAlerts = False
Workbooks(2).Sheets(1).Delete
Application.DisplayAlerts = False
Workbooks(1).Sheets("临时文件").Move before:=Workbooks(2).Sheets(1)
Application.DisplayAlerts = False
Workbooks(2).Sheets(1).Name = "Sheet1"
Workbooks(2).SaveAs myPath & "/处理结果.xls"
MsgBox "完成!"
Else
MsgBox "该目录没有slk文件!"
End If
Else
Application.DisplayAlerts = False
Worksheets("临时文件").Delete
Application.DisplayAlerts = True
End If
End Sub
- vba 宏获取文件夹中所有excel文件,对slk文件数据每3000个求平均值
- vba 中, 创建文件夹, 获取行数, 新建excel文件
- VBA中如何打开一个文件夹内的所有EXCEL文件?
- VBA案例4:取文件夹中所有文件的名称
- 求向量中每100个点的平均值
- VBA获取某文件夹下所有文件和子文件目录的文件
- 获取所有文件夹中所有文件url(不包括文件夹)
- Java获取文件夹中所有文件
- 获取文件夹中所有文件的文件名
- Excel VBA 移动文件和文件夹
- Excel VBA 遍历目录下所有文件
- War3.mpq中slk文件详解
- War3.mpq中slk文件详解
- VBA 对 文件和文件夹的操作
- Python遍历文件夹下所有文件中数据并写入Excel
- 用VBA获取文件夹中的文件列表
- Java获取文件夹下所有文件文件名写入文件中
- Excel VBA 中 Excel文件的操作
- Chrome OS, 埃里森和他的NC
- Teradata中四舍五入规则与标准规则的差异
- 在Davinci平台上用CCS调试基于Codec Engine机制的DSP端程序
- windows 7安装后无法引导xp系统解决
- 屏幕右下角显示 AMD Unsupported hardware的logo
- vba 宏获取文件夹中所有excel文件,对slk文件数据每3000个求平均值
- 面视 收集
- 心目中的编程高手
- textbox回车换行空格替换
- IE的问题
- 字符串匹配
- 类似QQ的竖型目录菜单
- 嘿嘿,国庆要放假了
- BugTraq是什么?