作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
再会!我有一个代码可以复制一些文件中的所有信息并将其插入到主文件中。我想修改它。不是抄袭,而是总结。
Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Dim Rws As Long, Rng As Range, r as Range
Set Wb = ThisWorkbook
MyDir = "C:\Project\"
MyFile = Dir(MyDir & "*.xlsx")
ChDir MyDir
Application.ScreenUpdating = 0
Application.DisplayAlerts = 0
Do While MyFile <> ""
Workbooks.Open (MyFile)
For Each r in Rows
With Worksheets("Sheet1")
r.Rows.Hidden = False
Rws = .Cells(Rows.Count, 12).End(xlUp).Row
Set Rng = Range(.Cells(5, 12), .Cells(Rws, 12))
Rng.Copy Wb.Worksheets("Sheet1").Cells(Rows.Count, 12).End(xlUp).Offset(0, 1) 'So here I copy infromation adn insert it.
ActiveWorkbook.Close True
End With
Next
Application.DisplayAlerts = 1
MyFile = Dir()
Loop
我无法理解它。我如何总结相同的信息而不是复制它。
r.Rows.Hidden = False
最佳答案
你可以试试这个:
Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Dim Rws As Long, Rng As Range, r As Range
Set Wb = ThisWorkbook
MyDir = "C:\Project\"
MyFile = Dir(MyDir & "*.xlsx")
ChDir MyDir
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While MyFile <> ""
Dim sWb As Workbook
Set sWb = Workbooks.Open(MyFile)
With sWb.Worksheets("Sheet1")
.Rows.Hidden = False
Rws = .Cells(Rows.Count, 12).End(xlUp).Row
Set rng = Range(.Cells(5, 1), .Cells(Rws, 12))
End With
With Wb.Worksheets("Sheet1")
Dim MatchingColumn As Range
Set MatchingColumn = .Range(.Cells(5, 1), .Cells(.Rows.Count, 1).End(xlUp))
For Each r In rng.Rows
If r.Cells(1, 1).Value2 <> vbNullString Then 'Ignoring empty rows
'We find the row where the Ids matche
Dim MatchingRowNb As Long
MatchingRowNb = Application.Match(r.Cells(1, 1).Value2, MatchingColumn, False)
'We add the current value in the cell with the new value comming from the other file
.Cells(4 + MatchingRowNb, 12).Value2 = .Cells(4 + MatchingRowNb, 12).Value2 + r.Cells(1, 12).Value2
End If
Next
End With
sWb.Close SaveChanges:=True
Application.DisplayAlerts = True
MyFile = Dir()
Loop
关于excel - 如何从几个excel文件中获取信息到某个列的主文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63192642/
我是一名优秀的程序员,十分优秀!