+ Reply to Thread
Results 1 to 9 of 9

Using a formula within a Macro but with varying row amounts

  1. #1
    Registered User
    Join Date
    10-06-2010
    Location
    Cheshunt
    MS-Off Ver
    MS Office 365 Business
    Posts
    13

    Using a formula within a Macro but with varying row amounts

    Hi everyone

    Hoping someone can help me, please!

    I have a standard report that I produce, which is a tidied up version of raw data that is sent to me. I therefore have a Macro that I can apply that gets me a long way down the line for all the things that I would otherwise do for every report.

    The reports vary in their number of rows - anything from 50 to 9,000.

    There are two things that I do that I have not been able to include in the Macro because they they involve formulas that need to be copied to the bottom of the report. So if I try to include them, all that happens in the Macro is that the formula is copied to the exact number of rows that I happened to have when I recorded the Macro.

    Is there a way of telling the Macro that when I copy the formula down, it should go as far as the last row of data?

    Thanks a lot and let me know if you need more info!

    Neal

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,446

    Re: Using a formula within a Macro but with varying row amounts

    Administrative note

    Still using XL2003 as indicated in your profile?
    If not,perhaps update your profile as necessary to properly reflect the exact version(s) of Excel your question relates to? Members tailor answers based on your Excel version.
    Thanks

  3. #3
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444

    Re: Using a formula within a Macro but with varying row amounts

    Hi,

    You can identify the last row that contains data like this (Column A for example)

    Please Login or Register  to view this content.
    You could assign that value to a variable, like this..

    Please Login or Register  to view this content.
    and then use it to define the range that you want the macro to work on like this

    Please Login or Register  to view this content.
    I hope that gets you started..
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  4. #4
    Registered User
    Join Date
    10-06-2010
    Location
    Cheshunt
    MS-Off Ver
    MS Office 365 Business
    Posts
    13

    Re: Using a formula within a Macro but with varying row amounts

    Sorry, didn't realise.. it's Microsoft Office 365 Business, I'll update it now!

  5. #5
    Registered User
    Join Date
    10-06-2010
    Location
    Cheshunt
    MS-Off Ver
    MS Office 365 Business
    Posts
    13

    Re: Using a formula within a Macro but with varying row amounts

    Quote Originally Posted by sweep View Post
    Hi,

    You can identify the last row that contains data like this (Column A for example)

    Please Login or Register  to view this content.
    You could assign that value to a variable, like this..

    Please Login or Register  to view this content.
    and then use it to define the range that you want the macro to work on like this

    Please Login or Register  to view this content.
    I hope that gets you started..

    Thank you, and I'm sorry for my limited knowledge, but I don't really know where I put those formulas and at what stage?

    I think I understand what you've written but am unsure how to use it in the process.

    Thanks a lot for your time

  6. #6
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444

    Re: Using a formula within a Macro but with varying row amounts

    You'll have to amend your macro with this code - or something similar to it. Can you post the macro that you have recorded? To view the VBA editor you can press ALT&F11.

  7. #7
    Registered User
    Join Date
    10-06-2010
    Location
    Cheshunt
    MS-Off Ver
    MS Office 365 Business
    Posts
    13

    Re: Using a formula within a Macro but with varying row amounts

    Thanks - here's what came up.

    I have just recorded the Macro by starting to record and then running through the tasks, so don't usually use this screen!


    Sub Rankingreport()
    '
    ' Rankingreport Macro
    '

    '
    ActiveCell.Offset(0, -6).Columns("A:E").EntireColumn.Select
    Selection.ColumnWidth = 18.43
    Selection.ColumnWidth = 15.86
    ActiveCell.Select
    ActiveCell.FormulaR1C1 = "Postal sector"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Target HH"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Total HH"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Penetration"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-1]"
    ActiveCell.Columns("A:A").EntireColumn.Select
    Selection.NumberFormat = "0.00%"
    ActiveCell.Offset(1, 1).Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.NumberFormat = "0.00"
    Selection.NumberFormat = "0.0"
    Selection.NumberFormat = "0"
    ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select
    Selection.Font.Bold = True
    ActiveCell.Offset(6, 3).Range("A1").Select
    End Sub

  8. #8
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444

    Re: Using a formula within a Macro but with varying row amounts

    OK,

    First order of business - can you take a look at adding code tags to your post so it formats correctly?

    Next, your macro seems to do the following:

    The column width is set to 15.43 for A:E
    In row 1, add headers to columns A through D
    In column D, the formula of =B1/C1 is inserted
    Column D is set to percentage format
    Row 1 is made bold

    I guess that you want the formula copied down the length of the data?

  9. #9
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,446

    Re: Using a formula within a Macro but with varying row amounts

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)

+ 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] Counting Cells with Varying amounts of data
    By bradydecouto in forum Office 365
    Replies: 3
    Last Post: 11-07-2018, 03:38 PM
  2. Column matches on separate sheets to return varying amounts
    By arthurphil in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-25-2015, 07:14 AM
  3. [SOLVED] Compound daily interest for varying amounts of child support
    By trout14 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 08-10-2012, 09:46 AM
  4. Line Graph with varying amounts
    By CrzyBoy2599 in forum Excel General
    Replies: 10
    Last Post: 02-09-2012, 04:28 AM
  5. Pulling large amounts of data from varying tabs
    By bjvick in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-14-2010, 05:24 PM
  6. Copy varying amounts of data between sheets
    By amuza in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-07-2008, 06:50 PM
  7. Remove varying amounts of space characters
    By Access Joe in forum Excel General
    Replies: 4
    Last Post: 01-13-2006, 06:30 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