合并多个工作簿时为每个项目创建单独的行

2024-01-21

I have several hundred spreadsheets that I would like to combine into a single master sheet. Each spreadsheet contains general description information in several sells, and then a list of parts with columns of information that are specific to each part, as shown: enter image description here

In the master sheet, I want a separate line for each part that includes the general information as well as the specific part information, as shown: enter image description here

I have created a loop that pulls all the information I want, but all the information is written as a single line in the master sheet, as shown: enter image description here

谁能告诉我如何为每个项目创建单独的行?显示了我拼凑在一起的代码 - 我认为问题的解决方案在于如何格式化标题为“更改此范围以满足您自己的需求”的部分

Sub MergeNT154BatchCards()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet

Dim dt As String
Dim bookName As String

Dim rnum As Long, CalcMode As Long
Dim a As Range, c As Range
Dim x As Long

Dim sourceRange As Range, destrange As Range

' Change this to the path\folder location of your files.
MyPath = "C:\Users\amiller\OneDrive - CoorsTek\temp"

' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
    MyPath = MyPath & "\"
End If

' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xls*")
If FilesInPath = "" Then
    MsgBox "No files found"
    Exit Sub
End If

' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
    FNum = FNum + 1
    ReDim Preserve MyFiles(1 To FNum)
    MyFiles(FNum) = FilesInPath
    FilesInPath = Dir()
Loop

' Set various application properties.
With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
End With

' Add a new workbook with one sheet.
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    ActiveSheet.Name = "Density"
    bookName = "DensitySummary"
    dt = Format(CStr(Now), "yyyy_mm_dd_hh.mm")
    BaseWks.SaveAs Filename:="C:\Users\amiller\OneDrive - CoorsTek\temp\" & bookName & dt
rnum = 1

Range("A1").Value = "FileName"
Range("B1").Value = "Description"
Range("C1").Value = "WaterTemp(C)"
Range("D1").Value = "WaterDensity(g/cc)"
Range("E1").Value = "PartID"
Range("F1").Value = "DryMass(g)"
Range("G1").Value = "SuspendedMass(g)"
Range("H1").Value = "Density(g/cc)"

' Loop through all files in the myFiles array.
If FNum > 0 Then
    For FNum = LBound(MyFiles) To UBound(MyFiles)
        Set mybook = Nothing
        On Error Resume Next
        Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
        On Error GoTo 0

        If Not mybook Is Nothing Then
            On Error Resume Next

            ' Change this range to fit your own needs.
            With mybook.Worksheets(1)
                Set R1 = Range("A11, A5, B5")
                Set R2 = Range("A13:D" & Range("A13").End(xlDown).Row)
                Set RF = Union(R1, R2)
                Set sourceRange = RF

            End With

            If Err.Number > 0 Then
                Err.Clear
                Set sourceRange = Nothing
            Else

                ' If source range uses all columns then
                ' skip this file.
                If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                    Set sourceRange = Nothing
                End If
            End If
            On Error GoTo 0

            If Not sourceRange Is Nothing Then

                SourceRcount = sourceRange.Rows.Count

                If rnum + SourceRcount >= BaseWks.Rows.Count Then
                    MsgBox "There are not enough rows in the target worksheet."
                    BaseWks.Columns.AutoFit
                    mybook.Close savechanges:=False
                    GoTo ExitTheSub
                Else

                    ' Copy the file name in column A.
                    With sourceRange
                        BaseWks.Cells(rnum + 1, "A"). _
                                Resize(.Rows.Count).Value = MyFiles(FNum)
                    End With

                    ' Set the destination range.
                    Set destrange = BaseWks.Range("B" & rnum + 1)

                    x = 0
                    For Each a In sourceRange.Areas
                        For Each c In a.Cells
                            x = x + 1
                            destrange.Offset(0, x - 1).Value = c.Value
                        Next c
                    Next a

                    ' Copy the values from the source range
                    ' to the destination range.
                    With sourceRange
                        Set destrange = destrange. _
                                        Resize(.Rows.Count, .Columns.Count)
                    End With
                    destrange.Value = sourceRange.Value

                    rnum = rnum + SourceRcount
                End If
            End If
            mybook.Close savechanges:=False
        End If

    Next FNum
    BaseWks.Columns.AutoFit
