|
楼主 |
发表于 2023-5-27 12:00
|
显示全部楼层
本帖最后由 同心/ty 于 2023-5-28 10:01 编辑
经过多位老师的帮助,在此表示感谢。最终代码附上。
Sub myTest()
Dim arr, dic, i%, j%, s#, r%, brr
Set dic = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
file = Application.GetOpenFilename(filefilter:="EXCEL 工作表(*.xlsx;*.xlsm;*.xls),*.xlsx;*.xlsm;*.xls")
If file <> False Then
With Workbooks.Open(file)
With .ActiveSheet
r = .Cells(.Rows.Count, "G").End(xlUp).Row - 6
arr = .[g7].Resize(r, 14)
End With
.Close False
End With
For i = 1 To UBound(arr)
If Len(Trim(arr(i, 1))) Then
For j = 9 To 14
s = s + arr(i, j)
Next j
dic(Trim(arr(i, 1))) = dic(Trim(arr(i, 1))) + s: s = 0
End If
Next i
With Sheet1
arr = .Range("e7:e" & .Cells(.Rows.Count, "e").End(xlUp).Row)
ReDim brr(1 To UBound(arr), 1 To 1)
For i = 1 To UBound(arr)
If dic.exists(Trim(arr(i, 1))) Then brr(i, 1) = dic(Trim(arr(i, 1)))
Next i
With .[o7].Resize(UBound(arr), 1)
.ClearContents
.Value = brr
End With
End With
Else
MsgBox "没有选定工作薄!~": Exit Sub
End If
Set dic = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
|
|