作者: risemp6 時間: 2024-3-3 23:50 標題: Excel問題
想做到之下C2:C6的效果,但根據Copilot所提供的公式顯示Error,請教如何做到以下效果。謝謝!
問題:
-假設您的中心名稱在 A 欄,職位名稱在 B 欄,您想要產生的編號在 C 欄,並且您的資料從第二列開始。
-在 C2 儲存格中,輸入公式 =B2&" "&TEXT(COUNTIF($A$2:A2&$B$2:B2,A2&B2),"00"),這個公式會將職位名稱和編號連接起來,並且使用 TEXT 函數將編號格式化為兩位數。這個公式也會使用 COUNTIF 函數計算相同中心和職位的出現次數,作為編號的依據。
-將 C2 儲存格向下填滿,即可得到各中心自己職位的編號。
例如,如果 A2:B6 的資料是:
CENTER A WORKMAN
CENTER A WORKMAN
CENTER B MANAGER
CENTER B WORKMAN
CENTER B MANAGER
那麼 C2:C6 的結果會是:
WORKMAN 01
WORKMAN 02
MANAGER 01
WORKMAN 01
MANAGER 02
作者: tragamer 時間: 2024-3-4 07:29
$A$2:A2&$B$2:B2
可能出唔到整體
只出1個,
作者: jimking332 時間: 2024-3-4 10:10
我見你expect個答案入面, WORKMAN01出現左兩次, 即係每間中心, MANAGER同WORKMAN GRADE都有自己既流水號, 即係要睇CENTER同職位兩個FACTOR, 就要用COUNTIFS
https://h2.hkepc.com/forum/attachment.php?aid=2413398&k=47fd7b4776dc98c872b5287f2df57f6f&t=1781451994&sid=crJHEUCwZO

作者: s20012797 時間: 2024-3-5 09:44
一行解決:
=B2&" "&TEXT(COUNTIFS($A$2:A$6, A2, $B$2:B$6, B2),"00")
VBA大法好:
Sub Macro1()
Set ws = ThisWorkbook.Worksheets("Sheet1") ' Change to the sheet you want to work with
lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row ' Find the last row with data in column A
For i = 2 To lastRow ' Loop through each row in the data
center = ws.Cells(i, "A").Value ' Get the center name
position = ws.Cells(i, "B").Value ' Get the position name
count = Application.WorksheetFunction.CountIfs(ws.Range("A2:A" & lastRow), center, ws.Range("B2:B" & lastRow), position) ' Count the number of occurrences of the center and position
ws.Cells(i, "C").Value = center & " " & position & " " & Format(count, "00") ' Output the center, position, and count
Next i
End Sub

