1

I have a macro that copy/pastes a selection from one worksheet (Sheet1), to another worksheet (Notes). It works well. Now I want to first check if that worksheet exists. If it does not exist, I want to create it, then continue with the copy/pasting the selection.

When the "Notes" worksheet exists, the copy/paste works fine. If the worksheet does not exist, it creates it, but the paste operation doesn't work. I don't get any errors. I have to rerun the macro and then the paste works (since the worksheet has already been created). Any ideas on what I missed?

Sub Copy2sheet()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Dim mySheetName As String, mySheetNameTest As String
mySheetName = "Notes"

'create worksheet at end of workbook if it does not exist
On Error Resume Next
mySheetNameTest = Worksheets(mySheetName).Name
If Err.Number = 0 Then
    GoTo CopyPasteSelection
Else
    Err.Clear
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = mySheetName
End If

'copy/paste selection to Notes worksheet
CopyPasteSelection:
Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Notes")
Selection.Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0).PasteSpecial xlPasteAll
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
shA.t
  • 16,580
  • 5
  • 54
  • 111
Rob
  • 25
  • 5
  • possible duplicate of [How to check whether certain sheets exist or not in Excel-VBA?](http://stackoverflow.com/questions/6838437/how-to-check-whether-certain-sheets-exist-or-not-in-excel-vba) – WombatPM Apr 29 '15 at 18:11
  • this is not a duplicate of that question. – Rob Apr 29 '15 at 18:44

4 Answers4

1

When you do the Add, the activesheet becomes the new worksheet and your previous Selection is lost...............you must "remember" it before the Add:

Sub Copy2sheet()
    Application.ScreenUpdating = False
    Dim copySheet As Worksheet
    Dim pasteSheet As Worksheet
    Dim mySheetName As String, mySheetNameTest As String
    mySheetName = "Notes"
    Dim RtoCopy As Range
    Set RtoCopy = Selection

    'create worksheet at end of workbook if it does not exist
    On Error Resume Next
        mySheetNameTest = Worksheets(mySheetName).Name
    If Err.Number = 0 Then
        GoTo CopyPasteSelection
    Else
        Err.Clear
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = mySheetName
    End If

    'copy/paste selection to Notes worksheet
CopyPasteSelection:
    Set copySheet = Worksheets("Sheet1")
    Set pasteSheet = Worksheets("Notes")
    RtoCopy.Copy
    pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0).PasteSpecial xlPasteAll
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

Pay attention to the three lines referencing RtoCopy .

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • Excellent! That was it. I was suspecting that might be the case; but then I thought the selection was still valid at the Selection.copy line since I could see the cells were still selected after the macro completed. Thank you! – Rob Apr 29 '15 at 18:41
  • @Rob ............when I do this type of task, I try to create two **Range** objects...........one for the Copy source and one for the Copy destination. – Gary's Student Apr 29 '15 at 18:50
0

You have On Error Resume Next in your code. First time through it goes on its merry way. The second time through the Error check triggers the creation of the new tab.

On Error Resume Next is bad. Don't use it.

See this question for more information on solving your problem How to check whether certain sheets exist or not in Excel-VBA?

Community
  • 1
  • 1
WombatPM
  • 2,561
  • 2
  • 22
  • 22
0

You should first activate and select the sheet and range to be copied. This works.

CopyPasteSelection:
Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Notes")

Worksheets("Sheet1").Activate 'Activete "Sheet1"
Worksheets("Sheet1").Range("A1").Select 'Select the range to be copied
'Then copy selection
Selection.Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0).PasteSpecial xlPasteAll
Application.CutCopyMode = False
Application.ScreenUpdating = True
kitap mitap
  • 668
  • 8
  • 20
0

I suggest using Function for more re-usability:

  1. A dirty and fast way:

Function isWorksheetValid(wsName As String)
    ON Error Goto ErrHndl
    Dim ws as Worksheet
    Set ws = Sheets(wsName)
    isWorksheetValid = True
    Exit Function
ErrHndl:
    isWorksheetValid = False 
End Function
  1. A correct but a bit slower way:

Function isWorksheetValid(wsName As String)
    ON Error Goto ErrHndl
    Dim ws as Worksheet
    For Each ws in Sheets
        If (UCASE(ws.Name) = UCASE(wsName)) Then
            isWorksheetValid = True
            Exit Function
        End If
    Next
ErrHndl:
    isWorksheetValid = False 
End Function

Now you need just use it like this:

If (isWorksheetValid(mySheetName) Then
    ' Add your code here
End If
shA.t
  • 16,580
  • 5
  • 54
  • 111