Recorded Macro Edits

ad91

New Member
Joined
May 25, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I'm VERY new to VBAs and i just started with recording macros, it was brought to my attention that while recording macros after editing it, that there might be some commands that would be considered redundant and might cause problems down the line. The statement was very vague and i was wondering if anyone had any advice or guidance on how to spot such commands in recording macros and how they would compare if the code were to be written in a more efficient and clear way that wouldn't cause issues down the line.

I would appreciate your feedback.

Thank You.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Welcome to the Board!

If it does what you want it to, the only "issues" that really might cause down the line is it will be a little slower than you want, as it records things like cell selections, which usually aren't necessary.

Most of the time, if you have one line end that ends with:
VBA Code:
.Select
and the next line begins with
VBA Code:
Selection
those two lines can be combined to one line (getting rid of the Select/Selection will speed things up).

Also, it is very literal, and record things like scrolling, which is not necessary in your code and can be removed.

Because it is very literal, you sometimes need to edit the code to make it more dynamic. For example, if you are doing something to a data range that may be different sizes every time, you will want to dynamically determine the size of the range instead of using the recorded range (as that is hard-coded and will never change).

For example, if you wanted to apply something to column A from row 1 down to the last row of data, instead of using a recorded reference like:
VBA Code:
Range("A1:A10")
which is hard-coded as A1:A10, you can dynamically find the last row with data in column A like this:
VBA Code:
Dim lr as Long
lr = Cells(Rows.Count,"A").End(xlUp).Row
and then dynamically refer to your range like:
VBA Code:
Range("A1:A" & lr)
 
Upvote 0
Thank you for your explanation, i guess this is the kind of skill that comes with practice.
 
Upvote 0
No problem.

If your Macro does what you want, and the structure of your data doesn't typically change, you will probably be just fine. It might not be the most efficient Macro, but if it does what you want, and only takes a few seconds to run, that is the most important thing. Sure, you may be to do the some things that might save a little time, but if it already runs fairly quickly, it probably isn't necessary.

If you have specific examples of recorded code that does run fast enough, or isn't dynamic enough, you can simply post your code here and ask someone to help you improve it.
 
Upvote 0

Forum statistics

Threads
1,215,743
Messages
6,126,604
Members
449,321
Latest member
syzer

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