Dynamic column reference in loop solver/loop vba code

olarsen

New Member
Joined
Jan 16, 2012
Messages
1
I would highly appreciate if anyone can help me with this problem: <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I use the following macro (below). It works perfect, however I often add columns in the worksheet, and then I have to change the column names in the VBA formula (e.g. from BD to BE). Is it possible to make the column reference dynamic, such that if I add a column in the worksheet, I do not have to change the reference in VBA code? <o:p></o:p>


Sub Optimize_TV()
'
' Optimize_TV Macro
'
'
Worksheets("Calculations").Activate
RowCount = 12
Do While Not IsEmpty(Worksheets("Calculations").Range("B" & RowCount))
SolverReset
SolverOk SetCell:=Range("BD" & RowCount), MaxMinVal:=1, ValueOf:=0, ByChange:=Range("BF" & RowCount), Engine _
:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:=Range("BE" & RowCount), Relation:=1, FormulaText:=Range("AY" & RowCount)
SolverAdd CellRef:=Range("BE" & RowCount), Relation:=3, FormulaText:=Range("AZ" & RowCount)
SolverOk SetCell:=Range("BD" & RowCount), MaxMinVal:=1, ValueOf:=0, ByChange:=("BF" & RowCount), Engine _
:=1, EngineDesc:="GRG Nonlinear"
SolverSolve userFinish:=True
SolverFinish keepFinal:=1
RowCount = RowCount + 1
Loop

End Sub



Thanks for all help!

Thanks
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,216,130
Messages
6,129,056
Members
449,484
Latest member
khairianr

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