5 Ways to Generate a GUID in Microsoft Excel

Do you need to generate a GUID in Excel?

GUID stands for Globally Unique Identifier, but it might also be referred to as a UUID which stands for Universally Unique Identifier. These are the same thing.

A GUID is a 32-character code consisting of both numbers and letters that are commonly used to identify items in a database.

GUIDs are generated randomly, but the probability of duplicating a randomly generated GUID is so infinitesimally small that you can be sure that it will be unique. This means there is no need for a central registry to ensure their uniqueness.

This post is going to show you how you can generate random GUIDs in Excel. Get your copy of the file used in this post and follow along!

Generate a GUID with the RANDBETWEEN Function

GUIDs are usually 128 bits long and will have the below format where the x‘s are hexadecimal digits.

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

This format organizes the 32 hexadecimal digits into groups of 8-4-4-4-12 characters. The dash character is purely for ease of reading.

A common way to generate these random hex digits in the GUIDs is to generate random decimal numbers and convert them into hexadecimal.

= RANDBETWEEN ( min, max )

This can be achieved using the RANDBETWEEN function in Excel. This function allows you to generate a random integer number between a given upper min and lower max bound.

= DEC2HEX ( number, [digits] )

The DEC2HEX function will convert any decimal number to its equivalent hexadecimal representation. It also allows you to specify the number of digits to return. This way you can always return an 8 or 4 character length hexadecimal.

= DEC2HEX ( RANDBETWEEN ( 0, 4294967295 ), 8 )

A random sequence of 8 hex digits can be generated by generating a random number between 0 and 4,294,967,295 and converting the result to a hex value using the DEC2HEX function in Excel.

= DEC2HEX ( RANDBETWEEN ( 0, 65535), 4 )

Similarly, a random sequence of 4 hex digits can be generated by generating a random number between 0 and 65535 and converting it to hexadecimal.

Then you can generate a sequence of 12 hex digits by combining an 8 and 4 sequence.

=LOWER(CONCATENATE(
    DEC2HEX(RANDBETWEEN(0,4294967295),8),"-",
    DEC2HEX(RANDBETWEEN(0,65535),4),"-",
    DEC2HEX(RANDBETWEEN(0,65535),4),"-",
    DEC2HEX(RANDBETWEEN(0,65535),4),"-",
    DEC2HEX(RANDBETWEEN(0,4294967295),8),
    DEC2HEX(RANDBETWEEN(0,65535),4)
))

The above formula will generate the full GUID value including the joining dash characters.

The CONCATENATE function joins all the hex values together along with the dash to create the full GUID.

The LOWER function can also be used in the formula to create a GUID that returns lowercase instead of uppercase letters.

📝 Note: The RANDBETWEEN function is volatile so this will recalculate any time you edit the spreadsheet. You can force the recalculation and generate a new GUID by pressing the F9 key.

Generate a GUID with the WEBSERVICE Function

WEBSERVICE is a very interesting function. It allows you to call a web address and return data from that address.

When combined with a service such as uuidtools, the WEBSERVICE function will give you the ability to create your GUIDs.

https://www.uuidtools.com/api/generate/v4/count/5

The uuidtools online GUID generator is a free API you can use to generate GUIDs and UUIDs. The above URL will return a list of 5 generated GUIDs. You can change the 5 in the URL to any number you like.

= WEBSERVICE ( "https://www.uuidtools.com/api/generate/v4/count/5" )

The above formula will return a list of GUIDs.

["736f07e7-12e1-4d9f-aa83-762916761eba","c516d5f6-f816-448c-a2f7-dcb308c02393","04ce3d1b-d4f0-4c41-88cd-b90f7deacde4","960445e7-4e04-4bc1-a220-a7e6eb45fde0","d34c0e8b-11fa-4410-a673-2412aea39569"]

The function returns a single text string in square brackets which is a comma-separated list of GUIDs.

This comma-separated list of GUIDs can easily be parsed into separated cells using the TEXTSPLIT function.

