---
url: /excel-addin/vba.md
description: >-
  Refresh MODLR Excel Add-in functions from a VBA macro, and run follow-up code
  once the refresh has finished.
---

# Automating with VBA

The [Refresh Worksheet and Refresh Workbook](/excel-addin/reporting#refresh-worksheet-and-workbook) commands can be called from a VBA macro, so a refresh can be part of a larger automated task.

## Refreshing from a macro

Call either refresh with `Application.Run`:

```vb
Application.Run "RefreshWorkbook"
' or
Application.Run "RefreshWorksheet"
```

The refresh runs in the background, so the line after `Application.Run` may run before the refresh has finished. Do not put code that depends on the refreshed values directly after the call.

## Running code after a refresh

Put code that depends on the refreshed values in a macro named `MODLR_AfterRefresh` in the workbook. The MODLR Excel Add-in checks for this macro and runs it once a refresh has finished.

```vb
Public Sub MODLR_AfterRefresh()
    ' runs after a MODLR refresh
End Sub
```

## Prompts during a refresh

A refresh started from VBA shows the same prompts as one started from the ribbon. Take these into account when a macro is meant to run without anyone at the computer:

* **Confirm Refresh** - shown when the refresh includes a write from a [CubeSend](/excel-addin/cubesend) or [AliasSet](/excel-addin/aliasset). Turning writes off with a switch cell, as described in [Practice safe sends](/excel-addin/advanced-techniques#practice-safe-sends), means the prompt is not shown.
* **Error prompts** - shown when a cell fails, for example a [VariableGet](/excel-addin/variableget) with a variable name that does not match. These can appear even when the refresh includes no writes.

![The Confirm Refresh prompt shown before a refresh that includes CubeSend and AliasSet functions](/excel-addin/excel-addin-confirm-refresh.png)
