??? Variable = activesheet.range(selection.address) ???

dukeofscouts

Board Regular
Joined
Jan 19, 2009
Messages
146
So I'm working on a macro that will utilize GoalSeek to make calculations faster.

Working code....

Dim CellToChange As Range
'Trouble Starts
CellToChange = Sheet4.Range(Selection.Address)
Call maxoutform.FindMyNutritent(ComboBox1.Value)
Selection.Offset(0, -3).Select
Selection.GoalSeek goal:=0.01, changingcell:=CellToChange

Working Code...

Sub FindMyNutritent(nameMyNutrient As String)
Range("a7").Select
While Selection.Value <> nameMyNutrient
Selection.Offset(0, 1).Select
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi dukeofscouts,

Your statement CellToChange = Sheet4.Range(Selection.Address) needs to be

Set CellToChange = Sheet4.Range(Selection.Address)

because CellToChange is an object variable. Set is always required when assigning an object to an object variable.

That being said, since Selection in this context is clearly a Range object you could simply do:

Set CellToChange = Selection

I'm assuming the sheet containing your desired selection is the active worksheet (actually, your code is assuming this).

Damon
 
Upvote 0
THANKS!!!!!
PERFECTION...

Well almost. I get a run-time error on the goal seek. In debug mode it shows that the variable value is set to the value of the selection. I've done it both with the "selection.address" version and the strait "selection". With the variable set as a range I'm at a loss for why it would change the variable to be mixed.

If OptionNutrient.Value = True Then
Dim CellToChange As Range
Set CellToChange = Selection
Call maxoutform.FindMyNutritent(ComboBox1.Value)
Selection.Offset(0, -3).Select
Selection.GoalSeek goal:=0.01, changingcell:=CellToChange
'Error 1004
'Reference not valid
End If
 
Upvote 0

Forum statistics

Threads
1,215,837
Messages
6,127,187
Members
449,368
Latest member
JayHo

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