+ Reply to Thread
Results 1 to 6 of 6

Inserting Offset equation using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Inserting Offset equation using VBA

    Hi Folks,

    I'm having trouble and wondered if anyone could help:

    I have two worksheets. The first sheet contains a column of 10 values. I have named the cell in the last row "final"

    I want to put these values in to the second sheet using the offset function. So the first cell in the second sheet would have the equation
    =OFFSET(final,-9,0)
    The second cell would have the equation
    =OFFSET(final, -8, 0)
    The third would be
    =OFFSET(final, -7, 0)
    and so on down to the last row which would be
    =OFFSET(final, 0, 0)
    and sheet one and sheet two would be identical. How would I accomplish this? I don't want to use the offset vba function because I want the formula inside the cells. The trouble I'm having is with the row offset number changing, I don't know how to code that.

    Any ideas would be appreciated, many thanks for your help,

    AR
    Last edited by agentred; 05-19-2010 at 06:47 PM.

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

    Re: Inserting Offset equation using VBA

    In the first cell and copy down, =INDEX(Sheet1!A:A, ROW(final) + ROW() - 10)

    ... or

    =INDEX(Sheet1!$1:$65536, ROW(final) + ROW() - 10, COLUMNS(final))
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Inserting Offset equation using VBA

    Hi,

    Something like:

    Sub OffsetTen()
        Dim l As Long
    
        For l = 10 To 1 Step -1
            Sheet2.Range("A" & l) = "=Offset(final," & -l & ",0)"
        Next l
    End Sub
    perhaps?

    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

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

    Re: Inserting Offset equation using VBA

    Sorry, you did say VBA. Another way:
    Sub x()
        Sheet2.Range("A1:A10").Formula = "=INDEX(Sheet1!$1:$65536, ROW(final) + ROW() - 10, COLUMNS(final))"
    End Sub

  5. #5
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Inserting Offset equation using VBA

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Something like:

    Sub OffsetTen()
        Dim l As Long
    
        For l = 10 To 1 Step -1
            Sheet2.Range("A" & l) = "=Offset(final," & -l & ",0)"
        Next l
    End Sub
    perhaps?

    HTH
    But... I could've sworn I tried the exact same formula and it didn't work, but then I tried copying and pasting your code and it works almost perfectly!

    I say almost because there's one hitch... Because the range is the same as the offset, the column ends up being inverted? Like it starts at 10 and ends at 1, instead of the other way around. So I edited it to this

        Dim l As Long
        Dim j As Integer
        
        For l = 9 To 1 Step -1
        j = 9 - l
            Range("A" & j).Value = "=Offset(open," & -l & ",0)"
        Next l
    And it's perfect.

    Thanks for your help!!

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Inserting Offset equation using VBA

    Hi,

    In that case why not just use a loop counter from 1 to l, rather than a reverse (step -1) loop counter and then having to invert that with a j=9-l

    Rgds

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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