+ Reply to Thread
Results 1 to 13 of 13

VBA - Define Selection using cell reference

Hybrid View

  1. #1
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    VBA - Define Selection using cell reference

    Is it possible to use VBA to copy/paste data from a sheet and define the area being copied with a cell reference? In the attached example I want to copy A2:C11. Using F1 to work out how many the number of rows to select. Hope this makes sense!?

    example 1.xlsx

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: VBA - Define Selection using cell reference

    Try:

    Range("A2:C" & 1 + Range("F1").Value).Copy Destination:=Range(YouLikeToPasteTo)
    Will it always start at Range A2?
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: VBA - Define Selection using cell reference

    you could do

    Sub doit()
    
    Sheets("Sheet2").Range("A1:C" & Sheets("Sheet1").Range("F1").Value).Value = Sheets("Sheet1").Range("A2:C" & (Sheets("Sheet1").Range("F1").Value + 1)).Value
    
    End Sub

  4. #4
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: VBA - Define Selection using cell reference

    Yes, you can do that. See the attached example (I'm putting the values in F2 for the example)


    Option Explicit
    Sub pasteChoosenRows()
    Dim rowsToUse As Long
    Dim source As Range, destination As Range
    
    
    'Adjust these to the real sheet'
    rowsToUse = ActiveSheet.Range("F1").Value
    Set source = ActiveSheet.Range("A2")
    Set destination = ActiveSheet.Range("F2")
    
    'Align the values'
    
    destination.Resize(rowsToUse, source.End(xlToRight).Column - source.Column + 1).Value = _
    source.Resize(rowsToUse, source.End(xlToRight).Column - source.Column + 1).Value
    
    End Sub
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: VBA - Define Selection using cell reference

    Range("A2:C" & (Range("A2").Row + Range("F1").Value - 1)).Copy Range("H2")

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  6. #6
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: VBA - Define Selection using cell reference

    Got it working now thanks guys

  7. #7
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: VBA - Define Selection using cell reference

    Sorry to be a pain I managed to get it working using the code below but how would I move between sheets. So I want to run the macro from Sheet 2, then use the macro to copy the data from Sheet 1 to Sheet 2.

    Range("A2:C" & 1 + Range("F1").Value).Copy Destination:=Range(YouLikeToPasteTo)

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: VBA - Define Selection using cell reference

    Thanks for the rep.

    You need to fully qualify all the addresses.

    Try:

    Sheets("Sheet1").Range("A2:C" & 1 + Sheets("Sheet1").Range("F1").Value).Copy Destination:=Sheets("Sheet2").Range(YouLikeToPasteTo)

    Regards, TMS



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  9. #9
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: VBA - Define Selection using cell reference

    That works now thanks, one last question for an issue I did not foresee.. (sorry!)

    The cells I am copying contain formulas so it's only copying the formula and not the values, how do I integrate paste special into this code?

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: VBA - Define Selection using cell reference

    You'd probably have to split it into a separate Copy followed by a Paste Special | Values


    Regards, TMS

  11. #11
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: VBA - Define Selection using cell reference

    Sorry I'm not sure exactly what you mean?

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: VBA - Define Selection using cell reference

    Option Explicit
    
    Sub test()
    
    Dim YouLikeToPasteTo As String
    YouLikeToPasteTo = "H1"
    
    Sheets("Sheet1").Range("A2:C" & _
        1 + Sheets("Sheet1").Range("F1").Value).Copy _
        Destination:=Sheets("Sheet2").Range(YouLikeToPasteTo)
    
    End Sub
    
    Sub test2()
    
    Dim YouLikeToPasteTo As String
    YouLikeToPasteTo = "H1"
    
    Sheets("Sheet1").Range("A2:C" & _
        1 + Sheets("Sheet1").Range("F1").Value).Copy
    
    With Sheets("Sheet2")
        With .Range(YouLikeToPasteTo)
            .PasteSpecial (xlPasteValues)
        End With
    End With
    Application.CutCopyMode = False
    
    End Sub

    Regards, TMS

  13. #13
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: VBA - Define Selection using cell reference

    Thanks so much it's working fine now, I have another question but I feel as though I'm going off in another tangent so will start a new thread.

+ 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. Different sheet refernce using a formula to define the cell reference
    By tritty in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-06-2013, 11:16 PM
  2. [SOLVED] Use Cell Reference to Define Sum Ranges Across Different Sheets
    By jwhite0720 in forum Excel General
    Replies: 2
    Last Post: 06-29-2012, 11:34 AM
  3. How to define a cell name and reference it in a differen sheet
    By shaunsul in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-26-2011, 01:27 PM
  4. Define Name to current selection
    By azureriders in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2008, 11:02 PM
  5. Define Name to current selection
    By Eduard in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-25-2008, 10:58 AM

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