Copy name, project number, hours total from all employees timesheets in a directory vba

jimmygogo

New Member
Joined
Mar 29, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi there, I am looking for help in writing a code where I can copy employee name, project numbers and total hours worked on each project per week from employee directory. Each employee has their own excel file.

1648580216567.png




Each week (separate tab for each week) hundreds of employee fill in their time sheet (example) below. For my purpose, I want the code to open each workbook, copy name (C8), Project numbers (B13 to end) and total hours (N13 to end) and then move on to the next employee.

1648580557275.png



Employee directory

1648580427785.png




Thank you in advance for your help.
 

Attachments

  • 1648580316486.png
    1648580316486.png
    74.5 KB · Views: 8
  • 1648580546247.png
    1648580546247.png
    53 KB · Views: 11
I have something for you to try out and give feedback on. There is two code blocks. The first is to process the Userform:
VBA Code:
Option Explicit

Private Sub CommandButton1_Click()
  If Not IsDate(TextBox1.Text) Then
      MsgBox "Date required"
  Else
    If Weekday(TextBox1.Text) <> vbSunday Then  ' check if date is Sunday
      MsgBox "Date must be Sunday"
    Else
      Me.Hide
    End If
  End If
End Sub

Private Sub CommandButton2_Click()
  TextBox1.Value = ""
  Me.Hide
End Sub

The second should be in a standard code module:
VBA Code:
Option Explicit

Sub Main()
  Dim FileList As Collection, FileName As Variant, strFileName As String, EmployeeWB As Workbook
  Dim ProcessDate As Date, SummaryWS As Worksheet
  Dim srcEmployeeName As String, srcProjectNumber As String, srcProjectHours As String
  Dim EmployeeWS As Worksheet, EmployeeName As String, HourCell As Range, LastRow As Long
 
  srcEmployeeName = "$C$8"
  srcProjectNumber = "$B$13:$B$35"
  srcProjectHours = "$N$13:$N$35"
 
  UserForm1.Show
  If IsDate(UserForm1.TextBox1.Value) Then
    ProcessDate = UserForm1.TextBox1.Value
  Else
    Unload UserForm1
    Exit Sub
  End If
  Unload UserForm1
 
  Set SummaryWS = ActiveSheet
' find last row
  LastRow = GetLastRow(SummaryWS)
 
'  Set FileList = New Collection
' get all file names from employee folder
  Set FileList = GetAllFilenames("c:\TestFolder\*.xl*")
 
  If FileList.Count > 0 Then
    For Each FileName In FileList
      strFileName = "c:\TestFolder\" & CStr(FileName)
  '   open workbook read-only
      Set EmployeeWB = Workbooks.Open(strFileName, , True)
      For Each EmployeeWS In EmployeeWB.Worksheets
        If IsDate(EmployeeWS.Name) Then
          If ProcessDate = CDate(EmployeeWS.Name) Then
            EmployeeName = EmployeeWS.Range(srcEmployeeName).Value
            For Each HourCell In EmployeeWS.Range(srcProjectHours).Cells
              If HourCell.Value > 0 Then
'   copy cells from employee file to this file
                With SummaryWS
                  .Cells(LastRow + 1, 1).Value = EmployeeName
                  .Cells(LastRow + 1, 2).Value = EmployeeWS.Cells(HourCell.Row, 2)
                  .Cells(LastRow + 1, 3).Value = HourCell.Value
                End With
                LastRow = LastRow + 1
              Else
              End If
            Next HourCell
          Else
          End If
        Else
        End If
      Next EmployeeWS
  '   close employee file without saving
      EmployeeWB.Close False
    Next FileName
  End If
End Sub

Function GetAllFilenames(inPath As String) As Collection
  Dim StrFile As String, FileList As Collection
  Set FileList = New Collection
  StrFile = Dir(inPath)
  Do While Len(StrFile) > 0
    FileList.Add StrFile
    StrFile = Dir
  Loop
  Set GetAllFilenames = FileList
End Function

Function GetLastRow(sh As Worksheet) As Long
'Ron de Bruin, 5 May 2008
  On Error Resume Next
  GetLastRow = sh.Cells.Find(What:="*", After:=sh.Range("A1"), Lookat:=xlPart, _
                          LookIn:=xlFormulas, SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, MatchCase:=False).Row
  On Error GoTo 0
End Function

There is no code to trigger the actual gathering data from the employee sheets. I would suggest on the Summary sheet you have a button that links to the "Main" procedure.

I created a Userform to get the date to process. Ask if you need help with that.

let me know how it works or if you would like explanations of the code.
Hey disregard my last message, I found the error and was able to fix. The code ran and everything but no data was copied from the employee worksheets. Do you know what am I doing wrong ?
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
in the code module "Main" there is a line:

Set FileList = GetAllFilenames("c:\TestFolder\*.xl*")

You need to change the parameter "c:\TestFolder\*.xl*" needs to be changed to the drive and folder that contains the employee workbooks. For example if the employee workbooks are in a folder on the G: drive in a folder called "EmployeeTimesheets" then the line should read:

Set FileList = GetAllFilenames("g:\EmployeeTimesheets\*.xl*")

The same thing should be done on the line 3 lines below.

Make sure that is done and try it again.
 
Upvote 0
in the code module "Main" there is a line:

Set FileList = GetAllFilenames("c:\TestFolder\*.xl*")

