VBA (Visual Basic for Applications) is widely used for automating MS-Office products. Most of the time, when you are working with loads of data and want to automate your tasks, VBA comes in very handy.
VBA(用于应用程序的Visual Basic)被广泛用于自动化MS-Office产品。 大多数情况下,当您处理大量数据并希望自动化您的任务时,VBA会非常方便。
Last week, I had to work on dozens of workbooks and had to do some process of data cleaning over them. If I would have done it manually by going on each workbook and cleaning it - the same process for dozens of times, it would have sucked all my energy and would have taken hours to get all done and dusted.
上周,我不得不处理数十本工作簿,并且必须对它们进行一些数据清理过程。 如果我要通过每本工作簿并清洁它来手动完成该过程(同一过程数十次),那将消耗我的所有精力,并且将花费数小时来完成所有工作并除尘。
To avoid this tedious process of cleaning, I decided to go for a shortcut. And, that was writing a VBA script! And, that made the whole process super fast. I was done with my task in a few minutes. Not only it will save you time, but also bring a serious cut on efforts. And, all it takes will be some good knowledge of VBA script and you will rock at work.
为了避免繁琐的清洁过程,我决定寻求捷径。 而且,那是在编写VBA脚本! 而且,这使整个过程变得非常快。 几分钟后,我的任务就完成了。 它不仅可以节省您的时间,而且可以大大减少工作量。 并且,所需要的只是VBA脚本的一些知识,您会在工作中动摇。
As per Microsoft’s definition,
按照Microsoft的定义,
a macro is an action or a set of actions that you can run as many times as you want
宏是一个动作或一组动作,您可以根据需要运行多次
Let’s say, you get a weekly report and it needs some fixes on formatting issues. Every time making those formatting issues correct manually will consume time and you will get bored of doing the same task again and again. Hence, creating a macro for the task will come in very handy.
假设您有一份每周报告,并且需要对格式问题进行一些修复。 每次手动解决这些格式问题都会浪费时间,并且您会一次又一次地厌倦执行相同的任务。 因此,为任务创建宏将非常方便。
Every time you get the weekly report, run the macro, and you are done!
每次获取每周报告时,运行宏,您就完成了!
Macros are just small programs that you can run and those macros will perform certain actions. Let’s say, your task is to go to column A of your Excel dataset and lookup the cell values from other sheets. The lookup results are stored in Column B.
宏只是可以运行的小程序,这些宏将执行某些操作。 假设您的任务是转到Excel数据集的A列并从其他工作表中查找单元格值。 查找结果存储在B列中。
So, you can write a VBA code (that will be your macro) and run it. Hence, the Vlookup will be performed automatically. Not a thing to do!
因此,您可以编写一个VBA代码(将是您的宏)并运行它。 因此,Vlookup将自动执行。 没事做!
This is just a simple example that I am talking about. There can be complicated tasks that may require formatting, cleaning, and even filtering. So, going with the manual approach is too hefty. Hence, VBA comes to the pitch.
这只是我正在谈论的一个简单示例。 可能存在复杂的任务,可能需要格式化,清理甚至过滤。 因此,采用手动方法过于繁琐。 因此,VBA进入了市场。
Okay, I have talked about VBA a lot now. Let’s play with the coding exercise now. I am going with the Vlookup exercise only for now.
好的,我现在已经谈论了很多VBA。 现在开始进行编码练习。 我现在仅进行Vlookup练习。
Excel provides a feature that will help you in creating your macro. No need to write the code. It will be handled by Excel itself.
Excel提供了一项功能,可以帮助您创建宏。 无需编写代码。 它将由Excel本身处理。
It is an amazing feature. So, if you have to look up a column’s values from another column and you do it regularly, a macro will be very useful for you.
这是一个了不起的功能。 因此,如果您必须从另一列中查找一列的值并定期执行,那么宏将对您非常有用。
But, how to record a macro?
但是,如何记录宏?
Very Simple! Go to Developer > Record Macro. And, all your key actions will be recorded and converted into VBA code. Super Amazing, right?
很简单! 转到开发人员>记录宏。 并且,您所有的关键操作都将被记录并转换为VBA代码。 超级神奇吧?
(Left) Sheet1: Introduce lookup column here (Right) Sheet2: Looking up from this place (左)Sheet1:在此处介绍查找列(右)Sheet2:从此位置查找These are the two sheets (sample data) — at one I will be bringing lookup results and will perform the lookup from the right sheet.
这是两张纸(样本数据),我将带来一张查找结果,并从右一张纸开始执行查找。
So, after you start recording, you have to do the same you would do manually. Go to column B, set the column name at B1 and then, apply vlookup formula to the B2 cell, autofill the column, converting the column to values, and replace #N/A by blanks.
因此,在开始录制后,您必须执行与手动相同的操作。 转到列B,将列名设置为B1,然后将vlookup公式应用于B2单元格,自动填充列,将列转换为值,并用空格替换#N / A。
Go to stop recording and now, you can see the macro code. In my case, it looked like.
转到停止记录,现在您可以看到宏代码。 就我而言,它看起来像。
It is the code that Excel generated after I started recording. Hence, you can use this macro or its code to the same task on any excel sheet.
这是我开始记录后Excel生成的代码。 因此,您可以使用此宏或其代码对任何excel工作表执行相同的任务。
The other way i.e the hard way is to write code by yourself from the scratch. But, the Record Macro feature might not be as dynamic as you want it to be. But, at least it gives you a blueprint and you are good to go with changes and the baseline.
另一种方法,即困难的方法是从头开始自己编写代码。 但是,“录制宏”功能可能不会像您期望的那样动态。 但是,至少它为您提供了一个蓝图,并且很好地应对更改和基准。
So, next time you are looking to make things fast. Or, if you are bored with doing the same stuff on excel files again and again, just remember Microsoft has provided some beautiful features to us for crazy automation. That is Macros.
因此,下一次您希望快速进行操作。 或者,如果您无聊一次又一次地在excel文件上做同样的事情,请记住Microsoft为我们疯狂的自动化提供了一些漂亮的功能。 那就是宏。
Peace!
和平!
翻译自: https://medium.com/swlh/rock-the-excel-sheets-with-vba-c5e84e01261d