REPLACE Function (LibreOffice Calc)
The REPLACE function in LibreOffice Calc substitutes part of a text string based on position and length. It is essential for structured text editing, code manipulation, and controlled string transformations.
Verification status: Consult the documentation, examples and any stated test scope below. How formula examples are checked
Availability and version history
- Introduced: Documented by LibreOffice 26.8.1.1; original introduction not established.
- Availability: Supported in LibreOffice 26.8.1.1, verified against its function registry. This identifies a supported release, not a final working version.
- Scope: Desktop Calc, using the English function name. Availability in other spreadsheet applications is separate.
- Checked: 2026-10-14 — function reference; versioned support. Documentation/source check; not a new native test. Existing example-specific test claims below retain their stated scope.
Native-tested visual example: Correct fixed-position codes
Quick answer: Replace three characters starting at position ten. The following example was run in LibreOffice Calc 25.2.3.2, not simulated or inferred from a formula description.
Enter the sample data
The downloadable workbook uses these exact rows on its Example sheet. Enter the values or download the editable REPLACE ODS practice workbook instead.
| Sheet row | A: Invoice code | B: Team | C: Status | D: Note |
|---|---|---|---|---|
| 5 | INV-2026-NEW | A | Open | good |
| 6 | INV-2025-OLD | B | Closed | past |
| 7 | INV-2024-OLD | A | Open | historical |
| 8 | INV-2026-OLD | C | Pending | needs correction |
| 9 | INV-2026-NEW | C | Closed | done |
| 10 | INV-2027-OLD | A | Pending | future |
Formula and result
Select E5 in the practice workbook. The formula is:
=REPLACE(A8;10;3;"NEW")
Verified output: INV-2026-NEW. Position 10 is the O in OLD; replace three characters to correct the suffix.

Screenshot captured from LibreOffice Calc 25.2.3.2. The formula, input values and E5 result are visible in the actual application.
Second example you can try
The workbook also includes this independently tested variation in E12:
=REPLACE(A6;10;3;"NEW")
Verified output: INV-2025-NEW. Change the related input cell and press Enter to see the result update.
Common mistake: REPLACE counts character positions, not a searched word. For replace-by-match use SUBSTITUTE.
Official documentation: LibreOffice Calc reference for REPLACE. Test scope: These two worked examples were recalculated in Calc 25.2.3.2; other historical formulas elsewhere on this page may still require separate testing. This file includes no generated or artificial application screenshot.
What the REPLACE Function Does
- Replaces characters starting at a specific position
- Removes or inserts text by controlling length
- Works with text, numbers (converted to text), and formulas
- Ideal for structured, positional editing
It is designed to be precise, predictable, and essential for fixed‑format text manipulation.
Syntax
REPLACE(text; start; num_chars; new_text)
Arguments
-
text:
The original text string. -
start:
The position (1‑based) where replacement begins. -
num_chars:
Number of characters to remove. -
new_text:
The text to insert in place of the removed characters.
Basic Examples
Replace 3 characters starting at position 2
=REPLACE("Hello"; 2; 3; "i")
Returns "Hio".
Insert text without removing characters
=REPLACE("Hello"; 2; 0; "X")
Returns "HXello".
Remove characters without inserting anything
=REPLACE("Hello"; 2; 3; "")
Returns "Ho".
Replace part of a number (converted to text)
=REPLACE(2024; 3; 2; "99")
Returns "2099".
Advanced Examples
Mask part of an ID
=REPLACE(A1; 4; 4; "****")
Replace file extension
=REPLACE(A1; FIND("."; A1) + 1; LEN(A1); "txt")
Replace characters after a delimiter
=REPLACE(A1; FIND("-"; A1) + 1; LEN(A1); "NEW")
Replace the last N characters
=REPLACE(A1; LEN(A1)-2; 3; "XYZ")
Insert text at the end
=REPLACE(A1; LEN(A1)+1; 0; "_END")
Replace middle word in a three‑word string
=REPLACE(A1; FIND(" "; A1) + 1; FIND(" "; A1; FIND(" "; A1) + 1) - FIND(" "; A1) - 1; "NEW")
Replace characters dynamically
=REPLACE(A1; B1; C1; D1)
Where B1 = start, C1 = length, D1 = replacement.
Replace only if delimiter exists
=IF(ISERROR(FIND("-"; A1)); A1; REPLACE(A1; FIND("-"; A1)+1; 99; "OK"))
Replace characters in fixed‑width data
=REPLACE(A1; 6; 2; "00")
Edge Cases and Behavior Details
start < 1 → Err:502
=REPLACE("ABC"; 0; 1; "X")
num_chars < 0 → Err:502
=REPLACE("ABC"; 2; -1; "X")
start beyond text length inserts at end
=REPLACE("ABC"; 10; 2; "X") → "ABCX"
num_chars larger than remaining text removes to end
=REPLACE("ABC"; 2; 10; "X") → "AX"
text is a number → converted to text
=REPLACE(12345; 2; 2; "00") → "10005"
text is empty
=REPLACE(""); 1; 1; "X" → "X"
text is an error → error propagates
=REPLACE(#N/A; 2; 3; "X") → #N/A
new_text may be empty
=REPLACE("ABC"; 2; 1; "") → "AC"
Common Errors and Fixes
Err:502 — Invalid argument
Occurs when:
- start < 1
- num_chars < 0
- start or num_chars is non‑numeric
REPLACE returns unexpected output
Cause:
- Off‑by‑one start position
- num_chars too large
- Hidden characters in text
Fix:
Use LEN, TRIM, CLEAN to inspect and normalize.
REPLACE inserts at wrong position
Cause:
- start beyond text length
- start miscalculated from FIND/SEARCH
Best Practices
- Use REPLACE for positional editing and fixed‑width text
- Use SUBSTITUTE when replacing matching text, not positions
- Combine with FIND/SEARCH for delimiter‑based replacement
- Use LEN to calculate dynamic start/length values
- Use TRIM and CLEAN to normalize text before replacement
Related Patterns and Alternatives
- Use SUBSTITUTE to replace matching text
- Use LEFT, RIGHT, MID for extraction
- Use FIND and SEARCH to locate positions
- Use LEN to calculate dynamic ranges
- Use TEXTJOIN or CONCAT for reconstruction
By mastering REPLACE and its companion functions, you can build powerful, structured, and highly controlled text‑processing workflows in LibreOffice Calc.
Reproducible example (tested in LibreOffice Calc)
This standalone example was calculated in LibreOffice Calc 25.2.3.2 and matched the expected value. It is a practical cross-check of one usage, not proof that every example elsewhere on this page is correct.
=REPLACE("abcdef";2;3;"XYZ")
Expected result: aXYZef.
Replace characters at a specified position and length.
If the result differs, check the referenced cells, argument separators and number formatting. For more information, consult the LibreOffice Calc official help.