Writing A Macro
Writing A Macro
Writing A Macro
Click the Tools drop-down menu in Excel. Point to "Macro". Click on "Visual
Basic Editor".
2. Step 2
Create a place to store your macro. In the Insert menu on the VBA editor,
click "Module". The module will open in a new window. This module will
house the macro that you are going to write. The module is actually stored in
a folder labeled “VBA Project”.
3. Step 3
Type the word “Sub” in the blank window. Do not write over the text that
reads “End Sub”. After the Sub, type a name for your macro, like
“MyLoopMacro”. A loop is the easiest macro to write. A loop will count rows
automatically until it reaches a specified point to stop. This can be useful for
large amounts of data on a spreadsheet.
4. Step 4
Tell the loop to stop on an empty cell. In the VBA window, type the following
code: Do While Cells(x, 1). Value < > “”X = x + 1Y = y + 1Loop
5. Step 5
Tell your macro that you want the word “Name” to appear darker than other
text throughout your Excel workbook. Add the following code in the VBA
editor:IfMyCell.Value Like “Name” ThenMyCell.Font.Bold = TrueEnd If
6. Step 6
Code your macro to do more with cells in a spread sheet. This is just an
example but one that you can use for a number of things in a macro. You
are going to write code that will combine two cells into an additional cell.
This could be valuable if you have a spreadsheet that can combine first and
last names, pair data together or for any other reason you’d want to carry
data over to another column in the worksheet. Type the following code in the
VBA editor: Sub LoopRange1() X = 3 Do While Cells(x, 3). Value <> “ ”
7. Step 7
Add more code to your macro. So far you have assigned the value “x” to
start at row 3 in your worksheet. You have also created a command to loop
until a blank row is discovered by the macro. Next, type the following three
lines of code to carry the values of the third and fourth columns together with
a space between them in the fifth column: Cells(x, 5). Value = Cells(x, 3).
Value + _ “” + Cells(x, 4). Value x = x + 1
8. Step 8
Create a macro that will delete duplicate data in your worksheet. This can be
wonderful for tracking duplicate accounts in your spreadsheet system. Type
the following code to create and test your own macro. Sub LoopRange3()X
= ActiveCell.RowY = x + 1Do While Cells(x, 4). Value <> “ ”Do While Cells(y,
4). Value <> “ ”If (Cells(x, 4).Value = Cells(y, 4).Value) _And (Cells(x,
6).Value = _Cells(y,6).Value) ThenCells(y, 4).EntireRow.DeleteElsey = y +
1End IfLoopx = x +1y = x +1LoopEnd Sub
10. Step 10
Close the VBA window. Click Save. Your macro has been created. The code
becomes easy to understand when you practice with it. All you need to do is
change the variable names, the loop commands and the cells in the
spreadsheet that you want your macros to perform a task with.
Click on the macro you want to assign from the Macro name box.
Click on the File menu in Excel. Click on "Open Workbook". If the workbook
is hidden in the Personal.xlsb macro workbook, then you need to unhide the
workbook.
15. Step 2
Click "PERSONAL" under the Unhide workbooks tab and then click "OK".
Open the workbook that contains an existing macro that you want to copy a
portion of for your new macro.
18. Step 2
Click "Edit".
21. Step 5
Select the code from the code window that opens with your mouse. Press
Ctrl+C to copy the selection. Click the module in the Procedure box where
you want to place the copied code. Click Ctrl+V to paste the code.
Difficulty: Moderate
Instructions
Things You'll Need:
Computer with Internet access
Microsoft Word or Excel
Paper
1. Step 1
Click "OK." You will notice a small window named "Stop" at the top. Move
this window if necessary, but do not close it, as this stops your macro
recording. Select the text that needs to be changed and make the necessary
changes such as bold, underline, italic, text color or highlighting. When you
are finished, click the solid square in the small "Stop" window and your
macro recording will stop.
3. Step 3
Now you can test your macro by selecting some text and clicking the
shortcut. If the macro does not work as expected, you can try re-recording
by going through all of the steps above again (you can delete the old
shortcut when you open the "Customize" window). Another way to fix a
recorded macro is to go to the "Tools" menu, select "Macro" and "Visual
Basic Editor."
5. Step 5
Difficulty: Moderate
Instructions
Things You'll Need:
Excel Book
Microsoft Excel
1. Step 1
Open the Tools menu and select Macro, then select Record a Macro.
2. Step 2
Click on OK.
4. Step 4
Notice that the word "recording" appears in the lower left of the Excel screen
while recording is in progress.
5. Step 5
Open the Tools menu and choose Stop Recording to complete the macro.
7. Step 7
Activate the macro by pressing the shortcut keys you have selected or by
selecting the macro from a list on