In Excel, we especially want to convert data such as money into text. There are macro and macro-free methods for this. In this article, I will talk about the process of converting the number to text in excel using scripts.

Convert Number to Text in Excel

I have excel betik for this process I'm going to use the macro operation, which is also used for this. But there are some problems when you use a macro to convert a number to text. When you share this document with someone else, that person is constantly given a warning for security reasons. Macros do not work unless the user accepts these warnings. Users who are uneasy because of the warning may not approve this process.

The code below is a script that converts numbers to text. It takes the number from cell A1 and divides it into three parts. Then it converts the digit by digit to text. When the function finishes all operations, it returns the text and writes it to cell B1. Since I converted currency, I wrote "TL" at the end of the text.

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  let sayiIndex = selectedSheet.getRange("A1");
  let metinIndex = selectedSheet.getRange("B1");

  let sayi = sayiIndex.getValue();
  let metin = SayıyıYazıyaÇevir(parseFloat(sayi.toString())) + " TL";

  metinIndex.setValue(metin);
}


function SayıyıYazıyaÇevir(sayi: number): string {

  let birler = ["", "bir", "iki", "üç", "dört", "beş", "altı", "yedi", "sekiz", "dokuz"];
  let onlar = ["", "on", "yirmi", "otuz", "kırk", "elli", "altmış", "yetmiş", "seksen", "doksan"];
  let yüzler = ["", "yüz", "iki yüz", "üç yüz", "dört yüz", "beş yüz", "altı yüz", "yedi yüz", "sekiz yüz", "dokuz yüz"];
  let binler = ["", "bin", "milyon", "milyar", "trilyon", "katrilyon"]; // Binlik basamağı

  let metin = "";

  if (sayi === 0) {
    return "sıfır";
  }


  for (let i = 5; i >= 0; i--) {
    let basamak = Math.floor(sayi / Math.pow(10, i * 3)); // 3 basamaklı bloklar halinde sayıyı böler
    if (basamak > 0) {
      if (basamak >= 100 && basamak % 100 === 0) {
        metin += yüzler[Math.floor(basamak / 100)] + " " + binler[i] + " ";
      } else {
        if (i === 1 && basamak < 100 && basamak >= 10) {
          metin += onlar[Math.floor(basamak / 10)] + " " + birler[basamak % 10] + " " + binler[i] + " ";
        } else {
          metin += yüzler[Math.floor(basamak / 100)] + " ";
          if (basamak % 100 >= 10 && basamak % 100 < 20) {
            metin += onlar[basamak % 10] + " on " + binler[i] + " ";
          } else {
            metin += onlar[Math.floor((basamak % 100) / 10)] + " " + birler[basamak % 10] + " " + binler[i] + " ";
          }
        }
      }
      sayi %= Math.pow(10, i * 3);
    }
  }

  if (sayi > 0) {
    if (metin !== "") {
      metin += " ";
    }
    if (sayi < 10) {
      metin += birler[sayi];
    } else if (sayi < 20) {
      metin += onlar[sayi % 10] + " on";
    } else {
      metin += onlar[Math.floor(sayi / 10)] + " " + birler[sayi % 10];
    }
  }

  return metin.trim(); 
}

You will write this code in the scripts in the "Automate" field when you open excel. For this script to work, you can right click on the script and add a button to the workbook. However, it would be best to automate this script. I assign data to this excel via Power Automate and then run this script automatically to convert the number to text.

This code only converts whole numbers. If you write a decimal number, it will not convert the part after the comma. If you are going to convert such a number to text, you need to update the code. If you are unfamiliar with the concept of scripting in Excel, you may have difficulty applying the code at first. You can comment below to let us know what you are having difficulty with.

Note I tested the code for a long time with different numbers. The code can be updated if there is a missed number.

Many different operations can be done with Excel scripts. I recommend you to try it especially if you are actively using office365.

See you in the next article content...