excel - 我怎样才能从几个Excel文件中获取信息到主文件中的某一列?

标签 excel vba

美好的一天!我有一个代码可以复制某些文件中的所有信息并将其插入主文件中。我想修改它。不是抄袭而是总结。

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

似乎没有做这件事。 任何帮助将不胜感激

结构:所有文件在结构上都是相同的,主文件是相同的文件,但其中没有任何信息,只有标题。 - 前 4 行 enter image description here第 12 栏(“L”) - 是最后一个包含我需要的信息,或者我应该说的信息。每行都有一个与其他行不同的 ID(第 1 列 - “A”)。所有文档都非常相似,这些行中的项目是相同的,唯一的区别是最后一列中的数量,这是我需要计算的。主文件:所有其他文件中的行 - 项目 - 数量。

最佳答案

你可以试试这个:

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/

相关文章:

vba - 获取 "Out of Stack Space error",不确定为什么/在我的代码中存在递归

excel - 删除通过 Excel VBA 生成的 Outlook 2010 邮件中的签名

arrays - 查找字符串数组中字符串的索引

vba - 在Excel VBA中手动设置Application.Caller

VBA Excel - 组合多个自动过滤器

android - 在错误的excel表中插入图像?

excel - 从 VBA 中的多个按钮调用相同的 SUB

excel - Powershell 4.0 和 Excel 2013 |错误 |周而复始

excel - 范围仅在从引用的工作表中调用时才有效

.net - 如何在 Excel VBA 中使用 .NET 对象?