+ Reply to Thread
Results 1 to 7 of 7

Thread: Dynamic Range

  1. #1
    Registered User
    Join Date
    10-26-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    2

    Dynamic Range

    Hi guys,

    I am rather new to all of this and would appreciate some help.

    I am trying to create a dynamic range, but just cant get it right:

    I want to get the autofill feature for C column stop once it has reached the same number of row entries in column A.

    ********************************************************
    Code:
    Sub TestDynamicRange()
    '
    ' TestDynamicRange Macro
    ' TestDynamicRange
    '
    
    '
        Range("A1:B6").Select
        Selection.Copy
        Sheets("Sheet2").Select
        ActiveSheet.Paste
        Range("C1").Select
        Application.CutCopyMode = False
        ActiveCell.FormulaR1C1 = "Date"
        Range("C2").Select
        ActiveCell.FormulaR1C1 = "=TODAY()"
        Range("C2").Select
        Selection.AutoFill Destination:=Range("C2:C6"), Type:=xlFillDefault
        Range("C2:C6").Select
    End Sub
    **********************************************************

    Can anyone help me please?

    Thanks,

    Matt
    Last edited by royUK; 10-26-2009 at 09:07 AM.

  2. #2
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,431

    Re: Dynamic Range Help Please

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    I will add them this time only, be sure to read the Rules.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  3. #3
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,431

    Re: Dynamic Range Help Please

    Why not add the Dynamic range manually?

    Do you want =TODAY() or a fixed date?
    Last edited by royUK; 10-26-2009 at 09:11 AM.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Dynamic Range Help Please

    Maybe something like this:
    Code:
    Sub TestDynamicRange()
    Dim LR As Long
    
    Range("A1:B6").Copy
    Sheets("Sheet2").Select
    ActiveSheet.Paste
    Range("C1") = "Date"
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Range("C2:C" & LR).Formula = "=TODAY()"
    
    End Sub
    Putting in =TODAY() into those cells will result in a date that will keep changing. If you want the date to stay the same the next time you open the sheet, change that last line of code to:
    Code:
    Range("C2:C" & LR)= Date
    _________________
    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!)

  5. #5
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    5,826

    Re: Dynamic Range Help Please

    Hi Matt,

    One way would be to create a variable to record the number of rows in column A. Assuming the column A values start in row 2 use something like

    Code:
    Dim iARows as Integer
    iARows = Range("A2:A" & Range("A" & Application.Rows.Count).End(xlup).Row))
    Then substitute

    Code:
    Range("C2:C" & iArows)
    
    for
    
    Range("C2:C6")
    Incidentally, you don't need all the .Selects. It:s more effcient just to address the ranges in question. e.g.

    Code:
    Dim iARows as Integer
    iARows = Range("A2:A" & Range("A" & Application.Rows.Count).End(xlup).Row))
    
    Range("A1:B6").Copy
    Sheets("Sheet2").Range("C1").PasteSpecial(xlPasteAll)
    Application.CutCopyMode = False
    Sheets("Sheet2").Range("C1") = "Date"
        Sheets("Sheet2").Range("C2:C" & iArows) = "=TODAY()"
    Untested since I only have access to Excel for Mac 2008 at the moment and in their infinite wisdom Microsoft are no longer supporting VBA with Excel!
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  6. #6
    Registered User
    Join Date
    10-26-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Dynamic Range Help Please

    That worked a treat. Thanks guys!

    Matt

  7. #7
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Dynamic Range Help Please

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)
    _________________
    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!)

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.2.0