0

我需要根据工作簿中多个电子表格中 G 列单元格中的内容将单元格 A2 到 A88 和 C2 到 C88 的内容复制到摘要表。

因此,我需要代码来扫描所有电子表格,以查看 Case closed 一词是否在单元格 G33 中,然后将单元格 A33 和 C33 的内容复制到摘要页面上的单元格中。

我已经看到了几个很接近的答案,但没有任何效果。

抱歉,没有可用的代码。

感谢任何和所有的答案。

4

1 回答 1

0

如果你不能使用 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

您可以将此宏放入新工作簿的宏中。打开 Sheet1 并添加 4 列。我添加了 Excel 工作表外观的屏幕截图。

作为新用户,我不允许发布图片..所以这里是链接:Sheet1

代码的简短说明。

  • 通过工作簿并通过工作表名称选择工作表
  • 如果工作表可用,则脚本将运行三个相关列(连接需要两列,条件需要一列)并检查是否设置了值。当所有三列都不包含任何值时,循环停止(在您的情况下,您可以硬编码开始和结束索引,如果它始终保持不变)。
  • 在迭代期间,检查条件字段。如果它等于“已关闭”,则结果单元格将使用连接的前两列值填充。

您当然需要根据您的问题调整代码,但这不是一件大事。

于 2012-07-11T10:47:07.837 回答