Consulting

Results 1 to 4 of 4

Thread: Solved: Create 4 cell range to copy

  1. #1
    VBAX Regular
    Joined
    May 2010
    Posts
    12
    Location

    Solved: Create 4 cell range to copy

    [VBA]Sub fourcellcopy()
    '
    ' fourcellcopy Macro
    '
    ' Keyboard Shortcut: Ctrl+d
    '
    Range("J868:M868").Select
    Selection.Copy
    End Sub[/VBA]

    So I want to make a quick little macro that allows me to use a keyboard shortcut to select the cell which i am in and the three cells immediately to the right, and then copy them. My only problem is that obviously in the above code selects a specific range, where as I want it to point to the cell I have highlighted at the time and the three adjacent ones. Can somebody just enlighten me as to how to do this please?

    Many thanks.

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Try:
    [vba]ActiveCell.Resize(, 4).Copy[/vba]
    Last edited by GTO; 05-19-2010 at 03:36 AM. Reason: Evidently I'm now 'reading challenged'...

  3. #3
    VBAX Regular
    Joined
    May 2010
    Posts
    12
    Location
    Ah yes thanks very much for that. I changed the 3 to 4 so that it grabbed the active cell AND the 3 cells to the left. Although obviously this can just be changed to whatever I suppose.

    [VBA]ActiveCell.Resize(, 4).Copy [/VBA]

    This worked a treat though! I am a complete novice at VBA but hopefully everytime I ask a dumb question I'll learn something in the process until eventually I don't need to ask so many dumb questions!

    Just out of interest if you wanted to specify cells above or below the active cell as well, what are the VBA codes for that kind of operation?

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can combine Offset and Resize to define a range
    eg
    [VBA]
    ActiveCell.Offset(-3,2).Resize(5, 4).Copy
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •