我有四列:姓名、代碼、雇用和解雇。代碼列中的值是唯一的。一年中可以多次雇用和解雇某人,但我只需要第一次雇用某人,也只需要最后一次解雇某人。我可以使用 vba 過濾和更新這些列嗎?
我擁有的是從 A 到 D 列中的值。我想要的是從 I 到 L 列中的值。

uj5u.com熱心網友回復:
因此,如果您不知道如何撰寫VBA 代碼,那么您也可以嘗試使用Excel 公式,但是對于以下公式,您肯定需要訪問O365或O365 Insiders Beta 版

F2? 單元格中用于獲取Unique Name&的公式Codes,
=UNIQUE(A2:B20)
? 單元格中使用的公式H2
=MINIFS(C2:C20,A2:A20,F2:F6,B2:B20,G2:G6)
? 單元格中使用的公式I2
=MAXIFS(D2:D20,A2:A20,F2:F6,B2:B20,G2:G6)
使用LET()使閱讀和理解更容易,
? 單元格中使用的公式F9
=LET(u,UNIQUE(A2:A20),
c,UNIQUE(B2:B20),
CHOOSE({1,2,3,4},u,c,
MINIFS(C2:C20,A2:A20,u,B2:B20,c),
MAXIFS(D2:D20,A2:A20,u,B2:B20,c)))
使用LAMBDA()Function 創建一個custom,函式并通過名稱管理器中使用的reusable函式參考它們,friendly name其定義名稱為HireFire,語法為LAMBDA()
=HireFire(array,header)
在哪里,
HireFire = LAMBDA(array,header,
VSTACK(TAKE(header,1,4),
LET(a,INDEX(array,,1),
b,INDEX(array,,2),
c,INDEX(array,,3),
d,INDEX(array,,4),
u,UNIQUE(a),
uc,UNIQUE(b),
HSTACK(u,uc,
MINIFS(c,a,u,b,uc),
MAXIFS(d,a,u,b,uc)))))(A2:D20,A1:D1)
? 因此,單元格中使用的公式F15
=HireFire(A2:D20,A1:D1)
由于您沒有提到您的 Excel 版本,因此您可能正在使用Excel 2019或2016等等2013,因此,下面顯示了替代方案,

? 單元格中使用的公式F2
=IFERROR(INDEX(A$2:A$20,MATCH(0,COUNTIF($F$1:F1,A$2:A$20),0)),"")
上面的公式,是一個陣列公式,需要根據你的Excel版本按CTRL ,SHIFTENTER
? 單元格中使用的公式G2
=IF($F2="","",VLOOKUP($F2,$A$2:$D$20,2,0))
? 單元格中使用的公式H2-->適用于 Excel 2019 及更高版本
=MINIFS(C$2:C$20,$A$2:$A$20,$F2,$B$2:$B$20,$G2)
如果不使用上述任何一個版本,那么,
=MIN(IF(($F2=$A$2:$A$20)*($G2=$B$2:$B$20),$C$2:$C$20,""))
它是一個陣列公式,因此需要按CTRL SHIFT并ENTER填寫!
? 單元格中使用的公式I2-->適用于 Excel 2019 及更高版本
=MAXIFS(D$2:D$20,$A$2:$A$20,$F2,$B$2:$B$20,$G2)
如果不使用上述任何一個版本,那么,
=MAX(IF(($F2=$A$2:$A$20)*($G2=$B$2:$B$20),$D$2:$D$20,""))
它是一個陣列公式,因此需要按CTRL SHIFT并ENTER填寫!
uj5u.com熱心網友回復:
如果您有 Office 365,則可以使用其他答案之一中概述的公式來執行此操作。
這也可以使用 Windows Excel 2010 和 Excel 365(Windows 或 Mac)中提供的 Power Query 來完成
使用 Power Query
- 在資料表中選擇一些單元格
Data => Get&Transform => from Table/Range或者from within sheet- 當 PQ 編輯器打開時:
Home => Advanced Editor - 記下第 2 行中的表名
- 粘貼下面的 M 代碼代替您看到的內容
- 將第 2 行中的表名稱更改回最初生成的名稱。
- 閱讀評論并探索
Applied Steps以了解演算法
M代碼
let
//Read in data
// Change table name in next line to your actual table name
Source = Excel.CurrentWorkbook(){[Name="EmplTbl"]}[Content],
//Set the column data types
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"NAME", type text}, {"CODE", type text}, {"HIRED", type date}, {"FIRED", type date}}),
//Group by Name and ID
// Then aggregate by minimum HIRED and maximum FIRED to get results
#"Grouped Rows" = Table.Group(#"Changed Type", {"CODE", "NAME"}, {
{"Hired", each List.Min([HIRED]), type nullable date},
{"Fired", each List.Max([FIRED]), type nullable date}
})
in
#"Grouped Rows"