You need to change the parameter "c:\TestFolder\*.xl*" needs to be changed to the drive and folder that contains the employee workbooks. For example if the employee workbooks are in a folder on the G: drive in a folder called "EmployeeTimesheets" then the line should read:

Set FileList = GetAllFilenames("g:\EmployeeTimesheets\*.xl*")

The same thing should be done on the line 3 lines below.

Make sure that is done and try it again.
Yeah, I did update the link to my folder. I can see the code opening the worksheets but no data is getting transferred.

C:\Users\akhan\Desktop\Source\ - link to the files

employees files are saved in the folder.

1649168492789.png
 
Upvote 0
Do you know how to use the V BA Immediate window and the single-step function to trace execution? I want to determine if the how the names of the worksheets in the employee files are being interpretted. It may be that there is a hidden difference that we need to manage.
 
Upvote 0
Can you upload to Dropbox. com or Box.com or some other similar place 3 or 4 of the employee files so I can run my version against them to help find the problem?
 
Upvote 0
Can you upload to Dropbox. com or Box.com or some other similar place 3 or 4 of the employee files so I can run my version against them to help find the problem?
I can email it to you or I can add it to dropbox as well. Let me know what you prefer ?
 
Upvote 0
Please add it to dropbox & provide a link to the shared workbook.
All file sharing & communication must remain on the board.
Thanks
 
Upvote 0
I figured out that one problem is that I am in USA and input dates as mmddyyyy. You appear to be in UK (or at least not in USA) and use dates as ddmmyyyy. I need to figure how to know which format to use. From my searches, this is a common and not very well solved problem. I will try to determine that solution.
 
Upvote 0
jimmygogo here is an updated version of the code for the standard module:
VBA Code:
Option Explicit

Sub Main()
  Dim FileList As Collection, FileName As Variant, strFileName As String, EmployeeWB As Workbook
  Dim ProcessDate As Date, SummaryWS As Worksheet
  Dim srcEmployeeName As String, srcProjectNumber As String, srcProjectHours As String
  Dim EmployeeWS As Worksheet, EmployeeName As String, HourCell As Range, LastRow As Long
  Dim TestDateString As String
  
  srcEmployeeName = "$C$7"
  srcProjectNumber = "$B$13:$B$35"
  srcProjectHours = "$N$13:$N$35"
  
  UserForm1.Show
  If IsDate(UserForm1.TextBox1.Value) Then
'
' THIS MAY NEED TO CHANGE IF THE INPUT IS IN DD-MM-YYYY FORMAT
'
' this converts the mm-dd-yyyy input to dd-mm-yyyy format for comparing
'
    TestDateString = Format(CDate(UserForm1.TextBox1.Value), "dd-mm-yyyy")
  Else
    Unload UserForm1
    Exit Sub
  End If
  Unload UserForm1
  
  Set SummaryWS = ActiveSheet
' find last row
  LastRow = GetLastRow(SummaryWS)
  
'  Set FileList = New Collection
' get all file names from employee folder
  Set FileList = GetAllFilenames("c:\TestFolder\*.xl*")
  
  If FileList.Count > 0 Then
    For Each FileName In FileList
      strFileName = "c:\TestFolder\" & CStr(FileName)
  '   open workbook read-only
      Set EmployeeWB = Workbooks.Open(strFileName, , True)
      For Each EmployeeWS In EmployeeWB.Worksheets
        If IsDate(EmployeeWS.Name) Then
          If TestDateString = EmployeeWS.Name Then
            EmployeeName = EmployeeWS.Range(srcEmployeeName).Value
            For Each HourCell In EmployeeWS.Range(srcProjectHours).Cells
              If HourCell.Value > 0 Then
'   copy cells from employee file to this file
                With SummaryWS
                  .Cells(LastRow + 1, 1).Value = EmployeeName
                  .Cells(LastRow + 1, 2).Value = EmployeeWS.Cells(HourCell.Row, 2)
                  .Cells(LastRow + 1, 3).Value = HourCell.Value
                End With
                LastRow = LastRow + 1
              Else
              End If
            Next HourCell
          Else
          End If
        Else
        End If
      Next EmployeeWS
  '   close employee file without saving
      EmployeeWB.Close False
    Next FileName
  End If
End Sub

Function GetAllFilenames(inPath As String) As Collection
  Dim StrFile As String, FileList As Collection
  Set FileList = New Collection
  StrFile = Dir(inPath)
  Do While Len(StrFile) > 0
    FileList.Add StrFile
    StrFile = Dir
  Loop
  Set GetAllFilenames = FileList
End Function

Function GetLastRow(sh As Worksheet) As Long
'Ron de Bruin, 5 May 2008
  On Error Resume Next
  GetLastRow = sh.Cells.Find(What:="*", After:=sh.Range("A1"), Lookat:=xlPart, _
                          LookIn:=xlFormulas, SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, MatchCase:=False).Row
  On Error GoTo 0
End Function

Please note the comments near the top of this code!

If you always enter the date required in the mm-dd-yyyy format this code should work all the time. If you always enter the date required in the dd-mm-yyyy format this code will work sometimes but not always. This caution is based on the fact I am testing in the US. A complete testing should be done in the UK (or where you are) for multiple dates. For example, I tested 1-9-2022 and 2-20-2022. Then I tested 9-1-2022 and 20-2-2022. the 9-1-2022 whould not work, the other three worked properly.

Let me know how this works.
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top