title: Solver VBA macro across multiple columns and rows
body: <p>New to VBA.  Scoured the internet, can't come up with solution (but I learned a lot in the process).</p>
  
  <p>I am running solver to solve for a revenue number that will give me a specified profit margin given certain expenses.  I've got that all set and a Sub that will perform this task properly for one month.  However, I am trying to apply this to 12 columns (12-months).  Then step the loop down 19 rows.  Run solver across those 12 columns and so on 6-times.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>Sub Monthly()
  
  
  SolverReset
  SolverAdd CellRef:="$d$40", Relation:=2, FormulaText:="$d$41"
  SolverOk SetCell:="$d$40", MaxMinVal:=1, ValueOf:=0, ByChange:="$d$24", Engine _
      :=1, EngineDesc:="GRG Nonlinear"
  SolverSolve True
  
  SolverReset
  SolverAdd CellRef:="$e$40", Relation:=2, FormulaText:="$e$41"
  SolverOk SetCell:="$e$40", MaxMinVal:=1, ValueOf:=0, ByChange:="$e$24", Engine _
      :=1, EngineDesc:="GRG Nonlinear"
  SolverSolve True
  </code></pre>
  
  <p>This is an example that would get me January and February.  Want it to run through the remaining 10-months (without having to copy and paste this 10-times and manually type in the cell coordinates for F-O columns).  Then the code below is for the step down 19 rows:</p>
  
  <pre><code>SolverReset
  SolverAdd CellRef:="$d$59", Relation:=2, FormulaText:="$d$60"
  SolverOk SetCell:="$d$59", MaxMinVal:=1, ValueOf:=0, ByChange:="$d$43", Engine _
      :=1, EngineDesc:="GRG Nonlinear"
  SolverSolve True
  
  
  End Sub
  </code></pre>
  
  <p>And again, I would like this to run across 12 columns and step down 19 rows to do it again 4 more times.  So the end result is solver running 6 times across 12 columns for a total of 72 times.  I would prefer to not type those cell coordinates that many times, and it's probably taxing on my CPU to do it that way. Thanks!</p>
  
  <p>UPDATE:</p>
  
  <p>I am now getting an error, seemingly for no reason.  This thing was working great.  </p>
  
  <pre><code>Sub MonthlySolve1a()
      Dim c As Range
      Set c = ActiveSheet.Range("D40")
      MonthlySolve1b c
  End Sub
  
  'solve 12 months
  Sub MonthlySolve1b(c As Range)
  Dim m  As Long
  For m = 1 To 12
  
      SolverReset
      SolverAdd CellRef:=c.Address(), Relation:=2, FormulaText:=c.Offset(1, 0).Address()
      SolverOk SetCell:=c.Address(), MaxMinVal:=1, ValueOf:=0, _
              ByChange:=c.Offset(-16, 0).Address(), Engine:=1, EngineDesc:="GRG Nonlinear"
      SolverSolve True
  
      Set c = c.Offset(0, 1)
  Next m
  End Sub
  </code></pre>
  
  <p>I probably did something stupid in my sheet that messed it up, but any insight would be greatly appreciated.  Thanks!</p>