VBA Add Solver constraints based on criteria

JaCharger

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

I'm building a tool to change salaried employees hours to equal 70. I now have a way to add in a formula that adds a total hour count to be manipulated automatically, but I'm stuck on adding solver constraints automatically.

Here is the code I've been working on. It doesn't work - the
VBA Code:
If cell.Value = "R" Then
line errors with Object Required. Not sure why.

VBA Code:
Private Sub SalarySolver()

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
Dim pto As Range

    SolverOK SetCell:=Range("H21"), MaxMinVal:=3, ValueOf:=70, ByChange:=Range("C2:C21")

'For Each cell In Range("B2:B" & lRow)
    If cell.Value = "R" Then
    
        
    
        Else
            SolverAdd CellRef:=(2 + 1 & lRow), Relation:=2, FormulaText:="7"
            
        End If
        
    

'Next



End Sub

Essentially, all I need the code to do is look in column B, if the value is not "R" then you add the value in Column C to the constraints. These values must remain unchanged - that is one aspect of the code that I haven't implemented. I believe the SolverAdd line just sets the constraints to equal 7, when in reality they can't be changed.

Here is the mini-sheet. Any help is appreciated.

VIP Processing Tool.xlsm
ABCDEFGHIJ
1EmpNoPaycodeCodeHoursRateFlat AmountSalaryFirst IgnoredSecond IgnoredShift DiffWorkDate
24638R3.809/12/22
34638R3.5309/12/22
44638R7.3709/13/22
54638V709/14/22
64638V709/15/22
74638V709/16/22
84638R3.809/19/22
94638R3.509/19/22
104638R109/19/22
114638R3.8709/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.3709/23/22
221451R5.1209/12/22
231451R1.8809/12/22
241451R3.7509/13/22
251451R3.2509/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.57009/23/22
435257R0.2709/11/22
445257R0.609/11/22
455257R9.5209/12/22
465257R3.9309/13/22
475257R3.409/13/22
485257R8.509/14/22
495257R10.409/15/22
505257R7.2309/16/22
515257R3.7709/19/22
525257R3.7209/19/22
535257R3.809/20/22
545257R1.5809/20/22
555257S709/21/22
565257R4.1309/22/22
575257R1.7509/22/22
585257R7.0876.6809/23/22
592234R3.6509/12/22
602234R3.3509/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.57009/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
Cell Formulas
RangeFormula
H21,H107,H89,H78,H58,H42H21=SUMIF($A$2:$A$111,A21,$C$2:$C$111)
Named Ranges
NameRefers ToCells
solver_adj=Sheet1!$C$2:$C$21H107, H89, H78, H58, H42, H21
solver_lhs1=Sheet1!$C$5:$C$7H107, H89, H78, H58, H42, H21
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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