当前位置:网站首页>VBA: 遍历文件抓取指定条件的数据
VBA: 遍历文件抓取指定条件的数据
2022-08-10 09:40:00 【用户9949496】
文章背景:要查看某次考试成绩不及格的所有学生名单;假定按年级建文件夹,每个文件夹内有各班的考试成绩表(见下图)。需要遍历所有表格,然后对每行的学生成绩进行判断。
图1 文件框架
图2 表格示例
通过Excel VBA的UserForm控件来完成本文的任务。
各个控件内的代码如下所示:
Option Explicit
Option Base 1
'存储数据
Dim data(), flag As Integer
Private Sub CommandButton6_Click()
'修改路径1的按钮
With Application.FileDialog(filedialogtype:=msoFileDialogFolderPicker)
.InitialFileName = "E:\工作\A校" '设置起始目录
.AllowMultiSelect = True '单选
.Title = "请选新的文件夹路径1" '设置对话框标题
.Show '显示对话框
If .SelectedItems.Count > 0 Then
TextBox1.Text = .SelectedItems(1) '将选中的文件夹路径添加到文本框1
Else
MsgBox "没有选择目录!"
End If
End With
End Sub
Private Sub CommandButton7_Click()
'修改路径2的按钮
With Application.FileDialog(filedialogtype:=msoFileDialogFolderPicker)
.InitialFileName = "E:\工作\B校" '设置起始目录
.AllowMultiSelect = True '单选
.Title = "请选新的文件夹路径2" '设置对话框标题
.Show '显示对话框
If .SelectedItems.Count > 0 Then
TextBox2.Text = .SelectedItems(1) '将选中的文件夹路径添加到文本框1
Else
MsgBox "没有选择目录!"
End If
End With
End Sub
Private Sub CommandButton8_Click()
'遍历查找
Dim tarSheet As Worksheet, num As Integer, folder As String
Dim time_ini As Date
'0 准备工作
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
time_ini = Timer
'1 清除原有数据
Set tarSheet = ThisWorkbook.Worksheets("查找结果")
num = tarSheet.Range("A65535").End(xlUp).Row
If num > 1 Then
tarSheet.Range("A2:E" & num).ClearContents
End If
flag = 0
'2 遍历文件夹1
folder = TextBox1.Text
searchdata folder
'3 遍历文件夹2
folder = TextBox2.Text
searchdata folder
'4 数据汇总
tarSheet.Range("A2").Resize(flag, 5) = Application.WorksheetFunction.Transpose(data)
MsgBox "Done! " & vbCrLf & vbCrLf & "用时:" & Format(Timer - time_ini, "0.0s")
Erase data
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Exit Sub
End Sub
Sub searchdata(folder As String)
'遍历子文件夹内的各个文件
Dim fso As Object, fld As Object, subfld As Object, filename As String
Dim aWB As Workbook, tempSheet As Worksheet, row_total As Integer
Dim ii As Integer, jj As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(folder) Then '判断文件夹是否存在
Set fld = fso.GetFolder(folder)
For Each subfld In fld.SubFolders '遍历子文件夹
filename = Dir(subfld & "\*.xlsx")
Do
Workbooks.Open subfld & "\" & filename
Set aWB = ActiveWorkbook
Set tempSheet = ActiveWorkbook.Worksheets(1)
row_total = tempSheet.Range("A65535").End(xlUp).Row
'遍历各行数据
If row_total > 1 Then
For ii = 2 To row_total
If tempSheet.Cells(ii, 5) < 60 Then
flag = flag + 1
ReDim Preserve data(1 To 5, 1 To flag)
For jj = 1 To 5
data(jj, flag) = tempSheet.Cells(ii, jj)
Next jj
End If
Next
End If
aWB.Close SaveChanges:=False
filename = Dir
Loop Until filename = ""
Next
Else
MsgBox "文件夹的路径不存在,请确认!"
Exit Sub
End If
End Sub
Private Sub UserForm_Initialize()
TextBox1.Text = "E:\工作\A校"
TextBox2.Text = "E:\工作\B校"
End Sub运行过程如下:
http://mpvideo.qpic.cn/0bf27qaiiaaawuaapfsuufpvb7gdqt6abbaa.f10003.mp4?
边栏推荐
- 【系统设计】S3 对象存储
- 【Prometheus】Node Exporter常用查询PromQL 语句大总结
- 2022-08-09 第六小组 瞒春 学习笔记
- How to understand the difference between BIO, NIO, and AIO
- Lasso regression (Stata)
- 如何理解BIO、NIO、AIO的区别
- 微信小程序--》小程序生命周期和WXS使用
- DataStream API(基础篇) 完整使用 (第五章)
- Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...
- keepalived:双主配置
猜你喜欢

12 【其它组合式API】

郭晶晶家的象棋私教,好家伙是个机器人

2022-08-09 第六小组 瞒春 学习笔记

FPGA的虚拟时钟如何使用?

腾讯云校园大使开始招募啦,内推名额和奖金等你来拿

Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...

shell------常用小工具,sort,uniq,tr,cut
![[Metaverse Omi Says] Listen to how Rabbit Fan Rabbit creates a new era of trendy play from virtual to reality](/img/e8/f431a9c932e0fa5f413b1f7e09bf46.png)
[Metaverse Omi Says] Listen to how Rabbit Fan Rabbit creates a new era of trendy play from virtual to reality

消息队列概述

UE4 Sequence添加基础动画效果 (05-蓝图触发Sequence)
随机推荐
DeepFake换脸诈骗怎么破?让他侧个身
keepalived:双主配置
重学冒泡排序
关于编程本质那些事
【企业架构】敏捷与企业架构:战略联盟
Oracle rac所在的网络要割接,停掉其中一个rac节点,这种方案可行吗?
多线程知识点总结之温故而知新
设计分享|基于单片机的从左到右流水灯
Lasso回归(Stata)
Singleton pattern base class
SQL优化总结
并发的基本概念,操作,容器
武功修炼:招式
[System Design] S3 Object Storage
91.(cesium之家)cesium火箭发射模拟
JWT:拥有我,即拥有权力
go web之cookie
CSDN 21 Days Learning Challenge - Polymorphism (05)
Defending risks with technology and escorting cloud native | Tongchuang Yongyi X Boyun held a joint product launch conference
【软考 系统架构设计师】案例分析⑥ Web应用系统架构设计