Solver macro for active row

jarvisjaska

New Member
Joined
Jan 13, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I've recorded a simple solver macro and I have two problems with it. The recorded code is shown below:

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    SolverReset
    SolverOk SetCell:="$BU$8", MaxMinVal:=3, ValueOf:=0, ByChange:="$BC$8,$BQ$8", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$BU$8", MaxMinVal:=3, ValueOf:=0, ByChange:="$BC$8,$BQ$8", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve userFinish:=True
End Sub

Problem 1: Recorder didn't record constraints (cells BD8 and BO8 should both equal 0)
Problem 2: I want to be able to run the macro on the active row (ie. the row I'm currently on). For example, if I'm on row 11 I need to set cell BU11 to 0 by changing cells BC11 and BQ11. Also cells BD11 and BO11 should both equal 0.

Any ideas on how to solve these problems?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
ActiveCell refers to the current selected cell
Activecell.offset(0,1) refers to the cell one column to the right of the current selected cell
Activecell.Row refers to the row of the selected cell
Range("BC" & Activecell.row).value = 0 changes BC11 to zero if the selected cell is on row 11
Range("BQ" & Activecell.row).value = 0 changes BQ11 to zero if the selected cell is on row 11
 
Upvote 0
Solution
ActiveCell refers to the current selected cell
Activecell.offset(0,1) refers to the cell one column to the right of the current selected cell
Activecell.Row refers to the row of the selected cell
Range("BC" & Activecell.row).value = 0 changes BC11 to zero if the selected cell is on row 11
Range("BQ" & Activecell.row).value = 0 changes BQ11 to zero if the selected cell is on row 11
Thank you! Took a while to get it right, but now it's working perfectly.
 
Upvote 0

Forum statistics

Threads
1,214,618
Messages
6,120,544
Members
448,970
Latest member
kennimack

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