嗨我需要在MS excel的结果栏中提供结果。输入数据如下所示。在第一行中,有 3 X列(X1,X2,X3 ......)单元格包含" Req"及其相关的 2 Y个单元格(Y1,Y2,Y3 ...)Y1,Y2包含值。 X3有" Req"但Y3没有价值。这就是为什么,在结果列中,它已被标记为 3个中的1个。 X4有Glove但没有" Req"前缀。意思是不需要。
X1 | Y1 | X2 | Y2 | X3 | Y3| X4 | Y4| X5| Y5| Result
Req-BX |BOX |Req-EA |EACH |Req-CA | |Glove | | | | 1 out of 3 Required
Req-BT |BOTTLE|Req-GL | |Req-CTN| | | | | | 2 out of 3 Required
我有专栏,直到X50,Y50。请帮忙。
答案 0 :(得分:1)
试试这个VBA:
Option Explicit
Sub Stack()
Dim DataSheet As Worksheet
Dim LastRow As Long, LastCol As Long, _
ResultCol As Long, RowIdx As Long, _
ColIdx As Long, ReqCounter As Long, _
FoundCounter As Long
'assign sheet for easy reference
Set DataSheet = ThisWorkbook.Worksheets("Sheet1")
'define the range for our loops
LastRow = 500
LastCol = 75 'column CT
ResultCol = 76 'column CU
'loop through target rows
For RowIdx = 2 To LastRow
'initialize counters
ReqCounter = 0
FoundCounter = 0
'loop through target columns
For ColIdx = 1 To LastCol Step 2
'check to see if the cell contains "Req" and increment as necessary
If InStr(1, DataSheet.Cells(RowIdx, ColIdx), "Req", vbTextCompare) > 0 Then
ReqCounter = ReqCounter + 1
End If
'check the neighboring cell for a non-blank value and increment as necessary
If DataSheet.Cells(RowIdx, ColIdx + 1).Value <> "" Then
FoundCounter = FoundCounter + 1
End If
Next ColIdx
'write to the result cell
DataSheet.Cells(RowIdx, ResultCol) = FoundCounter & " out of " & ReqCounter & " Required"
Next RowIdx
Msgbox ("Script complete!")
End Sub