Search code examples
excelcopy-pasteworksheet-functionvba

Copy paste to new Worksheet in Excel


So i got this code wich copies cells from C4 to C10 and pastes them. The problem is that it pastes to the same worksheet and i need to paste it to second worksheet.

I have no idea how to change worksheets in code so i hope someone can help me out.

Here is the code:

Sub Save_Click()
Range("C4:C10").Copy
Dim curRange As Range
Dim curCol As Integer: curCol = 7
Dim completed As Boolean: completed = False
Do
    curCol = curCol + 1
    Set curRange = Range(Cells(3, curCol), Cells(9, curCol))

If (WorksheetFunction.CountA(curRange) = 0) Then Exit Do End If Loop While (Not completed) curRange.PasteSpecial End Sub

Solution

  • For the worksheet "Iskalnik", as you have done, use:

    Sheets("Iskalnik").Range("C4:C10").Copy
    

    For the worksheet "Baza", instead of this:

    Set curRange = Sheets("Baza").Range(Cells(3, curCol), Cells(9, curCol))
    

    use this:

    With Worksheets("Baza")
        Set curRange = .Range(.Cells(3, curCol), .Cells(9, curCol))
    End With
    

    The issue with curRange is that the Range and Cells keywords all need to reference the sheet. The With ... End With is just a convenient way to do that.