+ Reply to Thread
Results 1 to 7 of 7

macro to insert 1 column with formulas everytime it run

Hybrid View

aravindhan_31 macro to insert 1 column with... 06-06-2009, 03:06 AM
JBeaucaire Re: macro to insert 1 column... 06-06-2009, 11:06 AM
aravindhan_31 Re: macro to insert 1 column... 06-06-2009, 11:14 AM
JBeaucaire Re: macro to insert 1 column... 06-06-2009, 11:29 AM
aravindhan_31 Re: macro to insert 1 column... 06-07-2009, 09:51 AM
JBeaucaire Re: macro to insert 1 column... 06-07-2009, 11:20 AM
JBeaucaire Re: macro to insert 1 column... 06-07-2009, 11:44 AM
  1. #1
    Forum Contributor
    Join Date
    03-28-2008
    Location
    India, bangalore
    MS-Off Ver
    Excel 2003,2007
    Posts
    216

    macro to insert 1 column with formulas everytime it run

    Hi,

    I would appreicate verymuch if you guys help me on this.

    When even i run a macro a new column has to be inserted with the same formulas of previous column to get the updated data.

    I have attached a file with the detail explanation and examples

    thanks again...

    Regards
    Arvind
    Attached Files Attached Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro to insert 1 column with formulas everytime it run

    The problem you're going to have is that if you leave the formulas in the columns, the current week column will updated its values even right up to the moment we run the macro. So I'm going to suggest that you use the formulas to collate the data and then immediately remove the formulas. This way when you run the macro, it gives you an unchanging picture of the moment you ran it.

    Does that make sense?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    03-28-2008
    Location
    India, bangalore
    MS-Off Ver
    Excel 2003,2007
    Posts
    216

    Re: macro to insert 1 column with formulas everytime it run

    Hi, thanks for your response,

    It would really help me If I get a macro, as u said i will use the formula and remove.

    Thanks

    Arvind

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro to insert 1 column with formulas everytime it run

    Give this a try. Make sure the data on Sheet2 is ready to evaluate and click the button, it will add the new sections and static values.

    If you want to leave the formulas, remove or comment out the
     .value = .value
    ...section of the macro.
    Last edited by JBeaucaire; 06-07-2009 at 11:44 AM. Reason: Sheet removed...see below for latest version

  5. #5
    Forum Contributor
    Join Date
    03-28-2008
    Location
    India, bangalore
    MS-Off Ver
    Excel 2003,2007
    Posts
    216

    Re: macro to insert 1 column with formulas everytime it run

    Hi,

    Thanks a lot its working... however I made some changes on the code to work on my data. Its really amazing

    I have attached my data for your reference, When I run the macro everythings works fine except the Wk name, each time it gives me the same Wk. how do i change to get Wk1 Wk2 Wk3 etc.

    and how do I add total of the results?

    Thanks again for your help

    Arvind
    Attached Files Attached Files

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro to insert 1 column with formulas everytime it run

    Go back and look at the Wk column in my uploaded sheet. To make incrementing Wks easy, I actually removed the alpha characters and formatted the cell with a custom number format and a formula. There's no actual text in that cell. The first Wk 1 cell is actually a formula. You could just do a COPY/PASTE of of my cell onto yours and it would take all the fun formatting with it.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro to insert 1 column with formulas everytime it run

    Looking at your sheet, I think we can simplify down the formula insertion to one step, too. Since the sheet names match the column A values exactly, we can use an INDIRECT() formula for the match and just put them all in in one step.
    Option Explicit
    
    Sub WeeklyUpdate()
    Dim LC As Long
    Application.CutCopyMode = False
    LC = Cells(2, Columns.Count).End(xlToLeft).Column
        
        If Cells(2, LC) = "Difference" Then
            Range(Cells(2, LC - 1), Cells(19, LC)).Copy Cells(2, LC)
            Range(Cells(3, LC + 1), Cells(18, LC + 1)).FormulaR1C1 = "=RC[-1]-RC[-2]"
            Range(Cells(3, LC), Cells(18, LC)).FormulaR1C1 = "=SUM(INDIRECT(RC1&""!$H:$H""))"
            
            With Range(Cells(3, LC), Cells(18, LC + 1))
                .Value = .Value
            End With
            
        Else
            Range(Cells(2, LC), Cells(19, LC)).Copy Cells(2, LC + 1)
            Range(Cells(2, LC), Cells(19, LC)).Copy Cells(2, LC + 2)
            Cells(2, LC + 2) = "Difference"
            Range(Cells(3, LC + 2), Cells(19, LC + 2)).FormulaR1C1 = "=RC[-1]-RC[-2]"
            Range(Cells(3, LC + 1), Cells(18, LC + 1)).FormulaR1C1 = "=SUM(INDIRECT(RC1&""!$H:$H""))"
            
            With Range(Cells(3, LC + 1), Cells(18, LC + 2))
                .Value = .Value
            End With
        End If
      
    Range("A1").Select
    End Sub
    I also corrected an error in the second half's column-targeting.
    Attached Files Attached Files

+ 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