+ Reply to Thread
Results 1 to 5 of 5

VBA Goalseek Offset

  1. #1
    Registered User
    Join Date
    09-20-2016
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    3

    VBA Goalseek Offset

    Hi all,

    Bit of a tricky one, or possibly not.
    I am trying to code through a spreadsheet to allow me to update a column of values based on a two variable condition.
    The basic idea is that Column J contains values (angles) related to the difference in z elevations between cells in column C.

    Where the value of the angle in column J is greater than 15 degrees I need to check column C and determine whether the offset value on the same row is greater than or less than the offset value in the row below it or less than the row below it.
    In relation to Column J, Where the value in column C offset (0,-7) is greater than the value in column C offset (1,-7) I need to raise the value in (0-7) so that the formula calculating J updates to a value of equal to 15 degrees.
    Where the value in Column C offset (0-7) is less than the value in column C offset (1,-7), I need to raise the value in (1,-7) so that the formula calculating J updates to a value of equal to 15 degrees.

    The key point is that the value cannot be reduced in Column C, only increased in order to reduce the angle in Column J.

    Below is some of the code I tried, with no luck:

    Private Sub ChangeCell()
    Dim Target As range
    Dim B As Integer
    Set Target = range("J4:J100")
    B = 15


    For Each cell In Target

    If cell.Value > B Then
    cell.Offset(0, -7).Select
    Selection.Activate

    With Selection
    If cell.Value < cell.Offset(1, 0).Value Then
    cell.Offset(0, 7).GoalSeek goal:=15, changingcell:=ActiveCell.Value
    ElseIf cell.Value > cell.Offset(1, 0).Value Then
    cell.Offset(1, 7).GoalSeek goal:=15, changingcell:=cell.Offset(1, 0).Value
    End If
    End With
    End If
    Next

    End Sub

    The attachment button doesn't seem to be working for me, but please see attached screenshot of the columns / spreadsheet.
    If anyone can help it would be very much appreciated.

    Cheers,

    David
    Untitled.png

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA Goalseek Offset

    Welcome to the board.

    ChangingCell requires a range reference, not a value.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-20-2016
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    3

    Re: VBA Goalseek Offset

    Hi,

    Many thanks for the tip.
    Do you have a suggestion as to how I might get this to work?
    The range will have up to 40,000 values so referencing them individually is going to be impossible. Furthermore the Goalseek function does not exist as a standalone formulaic function for input directly into the spreadsheet.

  4. #4
    Registered User
    Join Date
    09-20-2016
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    3

    Re: VBA Goalseek Offset

    I figured it out.
    Thank again for the great tip, it dawned on me what to do a few hours later after some time out on Gran Turismo.
    Solution below:


    Private Sub ChangeCell()
    Dim Target As Range
    Dim B As Integer
    Dim y As Range
    Dim x As Range
    Dim w As Range
    Dim z As Range
    Set Target = Range("J4:J100")
    B = 15


    For Each cell In Target

    If cell.Value > B Then

    Set x = cell.Offset(0, -7)
    Set y = cell.Offset(0, 0)
    Set w = cell.Offset(1, -7)

    If x < w Then
    y.GoalSeek goal:=15, changingcell:=x
    ElseIf x > w Then
    Set z = cell.Offset(0, 1)
    z.GoalSeek goal:=15, changingcell:=w
    End If
    End If
    Next

    End Sub

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA Goalseek Offset

    Good job !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA to goalseek across column
    By hassanm in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-08-2015, 10:43 AM
  2. Goalseek in VBA - Is it possible to see how many iterations were used?
    By Belisartih in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2014, 04:12 PM
  3. Formula Instead of Goalseek
    By Darreno in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-31-2014, 05:58 PM
  4. GoalSeek & Index + GUI
    By defy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-13-2012, 07:23 PM
  5. Problems in Goalseek
    By elizi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-26-2007, 06:38 AM
  6. [SOLVED] GoalSeek in VBA
    By [email protected] in forum Excel General
    Replies: 1
    Last Post: 07-20-2006, 12:20 AM
  7. mimic goalseek
    By Monique in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-25-2005, 03:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1