+ Reply to Thread
Results 1 to 13 of 13

Auto fill a simple formula using a macro

  1. #1
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Auto fill a simple formula using a macro

    Hi,

    I cant seem to come across this issue yet on the internet (altough Im sure it has to be there somewhere).

    I have a workbook with a "Total" sheet and then 52 weekly sheets. Currently I paste a report into the weekly sheets, then use a macro to add any missing rows in the sequence (1,2,3,6 ..so a blank row for 4 & 5 is added).

    I cant seem to figure out how to set up the total sheet with a formula to copy over the numbers from the weekly sheet. It is only a simple like for like copy of 1 row of data in Column C, Each attempt at a formula cant account for the added blank rows and show as REF.

    I know I could just add the formula (=sheet1!C1 etc) after running the macro on the weekly sheets but I would like to have something or someway that avoids me having to add the formula new each time to the total page.

    I was thinking of adding a macro button to the total page that would add a formula to each row in a column. Anyone have any suggestions how I could do this or which macro to use ?

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Auto fill a simple formula using a macro

    I would suggest you upload a small sample of your workbook (total with 3 weekly sheets) to give a better indication of your needs.

    Alf

  3. #3
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Auto fill a simple formula using a macro

    Example 1.xlsm

    Hopefully that worked.

    As you can see in 'total' D12 shows as #'REF' as I added a blank row in sheet KW3. Column F is the totals before I use the macro on the sheet.

    If you use the macro on sheet KW4 you can see the effect I am trying to avoid. Also on another point the macro change the lines in Column A to 1-33 (they are 101-133 now) can this be avoided ?

  4. #4
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Auto fill a simple formula using a macro

    Is there other info needed that would help people ?

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Auto fill a simple formula using a macro

    For a start perhaps a macro like this could be of help?

    Please Login or Register  to view this content.
    This will loop through all sheets from KW2 to KW52 (in this case) and check if the range A1 to C1 contains more than 2 values. If so it will copy the values in column C to sheet "Total"

    Alf

  6. #6
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Auto fill a simple formula using a macro

    Thanks Alf.

    That looks like it has worked perfectly.

  7. #7
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Auto fill a simple formula using a macro

    actually just tried it again in the full sheet format and when I click it it says....Run-time error '9' subscript out of range...

    What might of I done wrong ?

    I have sheets from KW1 to KW52

  8. #8
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Auto fill a simple formula using a macro

    scratch that. Found the error. Changed the code to:

    Sub CopyVal()
    Dim i As Integer

    Sheets("Total").Activate
    For i = 1 To Sheets.Count - 52

    Sheets("KW" & i).Activate

    If WorksheetFunction.CountA(Range("A1:C1")) > 2 Then
    Range("C1:C" & Range("C" & Rows.Count).End(xlUp).Row).Copy
    Sheets("Total").Activate
    Cells(4, 2 + i).PasteSpecial Paste:=xlPasteValues
    End If

    Next

    Application.CutCopyMode = False

    Sheets("Total").Activate

    End Sub


    Seems to work fine now. Had to change the number of sheets to 52 and the paste to cell to another one over. Looks to work fine now. (i hope )

  9. #9
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Auto fill a simple formula using a macro

    Scratch that. scratch that. It works but only till KW3, after that the macro doesnt add any more detail to the 'Totals" page

    Im sure I just messed up the code how can I change "Cells(4, 2 + i).PasteSpecial Paste:=xlPasteValues" to work basically right up page for page until 52 ?

  10. #10
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Auto fill a simple formula using a macro

    Please Login or Register  to view this content.
    I hope the -52 is a slip up. If you have 52 sheets + sheet "Total" then command
    Please Login or Register  to view this content.
    will give you 53 as the number but your last sheet is KW52

    so the loop is
    Please Login or Register  to view this content.
    You should also check the line
    Please Login or Register  to view this content.
    as this pastes the copied value to row 4 and column 2 + i So for i = 1 result get pasted to C4, and next i = 2 get pasted to D4 and so forth.

    As this seem to clear up your problem could you please mark your thread "Solved"


    Alf

    Ps Thanks for feed back and rep!

    To mark your thread solved do the following:
    - Go to the first post
    - Click edit
    - Click Advance
    - Just below the word "Title:" you will see a dropdown with the word No prefix.
    - Change to Solve
    - Click Save

  11. #11
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Auto fill a simple formula using a macro

    Quote Originally Posted by Alf View Post
    Please Login or Register  to view this content.
    I hope the -52 is a slip up. If you have 52 sheets + sheet "Total" then command
    Please Login or Register  to view this content.
    will give you 53 as the number but your last sheet is KW52

    so the loop is
    Please Login or Register  to view this content.
    You should also check the line
    Please Login or Register  to view this content.
    as this pastes the copied value to row 4 and column 2 + i So for i = 1 result get pasted to C4, and next i = 2 get pasted to D4 and so forth.

    As this seem to clear up your problem could you please mark your thread "Solved"


    Alf

    Ps Thanks for feed back and rep!
    Hi,

    I changed the code back to your suggestions above and when I run it I get the "run-time error '9' subscript isout of range message again.

    ?

    Sheets("Total").Activate
    For i = 1 To Sheets.Count - 1

    Sheets("KW" & i).Activate

    If WorksheetFunction.CountA(Range("A1:C1")) > 2 Then
    Range("C1:C" & Range("C" & Rows.Count).End(xlUp).Row).Copy
    Sheets("Total").Activate
    Cells(4, 3 + i).PasteSpecial Paste:=xlPasteValues
    End If

    Next

    Application.CutCopyMode = False

    Sheets("Total").Activate

    End Sub

    I have a Totals sheet, Sheet 1, Sheet 2 and then KW1 - KW52 if that matters.

  12. #12
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Auto fill a simple formula using a macro

    Yes that matters remember
    Please Login or Register  to view this content.
    counts all the sheets in your workbook so you got 55 sheets but you wish i to go from 1 to 52 so you either use

    Please Login or Register  to view this content.
    or you could also use

    Please Login or Register  to view this content.
    perhaps easier. I'm so used to "Sheets.Count" that I do it without thinking as I've done now!

    Alf

    You get "subscription out of range" because 55 - 1 = 54 so macro tries to activate sheet "KW53" and since this sheet does not excists you get the runtime error
    Last edited by Alf; 02-26-2013 at 04:43 PM.

  13. #13
    Registered User
    Join Date
    02-18-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Auto fill a simple formula using a macro

    Thats solved it.

    Thanks Alf. Brillant help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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