Looking for better way to paste

hitbid

Board Regular
Joined
Jan 21, 2016
Messages
114
I am copying data from an outside excel source and then pasting that to excel.

I am fine clearing out the contents and the macro works as is, but the Paste function seems inadequate. I am trying to not use .select, and tried to incorporate CELLS vs Range, but nothing worked. The only thing I could use was the default from the macro recorder. Any idea how to enhance the Range("A2").select paste lines?

Code:
Dim lastrow As IntegerDim lastcol As Integer


Sub clear()


lastrow = Cells(Rows.Count, "A").End(xlUp).Row
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column


Range(Cells(2, 1), Cells(lastrow, lastcol)).ClearContents
End Sub


Sub Paste()


'insert your paste statement here
'Does not seem like the ideal paste statement.
'sub is separated from previous sub because the contents that were copied were erased from memory after the 'above ran. 
Range("A2").Select
ActiveSheet.Paste


Dim i As Integer


For i = 2 To lastrow
    Cells(i, lastcol).Formula = "=B" & i & "&"" - ""&A" & i
Next i


End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Perhaps this...

Code:
Option Explicit

Sub Paste()
'
    Dim lastrow As Integer, lastcol As Integer, i As Integer
    Dim strPaste  As Variant
    Dim DataObj As MSForms.DataObject
    Set DataObj = New MSForms.DataObject
    
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
    range(Cells(2, 1), Cells(lastrow, lastcol)).ClearContents
    
    DataObj.GetFromClipboard
    strPaste = DataObj.GetText(1)
        If strPaste = False Then Exit Sub
        If strPaste = "" Then Exit Sub
    [a2] = strPaste
    Set DataObj = Nothing
    
        For i = 2 To lastrow
            Cells(i, lastcol).Formula = "=B" & i & "&"" - ""&A" & i
        Next i
End Sub
 
Upvote 0
Thanks, but I don't get the option in the intellisense for MSForms. The user defined type is not defined.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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