+ Reply to Thread
Results 1 to 4 of 4

Problem setting values for a range

Hybrid View

ola7mat Problem setting values for a... 02-21-2019, 06:56 AM
banaanas Re: Problem setting values... 02-21-2019, 07:33 AM
mehmetcik Re: Problem setting values... 02-21-2019, 07:38 AM
ola7mat Re: Problem setting values... 02-21-2019, 08:42 AM
  1. #1
    Registered User
    Join Date
    06-07-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    42

    Problem setting values for a range

    Hello,

    I am trying to set some values for a range but am a bit stuck.
    When using this code it works fine:
    Workbooks("Book2.xlsx").Sheets("Sheet1").Range(Cells(1,1), Cells(1, 2)).Value = Workbooks("Book1.xlsx").Sheets("Sheet1").Range("A1:B1").Value
    But when using this code i get a Run Time Error 1004
    Workbooks("Book2.xlsx").Sheets("Sheet1").Range(Cells(1,1), Cells(1, 2)).Value = Workbooks("Book1.xlsx").Sheets("Sheet1").Range(Cells(1,1), Cells(1, 2)).Value
    Can any body help me here and point out why this produces an error, i want to use the second option to set values for a variable range and prefer to use the Cells() method?

    Thanks!

  2. #2
    Forum Contributor
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    365
    Posts
    199

    Re: Problem setting values for a range

    Could you provide your workbooks? Would be easier that way.
    I think this might be caused because you need to clarify what workbook and cells you are refeering to, and the later part of your code is trying to reference the book2 cells.

    You could try to add this bit of code at the start of the module:

    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    
    Set wb1 = Workbooks("Book1.xlsx") 'first workbook
    Set wb2 = Workbooks("Book2.xlsx") 'The other workbook
    
    Set ws1 = wb1.Worksheets("Sheet1")
    Set ws2 = wb2.Worksheets("Sheet1")
    And replace that line of code with:

    ws2.Range(ws2.Cells(1, 1), ws2.Cells(1, 2)).Value = ws1.Range(ws1.Cells(1, 1), ws1.Cells(1, 2)).Value
    As you can see the difference is that my line is refering to a specific workbooks worksheets cells.

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Problem setting values for a range

    Try


    
    CopyTo = Range(Cells(1, 1), Cells(1, 2)).Address
    CopyFrom = Range(Cells(1, 1), Cells(1, 2)).Address
    
    Workbooks("Book2.xlsx").Sheets("Sheet1").Range(CopyTo).Value = Workbooks("Book1.xlsx").Sheets("Sheet1").Range(CopyFrom).Value
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  4. #4
    Registered User
    Join Date
    06-07-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: Problem setting values for a range

    Thank you very much i think you helped me figure it out.

    The issue was the way i selected my range, i was using
    wb1.ws1.Range(Cells(x, y), Cells(x, y))
    Whereas, the way your wrote it using works just fine
    Range(ws1.Cells(x, y), ws1.Cells(x, y))
    I tried it and all working! thanks again.

+ 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. Replies: 11
    Last Post: 07-15-2012, 04:08 PM
  2. Problem with setting range in Macro
    By floridagunner in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-21-2012, 10:05 PM
  3. Problem setting range in macro based on todays date.
    By shakeyjake200 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-15-2009, 12:48 AM
  4. Problem When Setting Checkbox Values
    By rcoates in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2009, 06:44 PM
  5. Range setting problem
    By 63falcon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-09-2007, 04:37 PM
  6. Problem setting SeriesCollections source range
    By Lead Foot in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-27-2006, 01:15 AM
  7. [SOLVED] Problem setting RGB Values with ShapeRange
    By Bob Gibbons in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-26-2005, 06:05 PM

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