Need more efficient VBA Code for copy paste

EricB9

New Member
Joined
Apr 10, 2014
Messages
6
I have a bunch of expense categories and am using the macro to check if the categories are in a certain range. If they are not in the range, I need to insert a row copying the formulas from the above row and add the category. The code I am using is:

RepeatCheck = IsNumeric(Application.Match(TempCategory, Range("Management:GenAdmin"), 0))


If RepeatCheck = False Then

ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormulas
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormulas
Application.CutCopyMode = False


ActiveCell.Offset(0, 1).Value = Isblank

ActiveCell.Offset(0, 2).Value = TempCategory

Else

End If

The code works but it is slow. I have a lot of expense categories to cycle through so getting this as streamlined as possible is important.

Thanks for your help
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Do you have the following two lines of code at the beginning of your code?

Code:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

And the following two lines of code before the end of your code?

Code:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
 
Upvote 0
Yes, I have both in and it is still much slower than I would like. I'm not sure if it is the copy paste portion slowing it down or the match.
 
Upvote 0
how many rows below do you have, do they have formulas, maybe 90% of those could be turned to static numbers so no formulas update, evry push a row down will henge every reference
 
Upvote 0
The rows all have formulas that need to be copied into the new row when inserted. Once the code finishes there will be about 10-20 rows inserted.
 
Upvote 0
You can try copying only the range that has data, not the entire row. If the ActiveCell is the left-most cell, try this:

Range(ActiveCell, ActiveCell.Offset(0, 5000).End(xlToLeft)).Copy 'substitute 5000 with the column number of the right-most column
 
Upvote 0
I've always used this since I found it

'By-passes the Clipboard
Sheet1.Range("A1:A200").Copy Destination:=Sheet2.Range("B1")
'Or, if only values are needed:
Sheet2.Range("B1:B200").Value= Sheet1.Range("A1:A200").Value
'Or, if only formulae are needed:
Sheet2.Range("B1:B200").Formula = Sheet1.Range("A1:A200").Formula
 
Upvote 0
Thanks. I think I've determined the part that is slowing the code down is searching to see if the category is already there or not. I'm currently using the following code for this:

Code:
Dim TempCategory As String
Dim RepeatCheck As Boolean


[COLOR=#333333]RepeatCheck = IsNumeric(Application.Match(TempCategory, Range("Management:GenAdmin"), 0))[/COLOR]

Is there a better way to do this than what I'm currently using?
 
Upvote 0
To eliminate any guessing, you can put a time stamp before each line of code. So, you know exactly how much time is spent on each line.
 
Upvote 0

Forum statistics

Threads
1,216,574
Messages
6,131,492
Members
449,653
Latest member
aurelius33

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