= TEXTSPLIT ( C2, , {""",""","[","]",""""}, TRUE )

The above formula will split the list of comma-separated GUIDs based on these sets of characters ",", [, ], " in that order.

TEXTSPLIT splits these GUIDs into rows and then also removed any blank cells to leave only the GUIDs each in its own cell.

Generate a GUID with Power Query

Power Query is loading and transforming your data. It also has a fairly easy way to create GUIDs.

The Power Query formula language includes a Text.NewGuid function which will generate a GUID for you.

This means when you are importing or transforming a data set, you can add a column of GUIDs.

You can import your data into Power Query through these steps.

  1. Select your table.
  2. Go to the Data tab.
  3. Click on the From Table/Range command.

Now the dataset is in the Power Query editor. Go to the Add Column tab and press the Custom Column button in the General section.

This will open the Custom Column formula editor.

#table({"GUID"},{{Text.NewGuid()}})[GUID]{0}

Give your new column a name and enter the above formula into the Custom column formula input, then press the OK button.

This part #table({"GUID"},{{Text.NewGuid()}}) creates a table with a single column named GUID and a single row containing a GUID value.

Then [GUID]{0} will get the zeroth row in the GUID column. In other words, it gets the GUID value out of the table that was created.

⚠️ Warning: Unfortunately, you can’t just use the formula =Text.NewGuid(), this will result in each row having the same GUID when it’s loaded to Excel. Creating a table #table() with the formula seems to work and results in different GUIDs across the rows.

This creates a new column in the data preview with a different GUID value in each row.

This can then be loaded back into Excel. Go to the Home tab of the Power Query editor and press the Close and Load button.

Generate a GUID with Power Automate

Power Automate has the ability to connect to Excel spreadsheets saved in SharePoint or OneDrive. This means you can automate processes involving Excel and other cloud apps.

Power Automate comes with its own workflow expression language which contains a guid function.

This can be used to create GUIDs and add them to an Excel table.

You generally won’t want your GUIDs to change, so this method is great because it will add static values to Excel. Whereas the previous methods will produce GUIDs that will change when you refresh Excel.

You can easily make a simple flow that will add GUIDs to an Excel table with these steps.

  1. Go to https://make.powerautomate.com/ and create a new flow using the button trigger.
  2. Add the Add a row into a table action to the flow.
  3. Select the Excel file and table to which you want to add your GUIDs from the Location, Document Library, File, and Table dropdowns.

When you select the Table, a list of columns in the table will appear in the action.

  1. Click in the column to add the GUID.
  2. Click on the Expression tab in the popup.
  3. Enter guid() in the formula bar.
  4. Click the Update button.
  5. Save the flow.

Now when you run the flow it will add a GUID to the table!

Generate a GUID with VBA

You might not want to save your spreadsheet in the cloud to use Power Automate, but still want a way to create static GUIDs.

This is where VBA might be useful.

You can create a macro to create and enter GUIDs in your workbook without the need for an external tool.

Go to the Developer tab and click on the Visual Basic command to open the visual basic editor. Alternatively, you can press Alt + F11 to open the editor.

Inside the visual basic editor, go to the Insert menu and select the Module option. This is where you can add the macro code.

The code relies on two functions and a procedure. You will need to copy and paste all of them into the module.

Function randBetween(ByVal min As Long, max As Long)
    randBetween = Int(Rnd() * (max - min + 1)) + min
End Function

This function allows you to generate a random number between a min and a max value.

Function GUID()

Dim guid1, guid2, guid3, guid4, guid5, guid6, guid7, guid8 As String

guid1 = LCase(Hex(randBetween(0, 65535)))
guid2 = LCase(Hex(randBetween(0, 65535)))
guid3 = LCase(Hex(randBetween(0, 65535)))
guid4 = LCase(Hex(randBetween(0, 65535)))
guid5 = LCase(Hex(randBetween(0, 65535)))
guid6 = LCase(Hex(randBetween(0, 65535)))
guid7 = LCase(Hex(randBetween(0, 65535)))
guid8 = LCase(Hex(randBetween(0, 65535)))

guid1 = Right(String(4, "0") & guid1, 4)
guid2 = Right(String(4, "0") & guid2, 4)
guid3 = Right(String(4, "0") & guid3, 4)
guid4 = Right(String(4, "0") & guid4, 4)
guid5 = Right(String(4, "0") & guid5, 4)
guid6 = Right(String(4, "0") & guid6, 4)
guid7 = Right(String(4, "0") & guid7, 4)
guid8 = Right(String(4, "0") & guid8, 4)

GUID = guid1 & guid2 & "-" & guid3 & "-" & guid4 & "-" & guid5 & "-" & guid6 & guid7 & guid8

End Function

The above function allows you to create a GUID and it relies on the previous randBetween() VBA function to generate the hex values.

Sub GenerateGUID()
Dim rng As Range

For Each rng In Selection
    rng.Value = GUID()
Next rng
End Sub

The subroutine then loops through each cell in the selected range and adds a GUID generated from the GUID() VBA function.

All you need to do to use this is select the range of cells to which you’d like to add GUID values, then run the GenerateGUID macro.

Generate a GUID with Office Scripts

Another way to add static GUIDs in a range is with Office Scripts.

Go to the Automate tab and click on the New Script command.

function main(workbook: ExcelScript.Workbook) {
  //Create a range object from selected range
  let selectedRange = workbook.getSelectedRange();
  //Get dimensions of selected range
  let rowHeight = selectedRange.getRowCount();
  let colWidth = selectedRange.getColumnCount();

  //Loop through each item in the selected range
  for (let i = 0; i < rowHeight; i++) {
    for (let j = 0; j < colWidth; j++) {
      selectedRange.getCell(i, j).setValue(guid());
    }
  }
};

function randBetween(min: number, max: number) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
};

function guid() {
  let guid1 = randBetween(0, 4294967295).toString(16).padStart(8, '0');
  let guid2 = randBetween(0, 65535).toString(16).padStart(4, '0');
  let guid3 = randBetween(0, 65535).toString(16).padStart(4, '0');
  let guid4 = randBetween(0, 65535).toString(16).padStart(4, '0');
  let guid5 = randBetween(0, 4294967295).toString(16).padStart(8, '0');
  let guid6 = randBetween(0, 65535).toString(16).padStart(4, '0');
  let guid = guid1 + '-' + guid2 + '-' + guid3 + '-' + guid4 + '-' + guid5 + guid6;
  return guid;
};

This will open up the Code Editor and you can paste in the above code.

This code consists of three functions.

  • randBetween() is a function that allows you to generate a random number between a minimum and a maximum.
  • guid() uses the randBetween() function to generate the GUID in a similar way to the first method with the RANDBETWEEN Excel function.
    • .toString(16) will convert the random number to its hex value.
    • .padStart(8, '0') ensures the hex value is 8 characters.

The main() then loops through the selected range and populates a GUID in each cell using the guid() function.

All you need to do is select a range of cells in which you’d like to add GUID values and then run the script.

Conclusions

GUIDs are very commonly used for uniquely identifying items in a database. When working with data in Excel, there are a number of ways to generate a GUID in Excel.

If you need to generate a GUID quickly and don’t mind using an external service, the WEBSERVICE function may be the best option.

For those who prefer working entirely within the RANDBETWEEN or Power Query methods will be the way to go. However, these will create volatile GUIDs that change when the spreadsheet is refreshed.

Using Power Automate, VBA, or Office Scripts all provide a way to generate GUIDs in Excel that are static and won’t continuously change.

Have you ever needed to create GUIDs in Excel? How did you create them? Let me know in the comments below!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and qualified actuary with over 15 years of experience. He has worked in a variety of industries, including insurance, ad tech, and most recently Power Platform consulting. He is a keen problem solver and has a passion for using technology to make businesses more efficient.

Related Posts

Comments

0 Comments

Get the Latest Microsoft Excel Tips

Follow Us

Follow us to stay up to date with the latest in Microsoft Excel!

Subscribe for awesome Microsoft Excel videos 😃