[英]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. 代码的简短说明。
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.