+ Reply to Thread
Results 1 to 6 of 6

VBA copy values to newly created sheet problem

Hybrid View

dusoo VBA copy values to newly... 07-01-2013, 07:19 PM
xladept Re: VBA copy values to newly... 07-01-2013, 07:46 PM
dusoo Re: VBA copy values to newly... 07-01-2013, 07:49 PM
xladept Re: VBA copy values to newly... 07-01-2013, 08:14 PM
dusoo Re: VBA copy values to newly... 07-01-2013, 10:06 PM
xladept Re: VBA copy values to newly... 07-01-2013, 10:32 PM
  1. #1
    Registered User
    Join Date
    06-02-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    19

    VBA copy values to newly created sheet problem

    Hi guys,

    I'm trying to copy content of one sheet to newly created sheet. The copy&paste code I created using "record macro" option.
    Code below fails on the line "Sheets(wsName).Select" with error 400. (select method of class Range failed)
    Could you please write what's wrong with that?

    Thanks

        wsName = Sheets("Sheet1").Range("I7")
       
        Dim WS As Worksheet
        If WorksheetExists(wsName) Then
           Sheets(wsName).Delete
           Set WS = Sheets.Add(After:=Sheets(Worksheets.Count))
           WS.Name = wsName
        Else
           Set WS = Sheets.Add(After:=Sheets(Worksheets.Count))
           WS.Name = wsName
        End If
        
        Sheets("Sheet1").Range("A1:J10").Select
        Range("J10").Activate
        Selection.Copy
    
    ' HAVE PROBLEM HERE - ERROR 400
        Sheets(wsName).Select
    
        Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        ActiveSheet.Paste

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA copy values to newly created sheet problem

    Try:
    Sheets("Sheet1").Range("A1:J10").Select
        Range("J10").Activate
        Selection.Copy
    wsName = Sheets("Sheet1").Range("I7")
       
        Dim WS As Worksheet
        If WorksheetExists(wsName) Then
           Sheets(wsName).Delete
           Set WS = Sheets.Add(After:=Sheets(Worksheets.Count))
           WS.Name = wsName
        Else
           Set WS = Sheets.Add(After:=Sheets(Worksheets.Count))
           WS.Name = wsName
        End If
        
            WS.Select
    
        Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        ActiveSheet.Paste
    Last edited by xladept; 07-01-2013 at 07:49 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    06-02-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: VBA copy values to newly created sheet problem

    I get same error.
    Is there another way to copy & paste?
    I have tried this code below, but xlPasteColumnWidths does nothing, replacing it by xlPasteValues helps, but no format is copied over which is bad.
    Any ideas?

    Set r1 = Sheets("Sheet1").Range("A1:J10")
    Set r2 = Sheets(wsName).Range("A1:J10")
    
    r1.Copy
    r2.Offset(0, 0).PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA copy values to newly created sheet problem

    Try this:

    Sub DUSOO(): Dim wsName As String, WS As Worksheet
    Sheets("Sheet1").Range("A1:J10").Copy
        wsName = Sheets("Sheet1").Range("I7")
           
       
        For Each WS In Worksheets
        If WS.Name = wsName Then
           Sheets(wsName).Delete: End If: Next
           
           Set WS = Sheets.Add(After:=Sheets(Worksheets.Count))
           WS.Name = wsName
           
            WS.Range("A1").PasteSpecial xlPasteColumnWidths
            WS.Range("A1").PasteSpecial xlPasteAll
            
    End Sub

  5. #5
    Registered User
    Join Date
    06-02-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: VBA copy values to newly created sheet problem

    Thanks, it worked. So I don't have to Select newly created sheet...

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA copy values to newly created sheet problem

    You're welcome! No, the newly created sheet is always the active sheet.

    (Don't forget to mark this 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