+ Reply to Thread
Results 1 to 8 of 8

Help: Need macro that copies formula based on condition

  1. #1
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Help: Need macro that copies formula based on condition

    Hello,

    I'm new to the forum. Was wondering if you guys could help me with the following:

    I have a a table that has Lengths (km) with corresponding Temperature (°C). However, the last length value does not have a temperature associated with it. I would like to create a macro that goes to the last length value and checks to see if there is a temperature associated with it, if not it will go to the next temperature column (2 columns down) and copy the first temperature value. I would like to repeat this for all length/temperature columns.

    In the picture the yellow cells are the empty temperature cells that need to have the value of the first temperature cell (white cell) copied over. However, the last temperature cell in the table (red cell) should be left empty.
    Table.PNG

    I made a really rudimentary macro using Record Macro, and it works fine. However, it only works for this workbook. I have other workbooks with the same kind of table but with more/less Lengths & Temperatures values so I can't use the recorded macro on those. This is the macro I am currently using - it is cell reference specific and I need to make it so it can work on any number of values for columns:

    Sub CopyEndRef()
    '
    ' CopyEndRef Macro
    ' copy reference from beginning of next section over to end of last section
    '
    Range("B26").Select
    ActiveCell.FormulaR1C1 = "=R4C[2]"
    Range("B26").Select
    Selection.Copy
    Range("B26,D14,F10,H6,J704,L17,N6,P12,R7,T8").Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    End Sub


    Any help would be much appreciated!

    Thanks

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Help: Need macro that copies formula based on condition

    Impossible to test without a sample workbook to play with, but try:
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Help: Need macro that copies formula based on condition

    Thanks for the suggestion, unfortunately it doesn't work as intended. It instead copies a different value over and there are still some cells that did not get copied over.

    I have attached the workbook to play around with.
    Attached Files Attached Files

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Help: Need macro that copies formula based on condition

    Much easier to sort out when you attach a workbook!

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Help: Need macro that copies formula based on condition

    Thanks! That worked magic!!

    Now I just have to try and understand what you did!!

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Help: Need macro that copies formula based on condition

    And another similar way.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Help: Need macro that copies formula based on condition

    Thanks jolivanes, this also works perfectly.

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Help: Need macro that copies formula based on condition

    You can replace this
    Please Login or Register  to view this content.
    with this
    Please Login or Register  to view this content.
    If you don't need/want a formula

    Good luck and thanks for letting us know.

+ 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. Macro that copies based on cell value
    By freshfruit in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 02-12-2014, 03:13 PM
  2. Macro thats copies data based on 2 variables.
    By dwilkinson12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2013, 10:34 AM
  3. Replies: 2
    Last Post: 05-01-2013, 12:26 PM
  4. [SOLVED] Customize code for a macro that copies three rows to a new workbook based on a condition
    By philips in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 01-04-2013, 05:55 AM
  5. [SOLVED] Macro which copies based on row and column inputs
    By buntalan80 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-22-2012, 07:23 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