Working with Excel
Syncing and importing with Excel
Write to the workbook
| Command | Description |
|---|---|
ras pack |
Writes directly into the workbook's VBA project without launching Excel. Does not write if the workbook is open in Excel |
ras sync |
Applies changes to the workbook open in Excel (Windows only). In VS Code it runs automatically on every save, and also with "RAS: Sync with Workbook" |
ras watch |
Watches for changes and rebuilds (also runs pack when the workbook is closed) |
ras unpack |
Extracts handwritten VBA in the workbook into vba/ |
ras build |
Outputs .bas / .cls / .rasmap to build/ (for importing into the VBE by hand) |
- If the workbook has a module of the same name that was not generated by RAS, or if a handwritten event procedure (such as
Workbook_Open) has the same name as generated code, it reports an error without writing (the former can be replaced with--overwrite). - The workbook from before the replacement is saved in
build/backup/. ras syncrewrites the VBE code modules through COM. Sync in the Excel → local direction (ras sync --pull) is not implemented (Implementation status). Sync settings in VS Code and the status bar display are described in the VS Code extension.
Importing an existing workbook
ras sync (ras watch, and VS Code sync) and ras pack read the workbook's VBA before writing, convert handwritten code that RAS does not yet manage to RAS, and import it into the project (spec §14.7.1).
| What is in the workbook | Where it is imported |
|---|---|
Standard module Module1 |
src/Module1.ras |
Code of the sheet Sheet1 / ThisWorkbook |
objects/Sheet1.ras / objects/ThisWorkbook.ras (Worksheet_Change and the like become @on("Worksheet.Change") handlers, and Me and unqualified Range(...) become this.range(...). Public procedures become export functions and can still be called as Sheet1.Foo) |
User form frmInput created in the VBE |
forms/frmInput.form.json and forms/frmInput.ras (same as ras form pull) |
- The original VBA is saved in
vba/. Class modules are not imported. - For what was imported, the same sync replaces the workbook side with RAS-generated code (no overwrite option is needed). For sheet and
ThisWorkbookmodules too, the whole module is replaced with code generated from the file inobjects/. - Lines that could not be converted from VBA to RAS (such as
GoToandReDim) remain as// TODO(VBA):comments. For a file that still has such lines, the workbook's code is left as is without being replaced. Fix them and delete the// TODO(VBA):lines, and the next sync replaces it. - If the converted code has compile errors, it does not sync, as with other build errors (fix the errors shown in the editor).
- If you add procedures or variables in the VBE to a module that RAS generated, the next sync (
ras pack) also converts them to RAS and imports them at the end of that module's file (spec §14.7.3). If you rewrite or delete a procedure that RAS generated, it warns about drift. - To turn off importing, write
[sync]pull-unmanaged = falseinras.toml.
Writing VBA as is
Code that is hard to convert to RAS can be written as VBA in a vba block ("Writing VBA as is" in the cheat sheet).