+ Reply to Thread
Results 1 to 12 of 12

Run-time error '1004': Select method of Range class failed.

Hybrid View

  1. #1
    Registered User
    Join Date
    11-19-2012
    Location
    Hollywood, FL
    MS-Off Ver
    Excel 2010
    Posts
    7

    Run-time error '1004': Select method of Range class failed.

    Hello,

    I have a button in one of my sheets that calls on a Sub in Module1 and the following is the code. I am using variables to set ranges, but I am getting this error. Please assist.

    Sub MoveData()
    
    Dim SheetName As String
    Dim DataARange As Range
    Dim DataBRange As Range
    
    Set DataARange = Range(Range("DataA").Value)
    Set DataBRange = Range(Range("DataB").Value)
    
    Sheets("NoName1").Select
    SheetName = "Paper " & Range("A19").Value
            Sheets("NoName2").Select
            Range("B20:B31").Select
            Selection.Copy
            Sheets(SheetName).Select
            DataARange.Select                                    'This is where the error occurs.
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=True
            Sheets("NoName2").Select
            Range("C20:C31").Select
            Selection.Copy
            Sheets(SheetName).Select
            DataBRange.Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=True
    Thanks in advance.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '1004': Select method of Range class failed.

    Which worksheet are DataRangeA and DataRangeB meant to be on?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    11-19-2012
    Location
    Hollywood, FL
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run-time error '1004': Select method of Range class failed.

    They are meant to be on any of the sheets that the variable "SheetName" dictates.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '1004': Select method of Range class failed.

    As it is they'll be on whatever sheet is active when the code is run.

    If that's not the sheet named whatever is in SheetName then the code will error.

  5. #5
    Registered User
    Join Date
    11-19-2012
    Location
    Hollywood, FL
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run-time error '1004': Select method of Range class failed.

    Range("DataA").Value = B7
    I don't understand why I am still getting the error. I'm sure these sheets have a range "B7"....

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '1004': Select method of Range class failed.

    What sheet is DataA on?

    What sheet is active when the code runs?

  7. #7
    Registered User
    Join Date
    11-19-2012
    Location
    Hollywood, FL
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run-time error '1004': Select method of Range class failed.

    It is on a completely different sheet, but it is hidden. Could this be the problem?

  8. #8
    Registered User
    Join Date
    11-19-2012
    Location
    Hollywood, FL
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run-time error '1004': Select method of Range class failed.

    I tried running the same code several times and it works half the time and gets the error the half of the time.

  9. #9
    Registered User
    Join Date
    11-19-2012
    Location
    Hollywood, FL
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run-time error '1004': Select method of Range class failed.

    Nevermind. Problem persists even with the worksheet not hidden.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '1004': Select method of Range class failed.

    Can you attach a sample workbook?

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '1004': Select method of Range class failed.

    The problem is because the ranges in the code have no worksheet references so they will
    always refer to the active sheet.

    When you run this code DataARange and DataBRange will be referring to ranges on whatever the active sheet is.
    
    Set DataARange = Range(Range("DataA").Value)
    Set DataBRange = Range(Range("DataB").Value)
    The easiest way to fix the problem is to add worksheet references and remove all the unneeded Selects.

  12. #12
    Registered User
    Join Date
    11-19-2012
    Location
    Hollywood, FL
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run-time error '1004': Select method of Range class failed.

    Fixed! As you said, I think it was all the unnecessary selects...

    Instead of:
    Range.Select
    Selection.Paste

    I did:
    Range.Paste

    So cleaning up the code helped a lot. Thanks a lot!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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