首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有一个VBA函数可以为员工工作的天数创建布尔值?

是否有一个VBA函数可以为员工工作的天数创建布尔值?
EN

Stack Overflow用户
提问于 2021-01-05 10:18:37
回答 2查看 46关注 0票数 0

我正在尝试为员工工作的天数创建布尔值。下面是我的示例代码:

代码语言:javascript
复制
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

下面是一些示例数据

Excel sample data

下表是我正在努力实现的目标:

Final worksheet

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-05 11:34:02

我同意这可以很容易地通过公式来完成。首先,要创建唯一员工列表,请转到data>删除重复项,或者使用this之类的公式。如果你有最新版本的excel,我想也有一个独特的公式。

然后使用公式=COUNTIFS($F$2:$F$11,B$1,$E$2:$E$11,$A2)

票数 0
EN

Stack Overflow用户

发布于 2021-01-05 13:59:53

这里是使用VBA的一个尝试。

源表(名为"timesheet")

代码语言:javascript
复制
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计数的最佳方法是删除表中的重复值。这可以手动完成,但我录制了一个生成以下内容的宏:

代码语言:javascript
复制
ActiveSheet.Range("timesheet[#All]").RemoveDuplicates Columns:=Array(2, 3), Header:=xlYes

(请记住,跳过No.列,否则将不会删除任何行,因为所有这些值都是唯一的。)

一旦完成,这将是您的输出:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65572358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档