我正在尝试为员工工作的天数创建布尔值。下面是我的示例代码:
Sub BooleabAtt2()
'Create boolean entries for days worked
'Sign-on Worksheet
Dim ws1 As Worksheets
Set ws = Worksheets("Sign-ons")
Dim rng1 As Range
Set rng1 = ws1.Range("SignOns")
Dim EmployeeName1 As Long
EmployeeName1 = rng1.Columns("", 2)
Dim DateIn1 As Date
DateIn1 = rng1.Columns("", 6)
Dim Company1 As Long
Company1 = rng1.Colums("", 3)
Dim RowCount As Long
Dim LastRow As Long
Dim i As Long
'Employee ID Worksheet
Dim ws2 As Worksheets
Set ws2 = Worksheets("EmployeeID")
'ConsecutiveDays worksheet
Dim ws3 As Worksheets
Set ws3 = Worksheets("Consecutive-Days")
Dim rng3 As Range
Set rng3 = ws3.Range("ConsecutiveDays")
Dim EmployeeName
'conditional statement
With rng1
LastRow = .Colums(2).Rows.Count
For i = LastRow
If ws3.rng3.Cells(1, i) = DateIn1 Then
If ws3.rng3.Cells(i, 2) = EmployeeName1 Then Enter 1
Otherwise "0"
End If
End If
Next i
End With
End Sub下面是一些示例数据
下表是我正在努力实现的目标:
发布于 2021-01-05 11:34:02
我同意这可以很容易地通过公式来完成。首先,要创建唯一员工列表,请转到data>删除重复项,或者使用this之类的公式。如果你有最新版本的excel,我想也有一个独特的公式。
然后使用公式=COUNTIFS($F$2:$F$11,B$1,$E$2:$E$11,$A2)

发布于 2021-01-05 13:59:53
这里是使用VBA的一个尝试。
源表(名为"timesheet")

Public Function CreateMyPivotTable()
''' Source, destination, and pivot table name variables. '''
''' We're using Sheet1 and Sheet2 for our worksheets, but you
''' could probably add those as a parameter.
Dim source_worksheet_name As String: source_worksheet_name = "Sheet1"
Dim source_tablename As String: source_tablename = "timesheet"
Dim destination_worksheet_name As String: destination_worksheet_name = "Sheet2"
Dim pivot_table_name As String: pivot_table_name = "PivotTimeSheet"
Dim source_worksheet As Worksheet
Dim destination_worksheet As Worksheet
''' Create source and destination worksheet objects.
Set source_worksheet = ActiveWorkbook.Worksheets(source_worksheet_name)
Set destination_worksheet = ActiveWorkbook.Worksheets(destination_worksheet_name)
''' Create source and pivot table objects
Dim src_tbl As ListObject
Dim pivot_tbl As PivotTable
''' Focus on destinaton worksheet.
source_worksheet.Select
''' Set source table variable.
Set src_tbl = source_worksheet.ListObjects(source_tablename)
''' Clear out pivot table (function is below.
Call RemovePivotTable(pivot_table_name)
''' Generate pivotcache using our table source.
With ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=src_tbl.Range.Address(False, False, xlA1, xlExternal))
''' Create pivot table at A1 in the destination worksheet.
Call .CreatePivotTable(TableDestination:=destination_worksheet.Range("A1"), tablename:=pivot_table_name)
End With
''' Focus on destinaton worksheet.
destination_worksheet.Select
''' Set EmployeeName and Date in as Row and Column fields
''' Use count of Date in per EmployeeName as value.
With destination_worksheet.PivotTables(pivot_table_name)
''' Miss me with that Grand Total stuff
.RowGrand = False
.ColumnGrand = False
''' Fill any missing values with Zero.
.NullString = "0"
''' General pivot action. Pretty self explanatory.
With .PivotFields("EmployeeName")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Date in")
.Orientation = xlColumnField
.Position = 1
End With
''' Data field section
With .PivotFields("Date in")
.Orientation = xlDataField
.Function = xlCount ' Count dates per employee
.NumberFormat = "0" ' General number format.
End With
End With
''' Clear objects before leaving.
Set source_worksheet = Nothing
Set destination_worksheet = Nothing
Set src_tbl = Nothing
Set pivot_tbl = Nothing
End Function
Public Function RemovePivotTable(pivot_table_name As String)
''' Delete a pivot table by name.
''' Ex. usage -
''' RemovePivotTable("PivotTimeSheet")
If Len(pivot_table_name) > 0 Then
''' In general, iterate over each worksheet,
''' look for a name match
''' then clear out that pivot table.
Dim pvttbl As PivotTable
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
For Each pvttbl In ws.PivotTables
If pvttbl.Name = pivot_table_name Then
pvttbl.TableRange2.Clear
End If
Next pvttbl
Next ws
Set pvttbl = Nothing
Set ws = Nothing
End If
End Function输出(在Sheet2上):

编辑:在每个透视单元格中获得1计数的最佳方法是删除表中的重复值。这可以手动完成,但我录制了一个生成以下内容的宏:
ActiveSheet.Range("timesheet[#All]").RemoveDuplicates Columns:=Array(2, 3), Header:=xlYes(请记住,跳过No.列,否则将不会删除任何行,因为所有这些值都是唯一的。)
一旦完成,这将是您的输出:

https://stackoverflow.com/questions/65572358
复制相似问题