+ Reply to Thread
Results 1 to 11 of 11

VBA Code For shifting of formula to next Column

  1. #1
    Forum Contributor leo73pk's Avatar
    Join Date
    09-28-2006
    Location
    UAE
    MS-Off Ver
    2016, Microsoft 365
    Posts
    294

    VBA Code For shifting of formula to next Column

    I'm working on a file from which a sample data & structure is enclosed for a reference.

    Currently for the month of April-2020, Cell ranges F5:F8 and F12:F15 are linked with corresponding cells of Column E. On first day of every month, when I Insert word "DONE" in cell E1, the Cell ranges F5:F8 and F12:F15 formula turns into absolute value and once Cell E1 is blank the Cell range F5:F8 and F12:F15 are again linked to Corresponding Cells of Column E. I'm doing this thru the following VBA Code.

    Please Login or Register  to view this content.
    Now I want that on 1st day June when I insert word DONE in Cell E1, Values of April-2020 Cell ranges F5:F8, F12:F15 and May-2020 Cell ranges G5:G8, G12:G15 turns into absolute value and Next Column's (June-2020) cell ranges H5:H8 and H12:H15 are linked with corresponding cells of Column E similarly it goes till Column T (June-2021).

    In view of above, kindly suggest a VBA Code or modify the above referred VBA Code.
    Attached Files Attached Files
    Last edited by leo73pk; 05-04-2020 at 12:24 PM. Reason: Solved
    Best Regards,
    Hamza


    It is the greatest of all mistakes to do nothing because you can only do little - do what you can.

  2. #2
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: VBA Code For shifting of formula to next Column

    Hi, I have following suggestion for you
    Change the value in E1 into the month AFTER the month that is done, so in your case if April (1) is done, put 2 in E1 (and 3 if May is done etc.). (as it is in your row of current months)
    then I can provide you this code:
    Please Login or Register  to view this content.
    I deliberately put both ranges in different blocks of coding for better understanding.
    Cheers
    Erwin
    Attached Files Attached Files
    Last edited by Eastw00d; 05-01-2020 at 06:39 AM. Reason: additional information
    I started learning VBA because I was lazy ...
    Still developing.... being more lazy...

  3. #3
    Forum Contributor leo73pk's Avatar
    Join Date
    09-28-2006
    Location
    UAE
    MS-Off Ver
    2016, Microsoft 365
    Posts
    294

    Re: VBA Code For shifting of formula to next Column

    Dear Eastwood,

    First I would like to thank you for your time to review and propose a valuable solution. Moreover, I would like to ask that the code works for 15 months. Lets say for next 15 months if I place the 1st month i.e. 16th month, again in Column F how the Column F automatically linked to the corresponding cells of Column E.

  4. #4
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: VBA Code For shifting of formula to next Column

    Yes, now I have amended the ELSE part of the second IF:
    if you input 1 in cell E1, the following happens:
    The whole range is being emptied, watch out, it is NOT saved, the formula is set back in column F.
    Please Login or Register  to view this content.
    and then you can start over again.

    Cheers
    Erwin

  5. #5
    Forum Contributor leo73pk's Avatar
    Join Date
    09-28-2006
    Location
    UAE
    MS-Off Ver
    2016, Microsoft 365
    Posts
    294

    Re: VBA Code For shifting of formula to next Column

    Dear Eastwood,

    Thanks for your time and efforts to help me. I'm enclosing a sample workbook wherein I have incorporated your proposed VBA Code and my findings are as follows;

    1. Columns F5:F8 are linked with Cells E5:E8, whereas E5:E8 are linked with Sheet1 Corresponding Cells. When I change value of Cell E1=2, it turns the values of Cells F5:F8 into absolute numbers (as required).
    2. It also shifts the formula to Next Column cells G5:G8 (Link with the Corresponding cells of Column E) (as required).
    3. It also clear the entire range from Column F:Column T and again Link Column F with Corresponding Cells of Column E. (as required)
    4. But with the Change in values of Column E1, It also turns the values of Column E to Zero while shifting the formula to next column (not required) because Column E is linked with sheet1.Column E and this link should be remain intact at all time.
    5. I want to apply this VBA to extended ranges as as test case i tried to modify it but doesn't work for Ranges F5:F16 and F21:F50 in the sample workbook.
    6. if you guide me to modify the VBA code for F5:F16 and F21:F50, i'll apply it to other ranges at my own

    Regards,
    Attached Files Attached Files
    Last edited by leo73pk; 05-02-2020 at 04:44 PM.

  6. #6
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: VBA Code For shifting of formula to next Column

    Hi Leo,
    Thank you for responding, I am not sure I understand you point 4 correctly:
    So if you don't want E5 and below to be reset to 0, delete or Rem this line: Cells(i, 5) = 0

    and (point 5&6) for to work your code in the last workbook, change the following in the code:
    8 --> 16, 12 --> 21, 15 --> 50 (but not in this line: If [E1] > 1 And [E1] <= 15 Then )

    But I can make life easier for you: you only have to change R1 and R2 in following code when the Ranges are changing.
    Please Login or Register  to view this content.

    Cheers
    Erwin
    Last edited by Eastw00d; 05-03-2020 at 03:15 AM. Reason: additional information

  7. #7
    Forum Contributor leo73pk's Avatar
    Join Date
    09-28-2006
    Location
    UAE
    MS-Off Ver
    2016, Microsoft 365
    Posts
    294

    Re: VBA Code For shifting of formula to next Column

    Dear Eastwood,

    I appreciate your time and efforts to promptly responding to my queries. The recent code proposed by you is working as per requirement.
    However, when I tried to modify the vba code to add a New Range "54:84" by making following changes;

    Dim R1$, R2$, R3$
    ReDim rn(1)
    Dim i%, R$

    R1 = "5:16" '<--- to change
    R2 = "21:50" '<--- to change
    R3 = "54:84"
    rn(0) = Split(R1, ":")
    rn(1) = Split(R2, ":")
    rn(2) = Split(R3, ":")

    and save the code its gives runtime error 9.

    Do i need to amend the other areas of vba code as well if i want to add more ranges. (54:84, 87:101, 105:115, 120:124 and so forth) Please advise.
    Last edited by leo73pk; 05-03-2020 at 01:50 PM. Reason: elaboration

  8. #8
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: VBA Code For shifting of formula to next Column

    If you would have said that you had more ranges, I would have adapted the code immidiately in another way. Give me a little time to think about this.
    I need to rebuild the code.
    Cheers
    Erwin

  9. #9
    Forum Contributor leo73pk's Avatar
    Join Date
    09-28-2006
    Location
    UAE
    MS-Off Ver
    2016, Microsoft 365
    Posts
    294

    Re: VBA Code For shifting of formula to next Column

    My apologies for not elaborating it precisely. I'll be grateful if you also highlight which part to be modified in case I have to add more ranges in future.

  10. #10
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: VBA Code For shifting of formula to next Column

    This should do it:
    Please Login or Register  to view this content.
    Cheers
    Erwin

  11. #11
    Forum Contributor leo73pk's Avatar
    Join Date
    09-28-2006
    Location
    UAE
    MS-Off Ver
    2016, Microsoft 365
    Posts
    294

    Re: VBA Code For shifting of formula to next Column

    Dear Erwin,

    Thank you so much for your time and efforts to help me. Your proposed VBA works as per requirement really grateful.

    Best Regards,

+ 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] Data Validation- Formula Automatically Shifting Referenced Column
    By Ekazumon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-27-2020, 04:49 PM
  2. VBA code to remove duplicates without shifting cells
    By belpal in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2018, 08:59 PM
  3. [SOLVED] Is there a way to guide the VBA code so that it may just keep shifting from main sheet ...
    By omega0010 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-30-2015, 11:38 AM
  4. New code keeps shifting photos up when updating old photo
    By atomichybrid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2015, 06:33 PM
  5. [SOLVED] conditional shifting, deleting. VBA/Macro code
    By slxia1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-11-2012, 10:11 AM
  6. Code for range shifting based on month
    By kw42chan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-03-2012, 03:26 AM
  7. Shifting Last Name To Another Column
    By AdGuy in forum Excel General
    Replies: 4
    Last Post: 02-05-2005, 12:22 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