+ Reply to Thread
Results 1 to 7 of 7

Add relative formula to next column

Hybrid View

  1. #1
    Registered User
    Join Date
    02-08-2016
    Location
    Holland
    MS-Off Ver
    2010
    Posts
    22

    Add relative formula to next column

    Hi all,

    The recorder couldn't help me, so hopefully you can:

    I want to make a macro that drags my formula* over to the next cell (in the same row) which doesn't contain the formula, but has an absolute value in it. As a result the absolute value will be overwritten. The absolute value in the cell next to the changed cell, should be unchanged until I run the macro again.

    * I would like this formula be "relative" so when you drag it the formule "=A23" will be "=B23" in the next cell etc.
    ** I added a simplified example.

    *Solved thanks to Mehmet*
    Attached Files Attached Files
    Last edited by babymax; 04-11-2016 at 02:26 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Add relative formula to next column

    right click on your sheet name at the bottom of excel and select view code

    A Module will open

    paste this code and close the module. The macro will run when you right click on row 1.

    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Cells.Count > 1 Or Target.Row > 1 Or LCase(Left(Target.Value, 4)) <> "week" Then Exit Sub
    On Error GoTo Quit
    Set Z = Rows(3).Find(0, LookIn:=xlValues, Lookat:=xlWhole)
    t = Z.Address
    Z.Formula = "='Data entry'!" & Z.Address
    
    Cancel = True
    Quit:
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    02-08-2016
    Location
    Holland
    MS-Off Ver
    2010
    Posts
    22

    Re: Add relative formula to next column

    Thanks for your help, the macro does the work. Shamefully I wasn't clear enough of what I wanted to achieve so I will rephrase and
    add a better example.


    - I want this macro to be part of a larger routine, so would like it to have it work without the need to right click. Instead I would
    like to run it manually (through alt + F8 -> execute).
    - I want this macro to overwrite one cell at a time, everytime I run this macro (like yours already did).

    I added a new example to give a better understanding. I used the real layout, so I did hide some rows so the texts are in the right row
    and column number. I limited the example to 5 weeks, in reality it goes on and on. The coloring in the cells is just to
    make it more clear, in the real file the coloring isn't there.

    Hope you can help me out again.

    Cheers.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Add relative formula to next column

    
    Sub Macro()
    'Find Last used Column in Row 58
    LC = Cells(58, Columns.Count).End(xlToLeft).Column
    
    'Find Last Column with Actual in Formula
    For Col = LC To 1 Step -1
    T = Cells(58, Col).Formula
    If InStr(T, "Actual") > 0 Then Exit For
    Next
    
    'Put Formula in the next column
    Cells(58, Col + 1).Formula = "='Actuals 2016'!" & Cells(51, Col).Address
    End Sub

  5. #5
    Registered User
    Join Date
    02-08-2016
    Location
    Holland
    MS-Off Ver
    2010
    Posts
    22

    Re: Add relative formula to next column

    Works great again, thanks. This week I will test it on the 'real' sheet at work. If it works than as intented I will mark this thread as solved. Thanks alot Mehmet.

  6. #6
    Registered User
    Join Date
    02-08-2016
    Location
    Holland
    MS-Off Ver
    2010
    Posts
    22

    Re: Add relative formula to next column

    Hi I tried it on the real sheet and it works again. Hope I can add one more thing:
    - Is it possible to clear the coloured formatting of the cell in which the new formula


    (apologise for the late reply)

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Add relative formula to next column

    
    Sub Macro()
    'Find Last used Column in Row 58
    LC = Cells(58, Columns.Count).End(xlToLeft).Column
    
    'Find Last Column with Actual in Formula
    For Col = LC To 1 Step -1
    T = Cells(58, Col).Formula
    If InStr(T, "Actual") > 0 Then Exit For
    Next
    
    'Put Formula in the next column
    Cells(58, Col + 1).Select
    Selection.Formula = "='Actuals 2016'!" & Cells(51, Col).Address
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End Sub

+ 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] absolute column and relative row not working in my formula???
    By ciapul12 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-15-2014, 02:29 PM
  2. [SOLVED] Using relative addresses to call out Excel table column names in SUMIFS formula
    By djgerbec in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-29-2013, 03:43 PM
  3. Macro to sum relative column and row
    By keats in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2013, 07:22 PM
  4. Replies: 5
    Last Post: 09-21-2012, 02:08 PM
  5. Sum of relative cell in a column
    By Chetan Chauhan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2009, 09:19 AM
  6. Replies: 2
    Last Post: 05-26-2009, 05:55 PM
  7. Code to auto populate a relative formula into a column
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-13-2009, 12:30 PM
  8. Insert a formula with relative cell reference based on a number in a column
    By Dcritelli in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-16-2007, 05:54 PM

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