Excel Macro Examples

Wednesday, October 19th 2022. | Sample Excel

Excel Macro Examples – Bottom Line: Learn 3 tips for writing and creating formulas in your VBA macros with this article and video.

Writing formulas is the most time-consuming part of your weekly or monthly Excel work. If you want to automate the process with a macro, you can write a VBA formula and insert it into the cells for you.

Excel Macro Examples

Excel Macro Examples

Writing formulas in VPA can be a little tricky at first, so here are 3 tips to help you save time and make the process easier.

How To Create A Macro Button To Save & Close An Excel Workbook

The Formula property is a member of VBA’s Range object. It can be used to set/create a formula for a single cell or a range of cells.

The formula value we set with the formula property has certain requirements:

When your formulas are more complex or contain special characters, writing them in VBA can be more challenging. Fortunately, we can use the macro recorder to generate the code.

If your formula contains quotation marks or ampersand symbols, the macro recorder will calculate this. It creates all subtitles and wraps everything properly in quotes. Here is an example.

How To Create Vba Macro Buttons For Filters In Excel

If you use the macro recorder for formulas, you’ll see that it generates code with the attribute FormulaR1C1.

The R1C1 style code allows us to create both relative (A1), absolute ($A$1) and mixed ($A1, A$1) within our macro code.

For related notes, we specify the number of rows and columns we want to offset from the cell containing the formula. The number of rows and columns is shown in square brackets.

Excel Macro Examples

The following will create a reference to the cell 3 rows above and 2 rows to the right of the cell containing the formula.

Emotet Is Not Dead (yet)

We do not use square brackets for absolute references. The following will create a direct reference to cell $A$2, row 2 column 1

With compound references we add square brackets for row or column references and no brackets for other references. The following formula in cell B2 will create this reference to A$2, where the row is absolute and the column is relative.

When creating compound references, the corresponding row or column number depends on which cell the formula is in.

The FormulaR1C1 property reads the R1C1 code and creates the correct references in the cells. If you use the Regular Formula property with R1C1 code, VBA will try to insert those characters into the formula, causing a formula error.

Holy Macro! It’s 2,500 Excel Vba Examples: Every Snippet Of Excel Vba Code You’ll Ever Need

Therefore, use the Formula property when your code contains cell references ($A$1), when the FormulaR1C1 property applies to multiple cells, or when relative references are required depending on where the formula is entered.

If your spreadsheet changes based on conditions outside of your control, such as new columns or rows of data imported from a data source, relative references and R1C1 style notation are best.

Sign in again. The login page will open in a new tab. After logging in, you can close it and return to this page. In this post, I’ll dive into Excel 4.0 macros (also known as XLM macros – not XML) for offensive purposes. If, like me, you grew up with Windows 95 or later, you may not have heard of this technology that was introduced in 1992. All malicious macros for MS Office are based on Visual Basic for Applications (VBA). ) However, XLM macros are a hidden gem for red groups and can be a great alternative to VBA macros for malicious purposes: XLM is difficult to analyze and most antivirus solutions seem to have trouble detecting XLM maltox. Although the technology is now 26 years old, Excel 4.0 macros are still supported in the latest Microsoft Office versions (including Office 2016 at the time of writing).

Excel Macro Examples

This blog post is a teaser for Peter Seelan and Stan Hecht’s presentation at DerbyCon 2018 (Sunday, 1pm) titled “The MS Office Magic Show”. After the presentation, video and slide content will be posted online.

Video: Work With Macros

1992 Excel 4.0 spreadsheet software for Windows 3.0 and 3.1 is released. For automation, you can use XLM macros in a version of Excel called macro worksheets. The XLM concept is very different from the Visual Basic for Applications (VBA) macros introduced a year later in Excel 5.0. To give you an idea of ​​how old these things are: our lost friend Clippy wasn’t introduced before 1996.

Let’s get our feet wet. In recent versions of Excel you can perform the following steps to create an XLM macro:

A window will pop up allowing you to select different objects to insert. Select “MS Excel 4.0 Macro” and click “OK”.

A new worksheet called “Macro1” will be created. This is a special worksheet where you can insert XLM macros (called macro sheets). Click on any cell and enter the formulas “=EXEC(“calc.exe”)”, “=ALERT(“Hello world”)” and “=HALT()” in this cell and the cells below.

Office Excel Macro

To test your macro, right-click the first cell containing your macro code and select “Run.” A new window will pop up. It should contain the name of the first cell containing your macro code (default “R1C1” for row 1, column 1 in my case). Click “Run” to run the macro, then watch the calculator and message box pop up.

If you want your macro to run automatically when the workbook is opened (such as Sub AutoOpen() for VBA macros), rename the first cell of your macro to “Auto_open”.

Alternatively, you can hide your macro worksheet by right-clicking on the sheet name (“Macro1”) and selecting “Hide”. Auto_open XLM macro still runs from hidden worksheets.

Excel Macro Examples

XLM macros is a very rich language that offers many attack capabilities. Much of what you can do with VBA can also be achieved using XLM macros. A major exception to this is COM, the Component Object Model (COM) was introduced only in 1993, a year after Excel 4.0.

Maldoc: Excel 4.0 Macro

In our first example, we already explored the EXEC function used to create processes. You need to access the Win32 API to get more sophisticated attack operations. This can be achieved through REGISTER and CALL functions. The following code shows a proof of concept for shellcode injection using XLM macros. I have successfully tested it with the Cobalt Strike stager payload.

I assume you are familiar with the concept of shellcode injection, so I will only discuss the XLM specification here. An exported function of a DLL, including Win32 API DLLs, can be loaded using the REGISTER function. The syntax for this function is as follows:

The biggest challenge in making Win32 API functions work with XLM macros is matching the types expected by Win32 API functions to Excel 4.0 data types. An overview of available data types can be found here. Each data type is marked with a letter from “A” to “R”. The first character of the argument specifies the type for the return value, and subsequent characters specify the argument type. So the string “JJJCJJ” used in our example when entering in WriteProcessMemory The return value of this function is a long number (“J”) and Excel 4.0 data types long number, long number, pass 5 arguments as a string. . (“C”), extension number, extension number respectively.

There are several caveats that we find in the source of this conceptual code. First, there is the concept of variables. Variables in XLM macros exist conceptually as values ​​in cells. We need a variable (hence, a cell) to store our shellcode, but cells cannot contain different non-printable characters. Fortunately, we can use the = CHAR() function to encode these special characters. Also, cells cannot contain null bytes, so we need to remove these from our payload (this can be achieved using various tools such as msfvenom). Finally, we tackle the issue of maximum cell length in XLM macro sheets. This is why our proof of concept uses a WHILE loop to repeat the WriteProcessMemory call on all cells in the second column until the “END” value is encountered.

Office Macro Attacks

The PoC is messy, but it works and clearly shows the attack capabilities available using XLM and Excel 4.0 macro functions. And I’m sure there are elegant solutions to the problems we faced. For example, our payload can be stored in places other than cells (note: see the GET.WORKBOOK function to retrieve workbook metadata).

All recent Microsoft Office versions, including Office 2016, support Excel 4.0 macros by default. XLM macrosheets can be used in .xls and .xlsm files, although recent versions of Office block the opening of files with the .xlm file extension by default. (Among others, there are some interesting file types discussed in our TerbiCon presentation). up to me

Advanced excel macro examples, microsoft excel macro examples, vba macro excel examples, simple excel macro examples, excel macro examples download, excel macro code examples, macro examples for excel, basic excel macro examples, ms excel macro examples, macro examples in excel, excel macro vba code examples, excel macro if statement examples