Using goal seek within VBA

waptrick

New Member
Joined
Apr 20, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
The VBA code I have written uses goal seek within the code but it's returning values that are vastly different than if I run a manual goal seek for each sheet. I have pasted the code below and can attach the reference files upon request.

Sub UpdateAndSolver()
Dim count, lastrow, datenum, currow, filerow, updaterow, supplyrow As Integer
Dim curdate As Date
Dim adrvalue, adrvalueold, supply, occu, demand, oldsupply, gr1, gr2, gr3 As Long
Dim exrate, rmsoldvalue, rmsoldvalue2 As Double
Dim filenamea, datestring, tabname, rowname As String

Workbooks("Updater - Copy").Activate
filenamea = Sheets("Main panel").Range("inb_filename").Value
datenum = Sheets("Main panel").Range("date_num").Value
Workbooks.Open filename:=Sheets("Main panel").Range("inb_datapath").Value
Workbooks(filenamea).Activate

curdate = DateSerial(Year(datenum), Month(datenum), 1)
Workbooks.Open filename:=Workbooks("Updater - Copy").Sheets("Main panel").Range("strmonth_data").Value
Workbooks("STR Monthly").Activate

For count = 1 To 20

tabname = Workbooks("Updater - Copy").Sheets("Shtnames").Cells(count, 2).Value
rowname = Workbooks("Updater - Copy").Sheets("Shtnames").Cells(count, 3).Value
filerow = WorksheetFunction.Match(rowname, Workbooks(filenamea).Sheets("Multi-Segment").Range("B1:B40"), 0)
updaterow = WorksheetFunction.Match(Round(CDbl(curdate)), Workbooks("STR Monthly").Sheets(tabname).Range("A1:A500"), 0)
supplyrow = updaterow - 36
occu = Workbooks(filenamea).Sheets("Multi-Segment").Range("G" & filerow).Value / 100
demand = Workbooks("STR Monthly").Sheets(tabname).Range("J" & updaterow).Value
supply = demand / occu
gr1 = Workbooks("STR Monthly").Sheets(tabname).Range("L" & updaterow).Value
gr2 = Workbooks("STR Monthly").Sheets(tabname).Range("L" & (updaterow - 12)).Value
gr3 = Workbooks("STR Monthly").Sheets(tabname).Range("L" & (updaterow - 24)).Value
oldsupply = supply / (1 + gr1) / (1 + gr2) / (1 + gr3)
Workbooks("STR Monthly").Sheets(tabname).Range("I" & (updaterow - 36)).Value = oldsupply

' Use Solver for optimization
Dim ws As Worksheet
Set ws = Workbooks("STR Monthly").Sheets(tabname)
Dim goalCell As Range
Set goalCell = ws.Range("C" & (updaterow - 12)) ' Occupancy 12 months prior
Dim changingCell As Range
Set changingCell = ws.Range("M" & (updaterow - 12)) ' Value 12 months prior
Dim targetValue As Double
targetValue = Workbooks(filenamea).Sheets("Multi-Segment").Range("H" & filerow).Value

SolverOk SetCell:=goalCell, MaxMinVal:=3, ValueOf:=targetValue, ByChange:=changingCell
SolverSolve UserFinish:=True
Next count

For count = 1 To 5
tabname = Workbooks("Updater - Copy").Sheets("Shtnames").Cells(count, 10).Value
updaterow = WorksheetFunction.Match(Round(CDbl(curdate)), Workbooks("STR Monthly").Sheets(tabname).Range("B1:B500"), 0)
Workbooks("STR Monthly").Sheets(tabname).Range("A" & updaterow).Value = Workbooks("Updater - Copy").Sheets("Shtnames").Cells(count, 11).Value
Next count
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

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