How to Generate GUIDs in Excel and Google Sheets
By SimpleTools24Published 4 min read
Excel doesn't have a built-in GUID function, so you have three options: build a version 4 GUID with a formula using RANDBETWEEN and DEC2HEX, add a GUID column in Power Query with Text.NewGuid(), or use a short VBA macro. In Google Sheets, an Apps Script custom function using Utilities.getUuid() returns a proper UUID. Whichever you use, convert the results to fixed values, because formula-based GUIDs change every time the sheet recalculates.
In this guide
Method 1: A Formula (Excel and Google Sheets)
This formula builds a string in the version 4 format, with the version digit fixed to 4 and the variant digit in the 8–b range:
=LOWER(DEC2HEX(RANDBETWEEN(0,4294967295),8)&"-"&DEC2HEX(RANDBETWEEN(0,65535),4)&"-4"&DEC2HEX(RANDBETWEEN(0,4095),3)&"-"&DEC2HEX(RANDBETWEEN(32768,49151),4)&"-"&DEC2HEX(RANDBETWEEN(0,16777215),6)&DEC2HEX(RANDBETWEEN(0,16777215),6))How it fits together:
| Group | Formula part | Result |
|---|---|---|
| 1 (8 digits) | DEC2HEX(RANDBETWEEN(0,4294967295),8) | Random 32 bits |
| 2 (4 digits) | DEC2HEX(RANDBETWEEN(0,65535),4) | Random 16 bits |
| 3 (4 digits) | "4" & DEC2HEX(RANDBETWEEN(0,4095),3) | Version 4 plus 12 random bits |
| 4 (4 digits) | DEC2HEX(RANDBETWEEN(32768,49151),4) | Starts with 8, 9, a or b: the variant |
| 5 (12 digits) | Two 6-digit random parts | Random 48 bits |
The Two Big Caveats
- It recalculates.
RANDBETWEENis volatile: every edit anywhere in the workbook generates new values. Immediately copy the column and use Paste Special → Values to freeze it. - It isn't cryptographic randomness. Spreadsheet random functions are fine for test data and row IDs in a personal sheet, but not for security tokens or systems where unpredictability matters.
Method 2: Power Query in Excel
Power Query has a native GUID function, which is cleaner than a formula:
- Select your table and choose Data → From Table/Range.
- In the Power Query editor, choose Add Column → Custom Column.
- Enter
Text.NewGuid()as the formula and name the column, for exampleId. - Close & Load.
The GUIDs are generated when the query runs. Refreshing the query generates new ones, so if the IDs must stay fixed, load the result and then copy it as values into a separate table.
Method 3: A VBA Macro (Excel Desktop)
A macro can write fixed GUIDs into the selected cells, so nothing recalculates. A common approach builds the string from random numbers in VBA, similar to the formula above, and writes it into each selected cell. Older examples online use the Scriptlet.TypeLib COM object, but it may be blocked or unavailable on modern, locked-down Windows systems, so test any macro in your environment and follow your organisation's macro policy.
Method 4: Google Sheets With Apps Script
Google Sheets can call Apps Script's built-in UUID generator:
- Open Extensions → Apps Script.
- Paste this function and save:
function UUID() {
return Utilities.getUuid();
}- In the sheet, enter
=UUID()in a cell.
Utilities.getUuid() returns a random UUID string. Custom functions can recalculate when the sheet reloads, so copy and Paste special → Values only once you have the IDs you need.
Method 5: Generate Outside and Paste In
For a one-off column of IDs, the simplest route avoids formulas entirely. The GUID generator creates up to 1,000 version 4 (or other version) GUIDs using your browser's cryptographic random number generator, in lowercase, uppercase, braced or compact style. Download them as CSV and open the file, or copy and paste the list straight into a column. The values are fixed, so there's nothing to recalculate. If your data starts as JSON rather than a spreadsheet, the JSON to Excel converter can turn it into an XLSX first.
Checking the Results
Whichever method you use, check a few values: each should be 36 characters, with the 13th character 4 for version 4 GUIDs. UUID format explained shows where the version and variant digits live, and what is UUID v4 explains why those digits are fixed.
Using GUIDs as Row Keys in Spreadsheets
GUID columns are handy when rows from a spreadsheet will be imported into another system, merged with other sheets or synced with an app:
- Add the ID column first, before sorting or filtering, so every row keeps its identifier.
- Lock or protect the column to stop accidental edits.
- Check for blanks before importing; a missing ID is easy to overlook.
- Avoid copying rows without generating new IDs for the copies, or you'll create duplicates.
Removing Duplicate GUIDs
If you suspect duplicates, use Data → Remove Duplicates in Excel on the ID column (after making a backup), or =COUNTIF(A:A, A2) > 1 in a helper column to flag repeated values in either Excel or Google Sheets. Duplicates usually come from copied rows rather than from the generator itself.
Frequently Asked Questions
Does Excel have a GUID function?
Not as a worksheet function. Power Query's Text.NewGuid() and VBA are the built-in routes, alongside formulas you build yourself.
Why do my GUIDs keep changing?
Formula-based GUIDs recalculate. Paste them as values to freeze them.
Is the Excel formula a real UUID?
It follows the version 4 format, but its randomness comes from the spreadsheet's random function rather than a cryptographic generator.
How do I create a GUID in Google Sheets?
Use an Apps Script custom function that returns Utilities.getUuid(), then paste the results as values.
Summary
Use Power Query's Text.NewGuid() or a frozen formula in Excel, Apps Script in Google Sheets, or generate a batch externally and paste it in. Always freeze the values so your IDs don't change.
Related tools
Sources
- Text.NewGuid – Microsoft Learn (Power Query M) (opens in a new tab)
- Class Utilities – getUuid() – Google Apps Script (opens in a new tab)
Related articles
- Generate a GUID in PowerShellNew-Guid, [guid]::NewGuid(), formatting, bulk generation, clipboard tricks and validation in PowerShell.
- What Is UUID v4?The structure of a version 4 UUID, its 122 random bits, how to spot one and when it's the right choice.
- UUID Format ExplainedThe 8-4-4-4-12 layout, where the version and variant live, and the text forms systems accept.