Clipboard clears out so must create a separate routine

hitbid

Board Regular
Joined
Jan 21, 2016
Messages
114
I copy data from an outside source and paste into excel.

To make things easier, I created a macro that starts by clearing out the current contents of the sheet.

Then after that is done, I can copy the outside data, and run the FormInsert routine.

I had to create the separate FormInsert routine, because if I put them all into one, nothing gets pasted, which I suppose is because that is how excel and clipboard work.

Do you see any way to combine the two into one?

Code:
Dim lastrow As Integer
Dim lastcol As Integer


Sub ClearSheet()


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


'This function clears out the whole sheet.
'Add a -1 to the last col to keep the last column.
'may not need that though, if you use the below FormInsert function


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


Sub FormInsert()


'insert your paste statement here
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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
Code:
Sub ClearSheet()
Dim i As Integer

lastrow = Cells(Rows.Count, "A").End(xlUp).row
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
'This function clears out the whole sheet.
'Add a -1 to the last col to keep the last column.
'may not need that though, if you use the below FormInsert function
Range(Cells(2, 1), Cells(lastrow, lastcol)).ClearContents
Range("A2").PasteSpecial
For i = 2 To lastrow
    Cells(i, lastcol).Formula = "=B" & i & "&"" - ""&A" & i
Next i

End Sub
 
Upvote 0
Interesting, that worked.

So then, if you copy and paste from an outside source, it is ok to combine the two routines into one.

If you copy and paste inside excel though, no dice...?
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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