+ Reply to Thread
Results 1 to 4 of 4

Simplied VBA code to copy value of range from another worksheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Simplied VBA code to copy value of range from another worksheet

    I am working on a macro that used columns as number (i.e. A=1, B=2) in the VBA code and I am trying to see if I can simplified my code to copy the value of a range from another worksheet to a range on a specified worksheet. Both range are identical in size.
    I was able to first get it to work within the same worksheet with this code:

     Range(Cells(1, 1), Cells(1, 4)).Value = Range(Cells(1, 10), Cells(1, 13)).Value 'working
    But then I thought I could just do the below code and it would work with a range in another worksheet but I got an error (run-time error 9 subscript out of range):

     Sheets("Sheet1").Range(Cells(1, 1), Cells(1, 4)).Value = Sheets("Sheet2").Range(Cells(1, 10), Cells(1, 13)).Value 'Not Working
    I search the web and ultimately came up with the code below that works but I was wondering if it can simplified to a one liner similar to the code above?

    'Working
    Sheets("Sheet2").Activate
    ActiveSheet.Range(Cells(1, 10), Cells(1, 13)).Copy
    Sheets("Sheet1").Activate
    Range(Cells(1, 1), Cells(1, 4)).Select
    ActiveSheet.Paste
    Thank you for your time!

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: Simplied VBA code to copy value of range from another worksheet

    How about this.

    Sheets("Sheet2").Range(Sheets("Sheet2").Cells(1, 1), Sheets("Sheet2").Cells(1, 4)).Copy Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, 10), Sheets("Sheet1").Cells(1, 13))
    Last edited by bakerman2; 02-03-2017 at 08:04 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,913

    Re: Simplied VBA code to copy value of range from another worksheet

    The reason you are getting error in your 2nd code is that you didn't qualify Cells with sheet name.
    Below will work...
    Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, 1), Sheets("Sheet1").Cells(1, 4)).Value = Sheets("Sheet2").Range(Sheets("Sheet2").Cells(1, 10), Sheets("Sheet2").Cells(1, 13)).Value
    But I'd write it like...
    Sub Test()
    Dim dWs As Worksheet
    Dim sWs As Worksheet
    Set dWs = Sheets("Sheet1")
    Set sWs = Sheets("Sheet2")
    dWs.Range(dWs.Cells(1, 1), dWs.Cells(1, 4)).Value = sWs.Range(sWs.Cells(1, 10), sWs.Cells(1, 13)).Value
    End Sub
    For copy method you can write as...
    With Sheets("Sheet2")
        .Range(.Cells(1, 10), .Cells(1, 13)).Copy Sheets("Sheet1").Cells(1, 1)
    End With
    Edit: FYI - Range or Cells when unqualified will use activesheet as range parent.
    Last edited by CK76; 02-03-2017 at 01:59 PM. Reason: See Edit:

  4. #4
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Re: Simplied VBA code to copy value of range from another worksheet

    Great, thank you all for your answer, I used CK76 first proposed answer and it worked. Thank you for your time.

+ 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] Simplified vba code to copy value of range from another Worksheet
    By Chrispelletier in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2017, 01:22 PM
  2. [SOLVED] Simplified vba code to copy value of range from another Worksheet
    By Chrispelletier in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-03-2017, 11:00 AM
  3. [SOLVED] vba code to copy and paste used range to new worksheet, sheettab to be renamed basedon cel
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-15-2016, 01:40 PM
  4. Replies: 13
    Last Post: 10-05-2015, 08:59 AM
  5. Creating a VBA code to copy a range of cells from one part of a worksheet to another
    By amconner in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-18-2015, 03:43 PM
  6. Vba code to copy and paste second visible cell to other worksheet range then the next
    By Justin25150 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2012, 09:03 AM
  7. VBA Code to copy a "criteria based" range of cells from worksheet A to worksheet B
    By xbaglione in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-09-2010, 10:37 AM

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