Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

excel - VBA code to find value based on seek value

I want to write a piece of VBA CODE that will goal seek the variable value into the static value based on the input value

Excel1 !

ie. something along these lines but i manually did these,

Excel1 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK, here is what you have to do. First, you set the layout of your worksheet looks like this:

enter image description here

Here I use Sheet1. Please make changes accordingly if necessary.

In order to use Solver add-in, you must first make sure that the add-in is installed. For Office 2013 and later:

  1. Click the File tab, and then click Options below the Excel tab.
  2. In the Excel Options dialog box, click Add-Ins.
  3. In the Manage drop-down box, select Excel Add-ins, and then click Go.
  4. In the Add-Ins dialog box, select Solver Add-in, and then click OK.

After that, you must set a reference to the add-in in the workbook containing the code Visual Basic Editor (VBE) that calls the add-in's procedures. Click References on the Tools menu (see pictures below), and then select Solver under Available References.

enter image description here enter image description here

Now, go to worksheet 1 code module in VBE. Just click Sheet1 twice in VBE and paste the following code into it:

enter image description here

Sub VBASolver()
SolverReset
SolverOk SetCell:="$F$2", MaxMinVal:=3, ValueOf:=0, ByChange:="$B$2:$B$4"
SolverSolve True
End Sub

You can run the loaded VBA program by clicking Run icon below Debug menu or pressing F5 key on the keyboard.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...