|
楼主 |
发表于 2023-4-16 08:50
|
显示全部楼层
本帖最后由 cmo9020 于 2023-4-30 11:04 编辑
谢谢导师的帮助
我有自己写了一个需要打开工作簿进行复制
但觉的取数据有点慢,代码也有些问题
会变成有多余的数据出現,我在研究研究一下,谢谢您~
Sub ReadD()
ThisWorkbook.Worksheets("Sheet1").Range("A3:I100").ClearContents
Dim FolderPath As String
Dim Filename As String
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Long
Dim Data As Variant
Dim Year As String
Year = Range("B1").Value
Dim Month As String
Select Case Range("D1").Value
Case "1月"
Month = Year & "01"
Case "2月"
Month = Year & "02"
Case "3月"
Month = Year & "03"
Case "4月"
Month = Year & "04"
End Select
Dim FolderName As String
FolderName = Range("F1").Value
FolderPath = "D:\数据\" & FolderName & "\" & Month & "\"
Application.ScreenUpdating = False
Filename = Dir(FolderPath & "*.xlsx")
Do While Filename <> ""
Set wb = Workbooks.Open(FolderPath & Filename, False, True)
Set ws = wb.Worksheets("Report")
' 复制 J3
Data = ws.Range("J3").Value
i = i + 1
ThisWorkbook.Worksheets("Sheet1").Range("A" & i + 2).Value = Data
' 寻找相同项目并复制数据
Dim j As Long
For j = 2 To 8
Dim FindItem As Variant
FindItem = ThisWorkbook.Worksheets("Sheet1").Cells(2, j).Value
Dim FoundCell As Range
Set FoundCell = ws.Range("A:A").Find(FindItem, LookIn:=xlValues)
If Not FoundCell Is Nothing Then
ThisWorkbook.Worksheets("Sheet1").Cells(2 + i, j).Value = FoundCell.Offset(0, 1).Value
End If
Next j
wb.Close SaveChanges:=False
Filename = Dir()
Loop
Application.ScreenUpdating = True
End Sub
|
|