End If

ExitTheSub:
    ' Restore the application properties.
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub

我有点担心,因为您似乎写入主表的标题似乎与数据不一致,而且因为您似乎只是复制Range("A11, A5, B5")从每张纸的顶部,但你的图像显示 5 个字段是从顶部拍摄的,但我think你可以更换你的For FNum循环如下:

For FNum = LBound(MyFiles) To UBound(MyFiles)
    Set mybook = Nothing
    On Error Resume Next
    Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
    On Error GoTo 0

    If Not mybook Is Nothing Then
        With mybook.Worksheets(1)
            Set SourceRange = .Range("A13:D" & .Range("A13").End(xlDown).Row)

            SourceRcount = SourceRange.Rows.Count

            If rnum + SourceRcount >= BaseWks.Rows.Count Then
                MsgBox "There are not enough rows in the target worksheet."
                BaseWks.Columns.AutoFit
                mybook.Close savechanges:=False
                GoTo ExitTheSub
            Else

                ' Copy the file name in column A.
                BaseWks.Cells(rnum + 1, "A").Resize(SourceRcount).Value = MyFiles(FNum)
                ' Copy information such as date/time started, start/final temp, and Batch ID
                BaseWks.Cells(rnum + 1, "B").Resize(SourceRcount).Value = .Range("A4").Value
                BaseWks.Cells(rnum + 1, "C").Resize(SourceRcount).Value = .Range("B4").Value
                BaseWks.Cells(rnum + 1, "D").Resize(SourceRcount).Value = .Range("A5").Value
                BaseWks.Cells(rnum + 1, "E").Resize(SourceRcount).Value = .Range("A5").Value
                BaseWks.Cells(rnum + 1, "F").Resize(SourceRcount).Value = .Range("A11").Value
                'Copy main data
                BaseWks.Cells(rnum + 1, "G").Resize(SourceRcount, SourceRange.Columns.Count).Value = SourceRange.Value

                rnum = rnum + SourceRcount
            End If
        End With
    End If
    mybook.Close savechanges:=False
Next FNum
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

