Excel - Repeat Macro for Each Cell in A: Episode 1481

MrExcel.com
MrExcel.com
48.7 هزار بار بازدید - 13 سال پیش - Microsoft Excel Tutorial: Making a
Microsoft Excel Tutorial: Making a Macro Work for Every Value in Column A | Excel VBA.

Welcome back to the MrExcel netcast! In today's episode, we will be discussing a question sent in by JK about how to make a macro work for every single value in column A. JK has a macro that currently runs on the current cell, but he wants to know how to make it work for all the cells in column A. So, let's dive in and find out how to do that!

First, I want to mention that this episode is sponsored by MrExcel.com. Now, JK didn't tell me what his macro does, but I wrote a little macro to demonstrate the issue. As you can see, it puts the letter E in the seventh column when I press Ctrl A. Now, this may seem like a silly example, but the point is, how do we make this macro work for every value in column A?

To solve this problem, we will be using a line of code that I like to call my "favorite line of code". It's the one I use over and over again in my macros. It goes like this: FinalRow=Cells(Rows.Count, 1).End(x1Up).Row. What this does is it goes to the last row in the spreadsheet and figures out what row we're at. In this case, it tells us that we have 15 rows of data.

Next, we will use a loop to go through each row and apply the macro. So, we will use the code "for i = 2 to FinalRow" and then indent the macro we want to apply. Finally, we will use the code "Next i" to end the loop. This way, the macro will be applied to every single value in column A. No more pressing Ctrl A and down arrow repeatedly!

Now, JK mentioned that we may need to use Count or Count A to achieve this, but I found this to be a handy and efficient way to do it. We already have variables, so why not use them? By figuring out the last row and using a loop, we can quickly apply the macro to all the cells in column A. So, thank you JK for sending in this question and I hope this solution helps you and others facing a similar issue.

Thank you for tuning in to this episode of the MrExcel netcast. I hope you found it helpful and informative. Don't forget to subscribe to our channel and hit the notification bell to stay updated on all our latest episodes. And if you have any questions or suggestions for future episodes, please leave them in the comments below. Until next time, this is Bill Jelen signing off. See you in the next netcast!

Buy Bill Jelen's latest Excel book: https://www.mrexcel.com/products/latest/

You can help my channel by clicking Like or commenting below: https://www.mrexcel.com/like-mrexcel-...


Table of Contents:
(00:00) Introduction and Sponsorship
(00:10) Question from JK about repeating macro for each cell in column A
(00:21) Using VBA to find last row of data
(01:12) Using "For" loop to apply macro to each cell in column A
(02:06) Clicking Like really helps the algorithm


Learn a little about Customizing your VBA. We have a Macro that runs on the current cell; JK wants the macro to run for each cell in Column A. Today, in Episode #1481, Bill shows us how calculate the number of cells we need to check and then loop the macro.

...This episode is the video podcast companion to the book, "Learn Excel 2007 through Excel 2010 from MrExcel". Download a new two minute video every workday to learn one of the 512 Excel Mysteries Solved! and 35% More Tips than the previous edition of Bill's book!

"The Learn Excel from MrExcel Podcast Series"

MrExcel.com — Your One Stop for Excel Tips and Solutions. Visit us today!

Join the MrExcel Message Board discussion about this video at  https://www.mrexcel.com/board/threads...
13 سال پیش در تاریخ 1390/09/09 منتشر شده است.
48,707 بـار بازدید شده
... بیشتر