Consulting

Results 1 to 6 of 6

Thread: Goalseek last cell in column, changing last cell in adjacent column

  1. #1
    VBAX Newbie
    Joined
    Jun 2018
    Posts
    4
    Location

    Goalseek last cell in column, changing last cell in adjacent column

    Hi,

    New to VBA/Excel.

    I'm trying to change the value of a cell with goal seek to a value by changing the cell in the column next to it. I cant seem to find out how to set the last range to the last cell in the column.
    I attempted placing Worksheets("Hours").Cells(Rows.Count, 5).End(xlUp) directly in along with a number of other attempts and I'm at the point where I just need to walk away and come back.

    Sub CalcEndTime()
    '
    ' CalcEndTime Macro
    '
    ' Keyboard Shortcut: Ctrl+t
    '
    Dim Lastrow As Range
    Lastrow = ActiveSheet.Cells(Rows.Count, 5).End(xlUp).Row


    Application.CutCopyMode = False
    Worksheets("Hours").Cells(Rows.Count, 5).End(xlUp).Offset(0, 1).GoalSeek Goal:=8, ChangingCell:=Range(Lastrow)


    End Sub



    Error message says RTE 1004: Method 'Range' of object'_Global' failed

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Maybe

    Worksheets("Hours").Cells(Rows.Count, 5).End(xlUp).Offset(0, 1).GoalSeek Goal:=8, ChangingCell:=Worksheets("Hours").Cells(Lastrow,5)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Jun 2018
    Posts
    4
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Maybe

    Worksheets("Hours").Cells(Rows.Count, 5).End(xlUp).Offset(0, 1).GoalSeek Goal:=8, ChangingCell:=Worksheets("Hours").Cells(Lastrow,5)
    Same error, I was testing last night, and if I place a regular cell in the last cell say:

    Worksheets("Hours").Cells(Rows.Count, 5).End(xlUp).Offset(0, 1).GoalSeek Goal:=8, ChangingCell:=
    Worksheets("Hours").Cells(A1)

    it works using that or any set cell (obviously), so I've come to thing that its something with the Dim/Set?



  4. #4
    VBAX Newbie
    Joined
    Jun 2018
    Posts
    4
    Location
    Sorry Actually new error I forgot I had changed the dim several times, when using

    Worksheets("Hours").Cells(Rows.Count, 5).End(xlUp).Offset(0, 1).GoalSeek Goal:=8, ChangingCell:=Worksheets("Hours").Cells(Lastrow,5)


    I get a new error and highlights on line:

    Lastrow = ActiveSheet.Cells(Rows.Count, 5).End(xlUp).Row
    Error: RTE 91 Object Variable or With block variable not set

    This lead me to add Set to the beginning of line 2:

    Dim Lastrow As Range
    Set Lastrow = ActiveSheet.Cells(Rows.Count, 5).End(xlUp).Row


    which throws RTE 424 Object required on the same Set Lastrow line.

    Final code remains there for now:

    Sub CalcEndTime()'
    ' CalcEndTime Macro
    '
    ' Keyboard Shortcut: Ctrl+t
    '
    Dim Lastrow As Range
    Set Lastrow = ActiveSheet.Cells(Rows.Count, 5).End(xlUp).Row
    
    
        Application.CutCopyMode = False
        Worksheets("Hours").Cells(Rows.Count, 5).End(xlUp).Offset(0, 1).GoalSeek Goal:=8, ChangingCell:=Range(Lastrow, 5)
    
    
    End Sub

  5. #5
    VBAX Newbie
    Joined
    Jun 2018
    Posts
    4
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Maybe

    Worksheets("Hours").Cells(Rows.Count, 5).End(xlUp).Offset(0, 1).GoalSeek Goal:=8, ChangingCell:=Worksheets("Hours").Cells(Lastrow,5)

    Upon further review this did work Paul. I had made so many changes in the meantime that I messed up something else somewhere, I restarted from scratch with this input and it worked. Thank you!

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Glad you got it working
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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