Use Excel VBA to generate QR code and adapt size to cell

It becomes a trend to use QR code to transform complicate words to a QR picture. It would be much more useful if we can do this in Excel and automate this process.

This article is to tell how to achieve this by utilizing online QR code service and save the QR code in your Excel file.

For people who not care about the program detail, at the end the article, the full source code and executable .xlsm are attached. If you have security concern of the .xlsm, you can copy-paste the source code to a fresh .xlsm manually. A demo video (Chinese voice with Youtube English subtitle) is also provided to make it work on your side easier.

 

 

USE EXCEL VBA TO GENERATE QRCODE

Excel uses VBA (VB for application) language to do things programmatically. It’s the focus of this article. For QR code generating, we uses HTTP GET via online service to transform text to QR code image. Although this is a free and quick service, the risk is the network / service breaks down. So, remember to use this service properly and hope the provider not shut it down.

Program Flow

The flow is described below,

  1. For each cell in ColumnA, the program generates a QR code at Column E.
  2. The way to generate the QR code is to apply the text to URL  “https://api.qrserver.com/v1/create-qr-code/?size=150×150&data=YOUR_TEXT_HERE“. Then, download the image of the URL. This is a PNG image file.
  3. Insert the QR code image into ColumnE and set the cell size same as the image (150×150 in the program).
  4. Done
Key Techniques

There are couple key techniques.

  1. How to use VBA to insert pciture and set cell property to align to picture’s size
  2. How to convert the target string to URL-encoded string. The target string may contain space, special character and UTF-8. These string should be encoded as field in HTTP GET field.
  3. Find one online service for convert string to QR code image.

The forth item may not be such a key factor. But it does be important. The service we found just give you the picture without showing any advertisement or agreement.

Code Explanation

Insert picture(QRCODE IMG) and adapt to cell position and size:This is no hard but lots of outdate information. It seems the way to achieve is different since Office 2007. The code here is tested on Office 2010.

 

Download file from URL:Refer to someone’s article in Internet, use DLL to achieve. Not well understood, just use it.

 

Convert string to URL encoded:Also from Internet, just use it. Confirmed feasible for space and UTF-8

Generate QRCODE and insert at specific location: This function generates QR code of the string in cell ‘src’ and insert at position of cell ‘dst’. The string in ‘src’ is URL encoded and pass to online service to get its QR code. The QR code image is downloaded at place at specified location and adapt its size to selected cell.

 

Major Macro <Update QRCODE>: We use some tricks to make it easier to use. Assumed A1 contains a string to convert to QR code, we will put its QR code at A5.(Actually above it, A5 still contains string). Then, we copy the A1’s contain to A5. At the next time we want to generate QR code, the program will check if A1==A5. If they doesn’t match, the new QR code is generated else we pass it. The can effectively accelerate the update speed by doing modified items only.

 

Marjo Macro <Remove QRCODE>: We already have function to generate. So, here is the clean one. We already name QR image while insertion. This function will find out all the picture with the name and delete them. Also, all data in column 5 are removed. (Max 1000 record).

 

 

Demo Video (English Subtitle)

The demo image is at https://www.youtube.com/watch?v=-rlkrQqnd2o. Chinese pronunciation with English subtitle available.

 

 

File Downloads

  1. EXCEL auto QRCODE generating source code (Text File)
  2. EXCEL auto QRCODE generating .xlsm file –> This will show warning for Macro security concern. Please enable it. Or, open a whole new .xlsm and paste the source code (the file1) to it.
  3. *** While testing the file on new installed, it is found that the code on system without “Drive D” doesn’t work. To fix this, please do the replacement in macro content. “d:kkk.png” should be replaced as  Environ(“TEMP”) + “kkk.png”. All the bold part needs to be replaced including the double quote sign.

Leave a Reply(Name請以user_開頭,否則會被判定會垃圾息)

請輸入答案 ÷ 7 = 1