VBA copy and paste code help

michelangelo

New Member
Joined
Nov 23, 2016
Messages
6
I am looking for some help to make the below code a bit more efficient/elegant. Any help much appreciated. (note this is a sample, I use the same code 5 or 6 times hence I'm looking to shorten!).

If it helps I have the active sheet stored as sht from earlier in the code. LastRow, Next_Free_Row and today_green_col are integers created earlier in the code.

ActiveSheet.Cells(LastRow, today_green_col).Select
ActiveSheet.Cells(LastRow, today_green_col).Copy
ActiveSheet.Cells(Next_Free_Row, today_green_col).Select
ActiveSheet.Cells(Next_Free_Row, today_green_col).Activate
ActiveSheet.Paste


Any help would be much appreciated!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi michelangelo,

Since you are looking to shorten it, doesn't this work?
I don't think you need to select first.

Rich (BB code):
ActiveSheet.Cells(LastRow, today_green_col).Copy
ActiveSheet.Cells(Next_Free_Row, today_green_col).Paste



I didn't test it, but let me know :)
 
Upvote 0
Hi michelangelo,

Since you are looking to shorten it, doesn't this work?
I don't think you need to select first.

Rich (BB code):
ActiveSheet.Cells(LastRow, today_green_col).Copy
ActiveSheet.Cells(Next_Free_Row, today_green_col).Paste

I didn't test it, but let me know :)

Unfortunately not :(. Thanks for the suggestion though
 
Upvote 0
Hi Michelangelo, Can you show us the rest of the code so we can understand better.
 
Upvote 0
Does this do it?

Code:
sht.Cells(Next_Free_Row, today_green_col).Value = sht.Cells(LastRow, today_green_col).Value
 
Upvote 0
Hi decadence,

there are about 400 lines of code around this, but i have put the pertiment ones in;

Dim LastRow As Integer
Dim Next_Free_Row As Integer
Dim today_green_col As Integer

Set sht = ThisWorkbook.Worksheets("Records")

'find last row
sht.UsedRange 'Refresh UsedRange
LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).row
Next_Free_Row = LastRow + 1

sht.Select
ActiveSheet.Cells(LastRow, today_green_col).Select
ActiveSheet.Cells(LastRow, today_green_col).copy
ActiveSheet.Cells(Next_Free_Row, today_green_col).Select
ActiveSheet.Cells(Next_Free_Row, today_green_col).Activate
ActiveSheet.Paste


I hope this is emough. Also I realise I didnt state in the original post, what I really want to copy is the formulas and formatting from these cells.

thanks
 
Upvote 0
Apologies i should have said as below, I am trying to copy the formulas and format, so this doesn't work. Thanks for the help anyway
 
Upvote 0
Maybe:

sht.Cells(LastRow, today_green_col).Copy: sht.Cells(Next_Free_Row, today_green_col).PasteSpecial xlPasteFormulasAndNumberFormats
 
Upvote 0

Forum statistics

Threads
1,215,963
Messages
6,127,951
Members
449,412
Latest member
montand

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