生成VBA代码,批量抓取文件夹下所有excel文档下,工作表名为“资产负债表”下,A2单元格的数据
时间: 2024-06-01 11:13:29 浏览: 109
你好,很高兴为您解答问题。
生成VBA代码,批量抓取文件夹下所有excel文档下,工作表名为“资产负债表”下,A2单元格的数据,可以按照以下步骤操作:
1. 在Excel中打开Visual Basic编辑器(VBE),按Alt+F11键打开VBE;
2. 在VBE中,选择“插入”菜单,选择“模块”进行代码的编写;
3. 编写以下代码:
Sub readexcel()
Dim filename As String
Dim foldername As String
Dim wb As Workbook
Dim ws As Worksheet
Dim dir As String
Dim i As Integer
dir = Application.ActiveWorkbook.Path & "\"
foldername = Application.FileDialog(msoFileDialogFolderPicker).Show
If foldername <> 0 Then
filename = Dir(foldername & "\*.xls*")
Do While filename <> ""
Set wb = Workbooks.Open(foldername & "\" & filename)
For Each ws In wb.Worksheets
If ws.Name = "资产负债表" Then
ws.Activate
Range("A2").Select
MsgBox ActiveCell.Value
End If
Next ws
Workbooks(filename).Close
filename = Dir
Loop
End If
End Sub
4. 点击“运行”按钮或按下F5键运行代码,即可批量抓取文件夹下所有Excel文档下,工作表名为“资产负债表”下,A2单元格的数据。
阅读全文