答案1
VBA 中的以下代码可以完成此操作:
Public Sub ReqandAppr()
'****************
' variables
SourceSheet = "Sheet1" 'name of source sheet
DestSheet = "Sheet2" ' name of destination sheet
columntocheck = 5 'Column to check "Requested" or "Approved"
firstrow = 2 'first row in source sheet
destrow = 1 'first row in destination sheet
'end of variables
'*******************
Dim wkb As Workbook
Dim wksSource, wksDest As Worksheet
Set wkb = ThisWorkbook
Set wkSource = wkb.Sheets(SourceSheet)
Set wkDest = wkb.Sheets(DestSheet)
wkDest.Rows.Clear
totalrows = wkSource.Cells(Rows.Count, 1).End(xlUp).Row
For i = firstrow To totalrows
If wkSource.Cells(i, 1) <> wkSource.Cells(i - 1, 1) Then
requestedfound = False
approvedfound = False
End If
Select Case wkSource.Cells(i, columntocheck)
Case "Requested"
If requestedfound = False Then
wkSource.Rows(i).Copy Destination:=wkDest.range("A" & destrow)
requestedfound = True
destrow = destrow + 1
End If
Case "Approved"
If approvedfound = False And wkSource.Cells(i, columntocheck - 1) = 0 Then
wkSource.Rows(i).Copy Destination:=wkDest.range("A" & destrow)
approvedfound = True
destrow = destrow + 1
End If
End Select
Next i
End Sub
使用ALT+打开 VBA/MAcros F11,在本工作簿插入一个新的模块并将代码粘贴在右侧。
检查变量的值并执行。