Macro help

John Davis

Well-known Member
Joined
Sep 11, 2007
Messages
3,457
Hello All:

I have the following:

Code:
Sub GOALSEEK()
'
' GOALSEEK Macro
'
Dim i As Long
Dim x As Long
Dim lr As Long

lr = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lr Step -1

    x = Range("Q" & i).Value
    If Range("I" & i) = Range("I" & i).Offset(1, 0) And x < 0.05 And Range("Q" & i).Offset(1, 0).Value > 0.1 And Range("S" & i).Value = "3701.30.0000" Then

    Range("Q" & i).GOALSEEK Goal:=x, ChangingCell:=Range("L" & i).Value
    End If
Next i
    
End Sub

It does nothing. What am I missing? Any advice is appreciated.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
goalseek won't work because it's already at it's goal.

Thanks for your input, but shouldn't this modification work??

Code:
Sub GOALSEEK()
'
' GOALSEEK Macro
'
Dim i As Long
Dim x As Long
Dim lr As Long

lr = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lr Step -1

    x = Range("Q" & i).Offset(1, 0).Value
    If Range("I" & i) = Range("I" & i).Offset(1, 0) And Range("Q" & i).Value < 0.05 And x > 0.1 And Range("S" & i).Value = "3701.30.0000" Then

    Range("Q" & i).GOALSEEK Goal:=x, ChangingCell:=Range("L" & i).Value
    
    End If
Next i
    
End Sub
 
Upvote 0
would you mind posting your working code? I'd like to see what I missed.

It went through several revisions since my last post, but here is the final working one.

Code:
Sub GOALSEEK()
Dim i As Long
Dim lr As Long
Dim goalCell As Range
Dim variableCell As Range
Dim goalValue As Variant

lr = Cells(Rows.Count, 1).End(xlUp).Row

MsgBox "Please run Edit-Import, and Sort S,I & Q first"

On Error Resume Next

For i = lr To 2 Step -1
    
    If Range("I" & i) = Range("I" & i).Offset(1, 0) And Range("Q" & i).Value < 0.05 And Range("Q" & i).Offset(1, 0).Value > 0.1 And Range("S" & i) = "3701.30.0000" Then

Set goalCell = Range("Q" & i)
Set variableCell = Range("L" & i)
Set goalValue = Range("Q" & i).Offset(1, 0)

goalCell.GOALSEEK Goal:=goalValue, ChangingCell:=variableCell

End If

Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,548
Members
452,927
Latest member
rows and columns

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