+ Reply to Thread
Results 1 to 7 of 7

Range Issue

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2009
    Location
    Scotland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Range Issue

    Hi everyone,

    I was wondering if someone could help me.

    I have 3 workbooks, I have a sheet that will have a button on it that will run the code, when the button is pushed, data from another workbook will then copy to a 3rd workbook.

    Control.xls - Has the button, which will run the vba code.
    Source.xls - Has the data to be copied.
    Dest.xls - Were the data will be copied too.

    This is the code I have so far:
    strFirstFile = "C:\Users\Jay Webster\Desktop\Source.xls"
    strSecondFile = "C:\Users\Jay Webster\Desktop\Dest.xls"
    
    Workbooks.Open strFirstFile
    LastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
    LastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
    
    If LastColumn > 26 Then
        LastColumn = Chr(Int((LastColumn - 1) / 26) + 64) & Chr(((LastColumn - 1) Mod 26) + 65)
    Else
        LastColumn = Chr(LastColumn + 64)
    End If
    
    Range("A2", LastColumn & LastRow).Select      THIS IS THE PART THAT FAILS
    Selection.Copy
    
    Workbooks.Open strSecondFile
    Sheets("Sheet1").Select
    ActiveSheet.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    I get the error "Select method of Range class failed". Can someone tell me where I am going wrong?

    Thanks

    Any questions just ask.
    Last edited by JayWeb; 01-24-2010 at 07:15 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,462

    Re: Range Issue

    Hard to tell without knowing the values of LastRow and LastColumn.

    Try using the Cells method rather than converting column to a character.

    Range("A2", Cells(LastRow, LastColumn)).Copy
    Also no need to select.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    06-25-2009
    Location
    Scotland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Range Issue

    Thanks Andy, worked perfectly.

    I now have another problem though, when the data is being copied, it is copying cells from Control.xls and not Source.xls.

    I thought something like this would work but its doesnt, it still copies from Control.xls
    Workbooks("Source.xls").Activate

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,462

    Re: Range Issue

    Use objects rather than rely on the active ones.

        Dim wbkSource As Workbook
        Dim wbkDest As Workbook
        Dim shtSource As Worksheet
        Dim shtDest As Worksheet
        
        strFirstFile = "C:\Users\Jay Webster\Desktop\Source.xls"
        strSecondFile = "C:\Users\Jay Webster\Desktop\Dest.xls"
        
        Set wbkSource = Workbooks.Open(strFirstFile)
        Set shtSource = wbkSource.Worksheets("Sheet1")
        
        Set wbkDest = Workbooks.Open(strSecondFile)
        Set shtDest = wbkSource.Worksheets("Sheet1")
        
        With shtSource
            LastColumn = .UsedRange.Column - 1 + .UsedRange.Columns.Count
            LastRow = .UsedRange.Row - 1 + .UsedRange.Rows.Count
            .Range("A2", .Cells(LastRow, LastColumn)).Copy
        End With
        
        shtDest.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

  5. #5
    Registered User
    Join Date
    06-25-2009
    Location
    Scotland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Range Issue

    Thanks for taking the time to write that for me, although if I use your code I get the error "Named Argument Not Found"

    shtDest.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Any Ideas?

  6. #6
    Registered User
    Join Date
    06-25-2009
    Location
    Scotland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Range Issue

    bump, anyone?

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,462

    Re: Range Issue

    I couldn't check you actual code as I did not have example data files.
    I therefore assumed the code you posted was at least compiling if not running as expected.

    The problem is the Pastespecial arguments you posted are for a Range object. Where as they code suggested you where using a worksheet object.

    You need to make this change

        shtDest.Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

+ 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