Hi there!
I am struggling with this project, to have working it.
I have the first sheet where I fill in the hours and choose the project and date. And I have here the update button, which is a macro-related. When this is pressed, then on the next sheet all these data are aligned, and the hours are copied to this sheet right position and deleted from the first one. Some extra check are in the code.
Second sheet:
And the code:
But somehow I can not guess how to make it to align with the projects itself.
Somebody help me please to fix this issue.
Thanks!
I am struggling with this project, to have working it.
I have the first sheet where I fill in the hours and choose the project and date. And I have here the update button, which is a macro-related. When this is pressed, then on the next sheet all these data are aligned, and the hours are copied to this sheet right position and deleted from the first one. Some extra check are in the code.
Project 2 | 02/01/2024 | Person | Working Hours | |
Name 1 | ||||
Name 2 | ||||
Name 3 | ||||
Name 4 | ||||
Name 5 | ||||
Name 6 | ||||
Select project | Select date | Fill hours | ||
Second sheet:
Innovation project loadings_macro.xlsm | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | |||
1 | Grand total hours/project | Column1 | Column2 | Column3 | Column4 | Column5 | Column6 | Column7 | Column8 | Column9 | Column10 | Column11 | Column12 | Column13 | Column14 | Column15 | ||
2 | 01/01/2024 | 02/01/2024 | 03/01/2024 | 04/01/2024 | 05/01/2024 | 06/01/2024 | 07/01/2024 | 08/01/2024 | 09/01/2024 | 10/01/2024 | 11/01/2024 | 12/01/2024 | 13/01/2024 | 14/01/2024 | ||||
4 | ||||||||||||||||||
5 | 0 | Project 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
6 | 0 | Name 1 | ||||||||||||||||
7 | 0 | Name 2 | ||||||||||||||||
8 | 0 | Name 3 | ||||||||||||||||
9 | 0 | Name 4 | ||||||||||||||||
10 | 0 | Name 5 | ||||||||||||||||
11 | 0 | Name 6 | ||||||||||||||||
12 | 0 | Project 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
13 | 0 | Name 1 | ||||||||||||||||
14 | 0 | Name 2 | ||||||||||||||||
15 | 0 | Name 3 | ||||||||||||||||
16 | 0 | Name 4 | ||||||||||||||||
17 | 0 | Name 5 | ||||||||||||||||
18 | 0 | Name 6 | ||||||||||||||||
19 | 0 | Project 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
20 | 0 | Name 1 | ||||||||||||||||
21 | 0 | Name 2 | ||||||||||||||||
22 | 0 | Name 3 | ||||||||||||||||
23 | 0 | Name 4 | ||||||||||||||||
24 | 0 | Name 5 | ||||||||||||||||
25 | 0 | Name 6 | ||||||||||||||||
26 | 0 | Project 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
27 | 0 | Name 1 | ||||||||||||||||
28 | 0 | Name 2 | ||||||||||||||||
29 | 0 | Name 3 | ||||||||||||||||
30 | 0 | Name 4 | ||||||||||||||||
31 | 0 | Name 5 | ||||||||||||||||
32 | 0 | Name 6 | ||||||||||||||||
33 | 0 | Project 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
34 | 0 | Name 1 | ||||||||||||||||
35 | 0 | Name 2 | ||||||||||||||||
36 | 0 | Name 3 | ||||||||||||||||
37 | 0 | Name 4 | ||||||||||||||||
38 | 0 | Name 5 | ||||||||||||||||
39 | 0 | Name 6 | ||||||||||||||||
40 | 0 | Project 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
41 | 0 | Name 1 | ||||||||||||||||
42 | 0 | Name 2 | ||||||||||||||||
43 | 0 | Name 3 | ||||||||||||||||
44 | 0 | Name 4 | ||||||||||||||||
45 | 0 | Name 5 | ||||||||||||||||
46 | 0 | Name 6 | ||||||||||||||||
47 | 0 | Project 7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
48 | 0 | Name 1 | ||||||||||||||||
49 | 0 | Name 2 | ||||||||||||||||
50 | 0 | Name 3 | ||||||||||||||||
51 | 0 | Name 4 | ||||||||||||||||
52 | 0 | Name 5 | ||||||||||||||||
53 | 0 | Name 6 | ||||||||||||||||
54 | 0 | Project 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
55 | 0 | Name 1 | ||||||||||||||||
56 | 0 | Name 2 | ||||||||||||||||
57 | 0 | Name 3 | ||||||||||||||||
58 | 0 | Name 4 | ||||||||||||||||
59 | 0 | Name 5 | ||||||||||||||||
60 | 0 | Name 6 | ||||||||||||||||
61 | 0 | Project 9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
62 | 0 | Name 1 | ||||||||||||||||
63 | 0 | Name 2 | ||||||||||||||||
64 | 0 | Name 3 | ||||||||||||||||
65 | 0 | Name 4 | ||||||||||||||||
66 | 0 | Name 5 | ||||||||||||||||
67 | 0 | Name 6 | ||||||||||||||||
68 | 0 | Project 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
69 | 0 | Name 1 | ||||||||||||||||
70 | 0 | Name 2 | ||||||||||||||||
71 | 0 | Name 3 | ||||||||||||||||
72 | 0 | Name 4 | ||||||||||||||||
73 | 0 | Name 5 | ||||||||||||||||
74 | 0 | Name 6 | ||||||||||||||||
75 | 0 | Project 11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
76 | 0 | Name 1 | ||||||||||||||||
77 | 0 | Name 2 | ||||||||||||||||
78 | 0 | Name 3 | ||||||||||||||||
79 | 0 | Name 4 | ||||||||||||||||
80 | 0 | Name 5 | ||||||||||||||||
81 | 0 | Name 6 | ||||||||||||||||
82 | 0 | Project 12 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
83 | 0 | Name 1 | ||||||||||||||||
84 | 0 | Name 2 | ||||||||||||||||
85 | 0 | Name 3 | ||||||||||||||||
86 | 0 | Name 4 | ||||||||||||||||
87 | 0 | Name 5 | ||||||||||||||||
88 | 0 | Name 6 | ||||||||||||||||
89 | 0 | Project 13 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
90 | 0 | Name 1 | ||||||||||||||||
91 | 0 | Name 2 | ||||||||||||||||
92 | 0 | Name 3 | ||||||||||||||||
93 | 0 | Name 4 | ||||||||||||||||
94 | 0 | Name 5 | ||||||||||||||||
95 | 0 | Name 6 | ||||||||||||||||
96 | 0 | Project 14 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
97 | 0 | Name 1 | ||||||||||||||||
98 | 0 | Name 2 | ||||||||||||||||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
C5:P5,C96:P96,C89:P89,C82:P82,C75:P75,C68:P68,C61:P61,C54:P54,C47:P47,C40:P40,C33:P33,C26:P26,C19:P19,C12:P12 | C5 | =SUBTOTAL(9,C6:C11) |
A5:A98 | A5 | =SUM(C5:ND5) |
Cells with Data Validation | ||
---|---|---|
Cell | Allow | Criteria |
B5 | List | =Sources!$B$2:$B$27 |
B89 | List | =Sources!$B$2:$B$27 |
B82 | List | =Sources!$B$2:$B$27 |
B75 | List | =Sources!$B$2:$B$27 |
B68 | List | =Sources!$B$2:$B$27 |
B61 | List | =Sources!$B$2:$B$27 |
B54 | List | =Sources!$B$2:$B$27 |
B47 | List | =Sources!$B$2:$B$27 |
B40 | List | =Sources!$B$2:$B$27 |
B33 | List | =Sources!$B$2:$B$27 |
B26 | List | =Sources!$B$2:$B$27 |
B19 | List | =Sources!$B$2:$B$27 |
B12 | List | =Sources!$B$2:$B$27 |
B96 | List | =Sources!$B$2:$B$27 |
And the code:
VBA Code:
Sub UpdateHours()
Dim wsData As Worksheet
Dim wsSheet1 As Worksheet
Dim projectName As String
Dim selectedDate As Date
Dim personNames As Range
Dim workingHours As Range
Dim lastRowData As Long
Dim i As Long
Dim foundProject As Range
Dim foundDate As Range
Dim foundPerson As Range
Dim selectedProjectCell As Range
Dim overwriteData As VbMsgBoxResult
Dim projectRow As Long
Dim dateColumn As Long
' Set references to sheets
Set wsData = ThisWorkbook.Sheets("Data")
Set wsSheet1 = ThisWorkbook.Sheets("Sheet1")
' Get user input
projectName = Trim(wsData.Range("A1").Value)
' Check if date is a valid date
On Error Resume Next
selectedDate = CDate(wsData.Range("B1").Value)
On Error GoTo 0
' Find the last row in Data sheet
lastRowData = wsData.Cells(wsData.Rows.Count, "C").End(xlUp).Row
' Find the project row in Sheet1
Set foundProject = wsSheet1.Columns("B").Find(What:=projectName, LookIn:=xlValues, LookAt:=xlWhole)
' Check if project is found
If foundProject Is Nothing Then
MsgBox "Project not found in Sheet1!", vbExclamation
Exit Sub
End If
' Find the date column in Sheet1
Set foundDate = wsSheet1.Rows(2).Find(What:=selectedDate, LookIn:=xlValues, LookAt:=xlWhole)
' Check if date is found
If foundDate Is Nothing Then
MsgBox "Selected date not found in Sheet1! Selected Date: " & selectedDate, vbExclamation
Exit Sub
End If
' Check if the selected project cell in Sheet1 contains data
Set selectedProjectCell = wsSheet1.Cells(foundProject.Row, foundDate.Column)
If Not IsEmpty(selectedProjectCell.Value) Then
' Prompt to confirm overwriting data
overwriteData = MsgBox("The selected project contains data. Would you like to overwrite?", vbQuestion + vbYesNo, "Confirmation")
If overwriteData = vbNo Then
Exit Sub ' User chose not to overwrite, exit sub
End If
End If
' Get the project row in Sheet1
projectRow = foundProject.Row
' Get the date column in Sheet1
dateColumn = foundDate.Column
' Copy data to Sheet1
Set personNames = wsData.Range("C2:C" & lastRowData)
Set workingHours = wsData.Range("D2:D" & lastRowData)
' Copy numbers to Sheet1
For i = 1 To personNames.Rows.Count
If personNames.Cells(i, 1).Value <> "" Then
' Find the person in Sheet1 for the selected project
Set foundPerson = wsSheet1.Columns("B").Find(What:=personNames.Cells(i, 1).Value, LookIn:=xlValues, LookAt:=xlWhole)
' Check if person is found
If foundPerson Is Nothing Then
MsgBox "Person not found in Sheet1 for Project: " & projectName & " - Person: " & personNames.Cells(i, 1).Value, vbExclamation
Exit Sub
End If
' Copy working hours to Sheet1
wsSheet1.Cells(foundPerson.Row, dateColumn).Value = IIf(workingHours.Cells(i, 1).Value = "", 0, workingHours.Cells(i, 1).Value)
End If
Next i
' Clear only working hours in Data sheet
wsData.Range("D2:D" & lastRowData).ClearContents
End Sub
But somehow I can not guess how to make it to align with the projects itself.
Somebody help me please to fix this issue.
Thanks!