uj5u.com熱心網友回復:
使用字典的最大值和最小值是唯一的
Sub CreateHireFireReport()
Const sName As String = "Sheet1"
Const sFirstCellAddress As String = "A1"
Const uCol As Long = 2
Const hCol As Long = 3
Const fCol As Long = 4
Const dName As String = "Sheet1"
Const dFirstCellAddress As String = "I1"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim srg As Range: Set srg = sws.Range(sFirstCellAddress).CurrentRegion
If srg.Rows.Count < 2 Then Exit Sub ' no data or just headers
Dim srCount As Long: srCount = srg.Rows.Count
Dim cCount As Long: cCount = srg.Columns.Count
Dim sData As Variant: sData = srg.Value
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim Key As Variant
Dim sr As Long
For sr = 2 To srCount
Key = sData(sr, uCol)
If Not IsError(Key) Then
If Len(Key) > 0 Then
dict(Key) = Empty
End If
End If
Next sr
If dict.Count = 0 Then Exit Sub ' only blanks and error values
Dim drCount As Long: drCount = dict.Count 1
Dim dData As Variant: ReDim dData(1 To drCount, 1 To cCount)
Dim ddr As Long: ddr = 1
Dim dr As Long
Dim c As Long
' Write headers.
For c = 1 To cCount
dData(1, c) = sData(1, c)
Next c
' Write data.
For sr = 2 To srCount
Key = sData(sr, uCol)
If Not IsError(Key) Then
If Len(Key) > 0 Then
If dict(Key) = Empty Then
ddr = ddr 1
dr = ddr
dict(Key) = ddr
For c = 1 To cCount
dData(dr, c) = sData(sr, c)
Next c
Else
dr = dict(Key)
If IsDate(sData(sr, hCol)) Then
If IsDate(dData(dr, hCol)) Then
If sData(sr, hCol) < dData(dr, hCol) Then
dData(dr, hCol) = sData(sr, hCol)
End If
Else
dData(dr, hCol) = sData(sr, hCol)
End If
End If
If IsDate(sData(sr, fCol)) Then
If IsDate(dData(dr, fCol)) Then
If sData(sr, fCol) > dData(dr, fCol) Then
dData(dr, fCol) = sData(sr, fCol)
End If
Else
dData(dr, fCol) = sData(sr, fCol)
End If
End If
End If
End If
End If
Next sr
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
With dws.Range(dFirstCellAddress).Resize(, cCount)
' Format unique column as text.
.Resize(drCount, 1).Offset(, uCol - 1).NumberFormat = "@"
' Write result.
.Resize(drCount).Value = dData
' Clear below.
.Resize(dws.Rows.Count - .Row - drCount 1).Offset(drCount).Clear
' Apply other formatting.
.Font.Bold = True ' headers
.EntireColumn.AutoFit
End With
MsgBox "Hire-fire-report created.", vbInformation
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/471475.html
上一篇:如果我后來從其中任何一個中洗掉了任何超鏈接,為什么excel會洗掉所有粘貼的超鏈接?
下一篇:突出顯示選定單元格的周圍單元格
