what is wrong with this macro code?

yichuansancun

Board Regular
Joined
Feb 7, 2011
Messages
123
What I try to achieve is that when the cell named housing_choice = "specified amount", excel should wipe out all the contents in the range with name "housing_m", and if the cell named housing_choice = "m data" then the cell named "housing" should be empty

If Range("housing_choice").Value = "specified amount" Then
Range("housing_m").Select
Selection.Value = ""
ElseIf Range("housing_choice").Value = "m data" Then
Range("housing").Select
Selection.Value = ""
End If


but it won't work... any ideas?

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Perhaps

Code:
If Range("housing_choice").Value = "specified amount" Then
    Range("housing_m").ClearContents
ElseIf Range("housing_choice").Value = "m data" Then
    Range("housing").ClearContents
End If
 
Upvote 0
Hello,

Try this

If Range("housing_choice").Value = "specified amount" Then
Range("housing_m").ClearContents
ElseIf Range("housing_choice").Value = "m data" Then
Range("housing").ClearContents
End If
 
Upvote 0
how it gives me "compile error: wrong number of arguments of invalid property assignment"

Here is the entire code (changed some ranges' name):

Sub Macro8()
'
Macro8 Macro

If Range("choice").Value = "specified amount" Then
Range("housing_selection").ClearContents
ElseIf Range("choice").Value = "m data" Then
Range("housing").ClearContents
End If

SolverOk SetCell:="$N$7", MaxMinVal:=2, ValueOf:="0", ByChange:="$N$3"
SolverSolve userFinish:=True


End Sub


:confused::confused::confused:
 
Upvote 0
In Tools > References make sure that Solver is ticked.

You also need to delete or comment out this line of code

Rich (BB code):
Macro8 Macro

 
Upvote 0
this is frustrating... when I click the "calculate" button which is assigned with macro 8, the solver returns "#num!". And it is random... sometimes it works and sometimes it doesn't, once it didn't work, then I cannot even do "undo".

:(:(:(
<table border="0" cellpadding="0" cellspacing="0" width="100"><tr height="22"><td class="xl67" style="height: 16.5pt; width: 75pt;" height="22" width="100">
</td></tr></table>
 
Upvote 0
That's because running a macro clears the undo stack.

Try recording a macro whilst running Solver manually to make sure that you have the correct syntax.
 
Upvote 0
Now the macro runs without the error message, and the solver works fine, but the code before solverok doesn't do anything...

Sub Macro8()


If Range("choice").Value = "specified amount" Then
Range("housing_selection").ClearContents
ElseIf Range("choice").Value = "m data" Then
Range("housing").ClearContents
End If

SolverOk SetCell:="$N$7", MaxMinVal:=2, ValueOf:="0", ByChange:="$N$3"
SolverSolve userFinish:=True

End Sub

Here are the ranges definition:
choice = C21
housing_selection = C22 - C25
housing = C26

It looks like the codes before solverok were by passed... :(
 
Upvote 0

Forum statistics

Threads
1,224,563
Messages
6,179,527
Members
452,923
Latest member
JackiG

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