合并多个工作簿时为每个项目创建单独的行 的相关文章

  • VBA在下一个空白行正确输入用户表单数据

    创建了一个用户表单 添加了一个文本框和一个组合框 添加了提交按钮 单击 提交 后 会将数据添加到电子表格中 据我所知和我所读到的 这是错误的 ActiveCell Value TextBox3 Text ActiveCell Offset
  • VBA - 如何从网站下载.xls并将数据放入Excel文件

    我设法使用 VBA 达到准备从网络下载 Excel 文件的程度 但我无法弄清楚如何实际下载该文件并将其内容放入我正在使用的 Excel 文件中 有什么建议么 谢谢 这是到目前为止的代码 Sub GetData Dim IE As Inter
  • 根据用户名获取广告详细信息

    我有一个代码可以从 AD 检索用户的详细信息 例如电子邮件地址 电话号码等 我当前使用的代码是 Set objSysInfo CreateObject ADSystemInfo strUser objSysInfo UserName msg
  • 粘贴数据后取消选择列

    这可能很简单 但我无法解决它 我可以使用以下命令释放复制数据的列Application CutCopyMode False但粘贴数据的列仍然保持选中状态 我尝试过ActiveSheet EnableSelection xlNone正如论坛之
  • xlrd 读取 xls XLRDError:不支持的格式或损坏的文件:预期的 BOF 记录;找到“\r\n”

    这是代码 xls open workbook data xls 作为回报 File home woles P2 fin fin apps data container importer py line 16 in import data x
  • SQL查询从表的每条记录生成多条记录

    我有一个包含 3000 条记录的表 使用其中的每一条记录 我必须生成大约 200 条记录 总共 600k 条记录 并通过 SQL Server 2012 将它们插入到第二个表中 我尝试使用 VBA 执行此操作 从第一个表中选择数据 计算 然
  • 合并数据框中的值以写入 Excel

    我有一个看起来像的数据框 column1 column2 column3 colum4 column5 1 r n 1 r s 1 r n 2 r s 3 r n 3 2 r n 1 r s 1 r n 4 r s 4 r n 5 3 r
  • Excel VBA - 以编程方式列出用户窗体上控件的可用事件过程

    你好 我已经搜索过 google 但发现只有 1 页提到了如何在 MS Access 中执行此操作 但没有在 MS Excel 中执行此操作 此处 列出 MS Access 表单的控件及其事件 https stackoverflow com
  • 调用 UDF 时公式中使用的值的数据类型错误

    我一直在努力找出这里出了什么问题 我有两个包含字符串值的列 我使用第三列调用工作表中的 UDF 但最终得到 Value 并出现错误 公式中使用的值的数据类型错误 Eg Col I Col J File1 Y File1 N File2 Y
  • VBA - 使用字符串串联调用子例程

    是否可以使用字符串串联来调用 VBA 中的子例程 例如 Sub Call This 2019 do something end sub Sub From this Call Call This str 2019 Sub 我使用这种方法没有任
  • vba Excel 中的多个查找请求(在查找中查找)

    我正在尝试执行一种嵌套查找请求 用例是我需要在一个工作表上查找组 如果找到 则从找到的行中的单独列中获取用户 ID 值 然后在另一张纸 然后它应该执行一系列操作 然后在第一张表中找到下一个出现的组 我的代码是 LookupGroup Spl
  • 提高此 Excel 公式性能的最快方法是什么?

    这是从下面的帖子更进一步的内容 如何克服Excel中公式的最大长度限制 excel的一个bug https stackoverflow com questions 32604740 how to overcome the max lengt
  • 如何从Excel调用VBA函数

    我有一个 excel 文件 我必须在其中放置验证规则 我有一个单元格表示 客户时间 用户可以在其中输入时间 但它是自定义时间 用户可以这样输入时间 23 45 98 20 100 30 用户不能输入字符串 并且不能输入除冒号之外的特殊字符
  • 为什么 MS Excel 在 Worksheet_Change Sub 过程中崩溃并关闭?

    当我在 Excel 工作表上运行 VBA 代码时 我遇到了 Excel 崩溃的问题 我正在尝试在工作表更改中添加以下公式 Private Sub Worksheet Change ByVal Target As Range Workshee
  • Excel 公式或 VBA:在具有 2 列条件的单独表中查找匹配的地址 - 无辅助列

    我需要公式结构方面的帮助 我有2张桌子 我想找到 a 列和 b 列相等的匹配项并获取表 2 中的地址 它们将是唯一的条目 例如 项目信息表 A B C 1 Name Company Project 2 Chris Evans Apple I
  • 在没有加载项的情况下加载 Excel

    All 我编写了一个电子表格应用程序 用户通过双击我提供的图标来加载它 问题是用户有很多插件 这会降低 Excel 的速度 如何使用命令行开关加载 Excel 来禁用所有加载项 该问题适用于 Excel 2003 和 Excel 2007
  • 使用 SpreadsheetLight 进行行计数

    我正在寻找一个类似于 DataTable Rows Count 的函数 它可以与 SLDocument 一起使用来找出有多少行中有数据 SpreadsheetLight 中有可用的东西吗 还有其他方法可以实现这一目标吗 Brendan SL
  • Excel宏-将逗号分隔的条目拆分为新行[重复]

    这个问题在这里已经有答案了 我目前在一张纸上有这些数据 Col A Col B Col C 1 A angry birds gaming 2 B nirvana rock band 我想要做的是将第三列中的逗号分隔条目拆分并插入新行 如下所
  • 根据不同的列数据范围隐藏行

    我对使用 VBA 相当陌生 我正在尝试创建一个代码 该代码将查看具有不同数据范围的两个不同列 并隐藏最后一个数据点之外的行 引用两列 目前我有这个 Private Sub Worksheet PivotTableUpdate ByVal T
  • 连续工作表切换循环

    我有一个 Excel 电子表格来显示 SQL 数据图表 这些图表将显示在我们机加工车间的显示器上 该电子表格有 45 个选项卡 我需要在选项卡之间切换 每个选项卡间隔 10 秒 然后返回到第一个选项卡并重复循环 I found 如何使用 V

随机推荐