+ Reply to Thread
Results 1 to 6 of 6

Help - error code

Hybrid View

  1. #1
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Help - error code

    Hi,

    I'm making a macro that always ends up with a error when executes the last line of code that I put bellow. Can anyone explain me why it ends up in a error in that line? The letters 'a' and 'b' are two variables that contains the numbers 25 and 28 respectively.


    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = InputBox("Name for the new sheet:", "Create new sheet")
    NameNewSheet = Sheets(Sheets.Count).Name
    
    ...
    
    Sheets("2012").Range(Cells(1, a), Cells(150, b)).Copy Destination:=Sheets(NameNewSheet).Range("A:A:D:D")

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Help - error code

    Hi,

    First off, this is wrong:

    Range("A:A:D:D")
    Are you looking for:

    Range("A:D")
    Additionally you are trying to copy a large range into a smaller range. Can't do that either. The ranges have to be the same.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Help - error code

    Hmm I changed to the line bellow but it still doesn't work =\

    Sheets("2012").Range(Cells(1, a), Cells(150, b)).Copy Destination:=Sheets(NameNewSheet).Range("A1:D150")

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Help - error code

    My guess would be:

    Sheets("2012").Range(Cells(1, a), Cells(150, b)).Copy Destination:=Sheets(NameNewSheet).Range("A1:D150")
    Should be:
    With Sheets("2012")
        .Range(.Cells(1, a), .Cells(150, b)).Copy Destination:=Sheets(NameNewSheet).Range("A1:D150")
    End with
    So that you have dots in front of Cells.

    (Assuming that 'a' and 'b' are long variables, and you don't mean: "A" & "D".)
    Last edited by StevenM; 05-03-2012 at 01:02 PM.

  5. #5
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Help - error code

    It works

    Tkz both

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Help - error code

    If you are satisfied with the responses then please mark the thread as solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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