vba

rlee11

New Member
Joined
Dec 16, 2016
Messages
3
I have a code that will copy data from a sheet "Import" then search for a sheet by a name referenced in A6. If it does not exist, it will copy over the header row, if it already exists, it pastes the data to the end of the dataset. I keep getting an error on the final paste command with everything I try. What am i doing wrong?

Code:
Sub Copy1()

    Dim sName As String
    sName = Sheets("Import").Range("A6").Value
    
'Name Columns
    If Sheets(sName).Range("A1") = "" Then
        Sheets("Import").Select
        Range("A5:M5").Select
        Selection.Copy
        Sheets("P3").Select
        Range("A1").Select
        ActiveSheet.Paste
    End If

'Copy Data
    Sheets("Import").Select
    Range("A6:M6").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    
'Find End of Data
    Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
    Dim currentRowValue As String
    
    Sheets(sName).Select
    sourceCol = 1   'column A has a value of 1
    rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row

    'for every row, find the first blank cell and select it
    For currentRow = 1 To rowCount
        currentRowValue = Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            Cells(currentRow, sourceCol).Select
        End If
    Next
    
'Paste Data
    ActiveSheet.PasteSpecial x1PasteAll

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Oops, on my phone didn't mean to post that.:)
 
Last edited:
Upvote 0
What is the error you are getting?

EDIT
Actually, I think the error is due to a typo.
ActiveSheet.PasteSpecial x1PasteAll --> ActiveSheet.PasteSpecial xlPasteAll
 
Last edited:
Upvote 0
Thanks for catching that typo!
What is the error you are getting?

The error I get is "PasteSpecial method of Worksheet class failed"

I changed it back to ActiveSheet.Paste and now it works but it pastes the data over the header column instead of finding the first empty row.
 
Upvote 0
As you have the code now, the ActiveSheet.PasteSpecial xlPasteAll line always gets executed. You'd need to integrate it with an If function to check whether the sheet exists or not.
 
Upvote 0
Moving it into an If statement worked after I simplified the 'Find end of Data' section

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,649
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top