vba find function assistance

jiddings

Board Regular
Joined
Nov 22, 2008
Messages
135
I have a spreadsheet that I need to collect data and copy it to a separate spreadsheet.
The source spreadsheet is configured as follows
Column A: Column B:
Employee: Employee clock #
Department: Department A
Reg Pay: daily hrs Sun - Sat(column B - Column H)
Department: Department B
Reg Pay: daily hrs Sun - Sat(column B - Column H)
Department: Department C
Reg Pay: daily hrs Sun - Sat(column B - Column H)
Employee: Employee clock #
Department: Department A
Reg Pay: daily hrs Sun - Sat(column B - Column H)
Department: Department B
Reg Pay: daily hrs Sun - Sat(column B - Column H)
Department: Department C
Reg Pay: daily hrs Sun - Sat(column B - Column H)

This repeats for approx. 60 employees.

As the employees are a shared resource between departments, not all employees work for each department each week and some work in only one department for a given week. Thus, other department designations will not appear for a given employee and will only appear if the employee works for that department during that week.
I've used the vba find function to locate each employee's clock #. I'm having difficulity with vba to locate Department "B" for each employee and I'm attempting to utilize the find function. Any recommendations on vba code to do this would be appreciated.
 
The error is likely occurring because the '.select' method will only work if the range's parent worksheet is the active worksheet. For that reason and also because most every thing can be done without the '.select' method, avoid using it unless you want a particular range to be selected at the end of the code.

If you are trying to copy/format individual cells in a general manner you would have to replacing the resize code and do something along the lines of the following.

Code:
    DeptRefCell.Offset(k, 1).Copy Destination:=TargetWS1.Cells(outLR, 2) '//Copy Col B to B
    TargetWS1.Cells(outLR, 2).Interior.Color = RGBCategories(j)   

    DeptRefCell.Offset(k, 2).Copy Destination:=TargetWS1.Cells(outLR, 4) '//Copy Col C to D
    TargetWS1.Cells(outLR, 4).Interior.Color = RGBCategories(j)

    DeptRefCell.Offset(k, 3).Copy Destination:=TargetWS1.Cells(outLR, 7) '//Copy Col D to G 
    TargetWS1.Cells(outLR, 7).Interior.Color = RGBCategories(j)
    ...
    ...
    ...
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I just realized I had not previously posted the structure of the target worksheets.
Here is the structure. It's the same for the reg pay hrs also.



Excel 2007
ABCDEFGHIJ
1Holiday/Breavement/Jury Duty/Floating Holiday/Vacation
22014Holiday Pay
3Bereavement
4Jury Duty
5Floating Holiday
6Vacation
7Bonus Vacation Day
8Go to Holiday SummaryStatusRPTRPTRPTRPTRPTRPT
9Do not chg this lineClock #305120973503398027511348
10Week EndingDay of WeekTOTAL Hours% of TotalXXXXXXXXXXXWWWWWRRRRRYYYYYYZZZZZZZ
1129-DecSun0.000%000000
1230-DecMon0.000%000000
1331-DecTue0.000%000000
141-JanWed0.000%000000
152-JanThu0.000%000000
163-JanFri0.000%000000
174-JanSat0.000%000000
18Total Week0.00000000
19Year To Date0.000.000.000.000.000.000.00
20
215-JanSun0.000%000000
226-JanMon0.000%000000
237-JanTue0.000%000000
248-JanWed0.000%000000
259-JanThu0.000%000000
2610-JanFri0.000%000000
2711-JanSat0.000%000000
28Total Week0.00000000
29Year To Date0.00000000
30
3112-JanSun0.000%000000
3213-JanMon0.000%000000
Holidays - Vacations
 
Upvote 0
Considering the most recently posted target worksheet structure, I'm not understanding how your suggestion(s) would work wen each days hrs. could be different categories. I think your suggestion would result changing the cells for the whole week.
My thought would be a for / next loop to copy from source worksheet row to each days in the target worksheet column. Would that be something that would work?
 
Upvote 0
You're right it won't work and some of the other previous code won't work for this target worksheet.

You're correct an additional for loop would be required to loop through the days. The loop would have to contain an if statement copy/formatting when a non-zero hour is detected. This assume that only one category of "Holiday" can be used on a given day.

In addition the output reference would have to be adjusted, which also means the current loops would probably have to be changed too. The simplest part of that would be determining the output column for a given "Clock#" assuming they all exist on the output sheet. Finding the correct week on the output would be fairly simple assuming it exists on the output sheet.

Let me know if my assumptions are correct.
 
Upvote 0
Here's a small sub I was thinking could possibly be applied.

Code:
Sub xxx()
For k = 0 To 6
With ActiveSheet
Cells(1, (3 + k)).Copy
If Cells(1, (3 + k)) = 0 Then
'do nothing
ElseIf Cells((1 + k), 1) <> 0 Then
'myvalue = Application.WorksheetFunction.Sum((Cells(1, (3 + k))), (Cells((1 + k), 1)))
'Add cell comment when value previously exists and change cell color
        existval = Cells((1 + k), 1).Value
        addval = Cells(1, (3 + k)).Value
        Cells((1 + k), 1) = Application.WorksheetFunction.Sum((Cells(1, (3 + k))), (Cells((1 + k), 1)))
        Cells((1 + k), 1).ClearComments
        Cells((1 + k), 1).AddComment.Text Text:=Format(Str(Now), "mmm-dd-yy hh:mm:ss") & vbCrLf & "Two Values:" & vbCrLf _
        & "Existing value = " & Str(existval) & vbCrLf & "Added value = " & Str(addval)
        Cells((1 + k), 1).Interior.Color = RGB(86, 220, 198) 'changed value color
        Cells((1 + k), 1).Font.Color = vbWhite 'font color
        Cells((1 + k), 1).Font.Bold = True 'font color bold
ElseIf Cells((1 + k), 1) < 1 Then
Cells((1 + k), 1).PasteSpecial xlPasteValues
End If

'/// need code for changing cell color for each of holidays
'If Cells((1 + k), 1).Value <> 0 Then
''Cells((1 + k), 1).Interior.Color = RGB(255, 0, 255) 'holiday color
''Cells((1 + k), 1).Interior.Color = RGB(255, 153, 204) 'floating holiday color
''Cells((1 + k), 1).Interior.Color = RGB(51, 102, 255) 'vacation color
'Cells((1 + k), 1).Interior.Color = RGB(153, 102, 255) 'bonus vac day color
'Cells((1 + k), 1).Font.Color = vbWhite 'font color
'Cells((1 + k), 1).Font.Bold = True 'font color
'End If
End With
Next k
Range("a1").Select
Application.CutCopyMode = False
End Sub
 
Upvote 0
Your assumptions are correct. My thought is to have the code "look" at the cell that the cell it is about to post a value into, add the two values and a comment with a unusual cell color to "flag" it. Otherwise, set the color for vacation, holiday, etc. I think the previous For / Next VBA might be a good start?
 
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

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