简体   繁体   English

我需要将多个电子表格中的多个单元格复制到基于Excel中第三个单元格内容的摘要表中

[英]I need to copy several cells from multiple spreadsheets to a summary sheet base on the contents of a third cell in excel

I need to copy the contents of cells A2 to A88 and C2 to C88 based on the contents of what is in cells in column G from several spreadsheets in a workbook to the Summary sheet. 我需要根据工作簿中几个电子表格中G列中单元格的内容,将单元格A2到A88以及C2到C88的内容复制到摘要表中。

So I need code to scan all spreadsheets to see if the word Case closed is in cell G33 and than copy the contents of cell A33 and C33 to a cell on the summary page. 因此,我需要代码来扫描所有电子表格,以了解Case Case单词是否位于G33单元格中,然后将A33和C33单元格的内容复制到摘要页面上的某个单元格中。

I have seen several close answers but nothing that does the job. 我已经看到了几个接近的答案,但没有任何工作可做。

Sorry no code available. 抱歉,没有可用的代码。

Thanks for any and all answers. 感谢您提供所有答案。

You could create some vba if you cannot solve this using excel formulas... I made a little test excel sheet with following vba code: 如果您无法使用excel公式解决此问题,则可以创建一些vba。我使用以下vba代码制作了一个测试excel表格:

Sub test()
    processSheet Application.ActiveWorkbook, "Sheet1"
End Sub

Function FindSheet(currentWorkbook As Workbook, sheetName As String) As Worksheet
    If currentWorkbook Is Nothing Then
        Err.Raise vbObjectError + 1, "FindSheet", "Supplied workbook is nothing"
    End If
    Dim idx As Integer
    For idx = 1 To currentWorkbook.Sheets.Count
        Dim checkSheet As Worksheet
        Set checkSheet = currentWorkbook.Sheets.Item(idx)
        If checkSheet.Name = sheetName Then
            Set FindSheet = checkSheet
            Exit Function
        End If
    Next
End Function

Function IsEmpty(currentCell As Range) As Boolean
    IsEmpty = False
    If currentCell.Value = "" And currentCell.Value2 = "" Then
        IsEmpty = True
    End If
End Function

Sub processSheet(currentWorkbook As Workbook, sheetName As String)
    On Error GoTo Catch
    Dim currentSheet As Worksheet
    Set currentSheet = FindSheet(currentWorkbook, sheetName)
    If currentSheet Is Nothing Then
        Err.Raise vbObjectError + 2, "ProcessSheet", "Could not find sheet " + sheetName
    End If
    Dim colA As Range
    Dim colB As Range
    Dim colCondition As Range
    Dim colResult As Range

    currentSheet.Activate

    Set colA = currentSheet.Columns(1)
    Set colB = currentSheet.Columns(2)
    Set colCondition = currentSheet.Columns(3)
    Set colResult = currentSheet.Columns(4)

    Dim index As Integer: index = 2
    Dim run As Boolean: run = True

    Do While run
        If IsEmpty(colA.Rows(index)) And IsEmpty(colB.Rows(index)) And IsEmpty(colCondition.Rows(index)) Then
            run = False
        Else
            index = index + 1
            If colCondition.Rows(index).Value = "Closed" Then
                resultContent = CStr(colA.Rows(index).Value2) + ": " + CStr(colB.Rows(index).Value2)
            Else
                resultContent = "-"
            End If
            colResult.Rows(index).Value2 = resultContent
        End If
    Loop
    GoTo Finally

Catch:
        MsgBox ("An error occured: " + Err.Description)
        Exit Sub
Finally:

    End Sub

You can just put this macro in the macros of a new workbook. 您可以仅将此宏放在新工作簿的宏中。 Open the Sheet1 and add 4 columns. 打开Sheet1并添加4列。 I added a screenshot of how the excel sheet looks like. 我添加了一张Excel工作表的屏幕截图。

As a new user I'm not allowed to post images.. so here is the link: Sheet1 作为新用户,我不允许发布图像..因此,这里是链接: Sheet1

Short explanation of the code. 代码的简短说明。

  • A workbook is passed and a sheet selected by a sheet name 通过工作簿,并通过工作表名称选择工作表
  • If the sheet is available the script runs through the three dependent columns (two columns needed for concatenation and one for the condition) and checks if the values are set. 如果该工作表可用,则脚本将遍历三个相关列(连接需要两列,条件需要一列),并检查是否设置了值。 The loop stops when all the three columns do not contain any value (in your case you could hardcode the start and end index, if it always stays the same). 当所有三列都不包含任何值时,循环停止(如果您始终将其保持不变,则可以硬编码开始索引和结束索引)。
  • During the iteration, the condition field is checked. 在迭代期间,将检查条件字段。 If it is equals "Closed", the result cell is filled with the first two columns values concatenated. 如果等于“ Closed”,则结果单元格将前两个列的值连接在一起。

You certainly need to adapt the code to your problem, but shouldn't be a big thing to do. 您当然需要使代码适应您的问题,但是这并不是一件大事。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将合并后的单元格从其他工作表复制到摘要工作表中的一个单元格中 - Copy merged cells from other sheets into one cell in summary sheet 如果第三个单元格数据变量与第一个工作表变量匹配,Excel会将两个数据单元格从一个工作表复制到另一工作表 - Excel copy two cells of data from one sheet to another sheet if a third cell data variable matches the first sheet variable 从多个Excel文件中获取数据并将其复制到摘要表中 - Grabbing data from multiple excel files and copy them in a summary sheet Excel VBA:将单元格从多个工作表复制到单个工作表 - Excel VBA: Copy cells from multiple sheets to a single sheet 将单元格中的单元格复制到多张Excel - VBA中 - copy cells from one sheet into multiple sheets Excel - VBA Macro Excel可根据单元格匹配将范围单元格从一张纸复制到另一张纸,如果不匹配,则跳过单元格 - Macro Excel to copy range cells from one sheet to another based on cell match and skip cell if no match 将单元格值从Excel工作表复制到数组中 - Copy Cells values from Excel sheet into an array 根据多个条件将行从多个Excel工作表复制到摘要页面 - Copy rows from multiple excel sheets to a summary page based on several criteria Excel宏可根据单元格值将一行中的选定单元格从一张纸复制到另一张纸 - Excel Macro to copy select cells from a row from one sheet to another based upon a cell value Excel宏:将特定的单元格从多个工作表复制到单独的工作表中的特定单元格 - Excel Macro: Copy specific cells from multiple sheets to specific cells in separate sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM