---
title: Excel Macros and Exotel Reports
slug: business-phone-system/excel-macros-and-exotel-reports
docTags: 
createdAt: 2025-10-10T16:33:01.794Z
---

## Get Leg1 & Leg2 Duration from Exotel Call Reports

If you regularly analyze Exotel call reports in Excel, you can automate extraction of Leg1 and Leg2 durations using a VBA macro. Below are prerequisites, a short explanation of the data, step-by-step instructions to import and run the macro, and the macro source code.

## Prerequisites

### Windows

- Excel with the **Developer** tab enabled.
  &#x20;(Macros and VBA tools are on the Developer tab, which is hidden by default — see “Show the Developer tab” in Microsoft Office help.)&#x20;

### Mac

- Make the **Developer** tab visible:&#x20;
  1. Go to **Excel → Preferences… → Ribbon & Toolbar**.&#x20;
  2. In **Customize the Ribbon**, check **Developer** and click **Save**.&#x20;

## About Leg1 & Leg2 Duration

Call reports downloaded from the Exotel dashboard include a PriceDetails field that contains an array. That array can include Duration values (in seconds) for:

- **Leg1** — the From number (outgoing leg)&#x20;
- **Leg2** — the To number (incoming leg)&#x20;

Note: for a completed incoming call, you will typically see only **Leg2** duration.

## Quick Setup: Run the Macro

1. Download LegDuration.bas.&#x20;
2. In Excel, open the **Developer** tab and click **Visual Basic**.&#x20;
3. In the VBA editor: **File → Import File...**, and choose LegDuration.bas.&#x20;
4. Return to your Exotel call report worksheet.&#x20;
5. Click **Macros**, select LegDuration, and click **Run**.&#x20;

:::BlockQuote
**Note:** The macro was created and optimised for Microsoft Office 2016. It is expected to work on newer versions, but Exotel does not provide troubleshooting support for local macro errors — feel free to inspect or modify the source if needed.
:::

## VBA Macro Source (LegDuration.bas)

:::BlockQuote
Sub LegDuration()
' LegDuration Macro
' Keyboard Shortcut: Ctrl+Shift+L

&#x20;   Columns("O\:O").Select
&#x20;   Selection.Copy
&#x20;   Columns("S\:S").Select
&#x20;   ActiveSheet.Paste
&#x20;   Application.CutCopyMode = False

&#x20;   Columns("S\:S").Select
&#x20;   Selection.Replace What:="Divert", Replacement:="Exc", LookAt:=xlPart, \_
&#x20;       SearchOrder:=xlByRows, MatchCase:=False

&#x20;   Selection.TextToColumns Destination:=Range("S1"), DataType:=xlDelimited, \_
&#x20;       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, \_
&#x20;       Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo \_
&#x20;       :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)), \_
&#x20;       TrailingMinusNumbers:=True

&#x20;   Columns("T\:T").Select
&#x20;   Selection.Delete Shift:=xlToLeft
&#x20;   Columns("U\:U").Select
&#x20;   Selection.Delete Shift:=xlToLeft
&#x20;   Selection.Delete Shift:=xlToLeft

&#x20;   Range("S1").Select
&#x20;   Selection.AutoFilter

&#x20;   sheetName = Worksheets(1).Name
&#x20;   Set sht = Worksheets(sheetName)
&#x20;   Set StartCell = Range("R3")
&#x20;   LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
&#x20;   LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column

&#x20;   ActiveSheet.Range("$A$1:$T$" & CStr(LastRow)).AutoFilter Field:=19, Criteria1:="\<>\*Leg1\*"
&#x20;   Range("T1").Select
&#x20;   Application.CutCopyMode = False
&#x20;   ActiveCell.FormulaR1C1 = "=RC\[-1]"
&#x20;   Range("R1").Select
&#x20;   Selection.End(xlDown).Select
&#x20;   Range("T" & CStr(LastRow)).Select
&#x20;   Range("T1\:T" & CStr(LastRow)).Select
&#x20;   Range("T" & CStr(LastRow)).Activate
&#x20;   Selection.FillDown
&#x20;   Range("T1").Select
&#x20;   Selection.AutoFilter

&#x20;   Columns("T\:T").Select
&#x20;   Selection.Copy
&#x20;   Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks \_
&#x20;       :=False, Transpose:=False

&#x20;   Range("S1").Select
&#x20;   Application.CutCopyMode = False
&#x20;   Selection.AutoFilter

&#x20;   ActiveSheet.Range("$A$1:$T$" & CStr(LastRow)).AutoFilter Field:=19, Criteria1:="\<>\*Leg1\*"
&#x20;   Range("S2").Select
&#x20;   Range(Selection, Selection.End(xlDown)).Select
&#x20;   Selection.ClearContents
&#x20;   Selection.End(xlUp).Select
&#x20;   Selection.AutoFilter

&#x20;   Columns("S\:T").Select
&#x20;   Selection.Replace What:="N/A", Replacement:="", LookAt:=xlPart, \_
&#x20;       SearchOrder:=xlByRows, MatchCase:=False

&#x20;   Columns("S\:S").Select
&#x20;   Selection.Cut
&#x20;   Range("U1").Select
&#x20;   ActiveSheet.Paste
&#x20;   Columns("S\:S").Select
&#x20;   Selection.Delete Shift:=xlToLeft

&#x20;   Columns("T\:T").Select
&#x20;   Selection.TextToColumns Destination:=Range("T1"), DataType:=xlDelimited, \_
&#x20;       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, \_
&#x20;       Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar \_
&#x20;       :="D", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

&#x20;   Columns("U\:U").Select
&#x20;   Selection.TextToColumns Destination:=Range("U1"), DataType:=xlDelimited, \_
&#x20;       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, \_
&#x20;       Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar \_
&#x20;       :="=", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

&#x20;   Columns("V\:V").Select
&#x20;   Selection.TextToColumns Destination:=Range("V1"), DataType:=xlDelimited, \_
&#x20;       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, \_
&#x20;       Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar \_
&#x20;       :="s", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

&#x20;   Columns("T\:T").Select
&#x20;   Selection.Delete Shift:=xlToLeft
&#x20;   Selection.Delete Shift:=xlToLeft

&#x20;   Range("T1").Select
&#x20;   ActiveCell.FormulaR1C1 = "Leg1"
&#x20;   Columns("S\:S").Select
&#x20;   Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

&#x20;   Columns("U\:U").Select
&#x20;   Selection.Cut
&#x20;   Range("S1").Select
&#x20;   ActiveSheet.Paste

&#x20;   Columns("T\:T").Select
&#x20;   Selection.TextToColumns Destination:=Range("T1"), DataType:=xlDelimited, \_
&#x20;       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, \_
&#x20;       Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar \_
&#x20;       :="D", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

&#x20;   Columns("U\:U").Select
&#x20;   Selection.TextToColumns Destination:=Range("U1"), DataType:=xlDelimited, \_
&#x20;       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, \_
&#x20;       Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar \_
&#x20;       :="=", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

&#x20;   Columns("V\:V").Select
&#x20;   Selection.TextToColumns Destination:=Range("V1"), DataType:=xlDelimited, \_
&#x20;       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, \_
&#x20;       Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar \_
&#x20;       :="s", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

&#x20;   Columns("T\:U").Select
&#x20;   Selection.Delete Shift:=xlToLeft
&#x20;   Selection.End(xlUp).Select
&#x20;   ActiveCell.FormulaR1C1 = "Leg2"
&#x20;   Range("S1").Select
End Sub
:::

## Troubleshooting & Notes

- The macro assumes your Exotel call export layout has the PriceDetails (or equivalent) column in column **O**. Adjust column references in the macro if your export format differs.&#x20;
- If you encounter errors, verify:&#x20;
  - The Developer tab is enabled and macros are allowed.&#x20;
  - The spreadsheet has data starting at row 1 and the expected columns.&#x20;
  - The Sheet1 name or ID references match your workbook (if integrating with Google Sheets scripts).&#x20;
- This macro performs multiple text-to-columns and replace operations; always test on a copy of your report.&#x20;
