VBA Formula instead of hard value

JaCharger

New Member
Joined
Oct 7, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm working on building a script to modify a time tracking sheet to fit what we need as far as formatting. I'm stuck on this last part and that is changing salaried people's time to equal 70 hours. Our old software automatically did it, but the one we are using now does not.

I've settled on using Solver to make this work. The issue is, some code that I found (that will automatically add up times based on if values in column 1 match) only places the hard value into the sheet, rather than a sum formula - what I need for Solver to work.

Here is the modified code that allows this to happen.

VBA Code:
Private Sub SalaryTotals()
Dim wb As Workbook, og As Workbook
Dim ws1 As Worksheet
Dim lRow As Long, i As Long, endRow As Long
Dim CopyRange As String, PasteRange As String, searchValue As String

Set ws1 = Worksheets("Sheet1")
Set os = Worksheets("Sheet1")

lRow = os.Cells(Rows.Count, 1).End(xlUp).Row

With ws1.Range("H2:H" & lRow + 4)
    .ClearContents
    .Value = ws1.Evaluate("INDEX(SUMIF(A2:A" & lRow + 4 & ",A2:A" & lRow + 4 & ",C2:C" & lRow + 4 & "),)")
End With

 i = 5
    For i = 5 To lRow + 4
        If ws1.Cells(i, 7).Value = ws1.Cells(i, 8).Value Then
            ws1.Cells(i, 8).Value = ""
        End If
    Next i

    i = 5
    For i = 5 To lRow + 4
        If ws1.Cells(i, 8).Value = ws1.Cells(i + 1, 8).Value Then
            ws1.Cells(i, 8).Value = ""
        End If
    Next i
    Let PasteRange = "H2:H" & lRow + 4
End Sub

And here is the mini-sheet.

VIP Processing Tool.xlsm
ABCDEFGHIJ
1EmpNoPaycodeCodeHoursRateFlat AmountSalaryFirst IgnoredSecond IgnoredShift DiffWorkDate
24638R3.876.409/12/22
34638R3.576.409/12/22
44638R7.476.409/13/22
54638V709/14/22
64638V709/15/22
74638V709/16/22
84638R3.809/19/22
94638R3.509/19/22
104638R109/19/22
114638R3.909/20/22
124638R3.609/20/22
134638R109/21/22
144638R3.809/21/22
154638R3.509/21/22
164638R109/22/22
174638R3.809/22/22
184638R3.509/22/22
194638R109/23/22
204638R3.809/23/22
214638R3.576.409/23/22
221451R5.109/12/22
231451R1.909/12/22
241451R3.809/13/22
251451R3.309/13/22
261451R609/14/22
271451R409/15/22
281451R309/15/22
291451R209/16/22
301451R1.609/16/22
311451R1.509/16/22
321451R3.509/16/22
331451R0.909/16/22
341451R4.509/19/22
351451R2.509/19/22
361451R3.809/20/22
371451R3.209/20/22
381451R3.509/21/22
391451R3.509/21/22
401451R4.509/22/22
411451R2.509/22/22
421451V5.570.109/23/22
435257R0.309/11/22
445257R0.609/11/22
455257R9.509/12/22
465257R3.909/13/22
475257R3.409/13/22
485257R8.509/14/22
495257R10.409/15/22
505257R7.209/16/22
515257R3.809/19/22
525257R3.709/19/22
535257R3.809/20/22
545257R1.609/20/22
555257S709/21/22
565257R4.109/22/22
575257R1.809/22/22
585257R7.176.709/23/22
592234R3.709/12/22
602234R3.409/12/22
612234R3.509/13/22
622234R3.509/13/22
632234R3.509/14/22
642234R3.509/14/22
652234R3.509/15/22
662234R3.509/15/22
672234R3.509/16/22
682234R3.509/16/22
692234R3.509/19/22
702234R3.509/19/22
712234R3.509/20/22
722234R3.509/20/22
732234R3.509/21/22
742234R3.509/21/22
752234R3.509/22/22
762234R3.509/22/22
772234R3.509/23/22
782234R3.570.109/23/22
791234R3.509/12/22
801234PTO709/12/22
811234S709/13/22
821234P709/14/22
831234C709/15/22
841234AL709/16/22
851234CCP709/19/22
861234SDU709/20/22
871234FH709/21/22
881234FS1409/22/22
891234FV780.509/23/22
902899R2.409/12/22
912899R4.809/12/22
922899R4.109/13/22
932899R3.209/13/22
942899R2.209/14/22
952899R5.209/14/22
962899R4.309/15/22
972899R3.509/15/22
982899R4.109/16/22
992899R5.309/16/22
1002899R2.309/19/22
1012899R4.409/19/22
1022899R4.209/20/22
1032899R3.209/20/22
1042899R2.309/21/22
1052899R4.709/21/22
1062899R2.509/22/22
1072899R4.867.509/22/22
Sheet1


The totals are placed in the H column temporarily. (Also, why does it paste 3 times for the first employee? Not that it matters...)

I would like to have the total values be a formula that solver can change, if possible.


Thank you
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try:

VBA Code:
.FormulaR1C1 = "=SUMIF(R2C1:R" & lRow + 4 & "C1,RC[-7],R2C3:R" & lRow + 4 & "C3)"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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