{"id":25161,"date":"2022-08-13T09:26:40","date_gmt":"2022-08-13T08:26:40","guid":{"rendered":"https:\/\/ekiwi-blog.de\/?p=25161"},"modified":"2023-08-12T16:06:32","modified_gmt":"2023-08-12T15:06:32","slug":"vba-convert-boolean-bool-to-string","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/25161\/vba-convert-boolean-bool-to-string\/","title":{"rendered":"VBA convert Boolean\/Bool to String"},"content":{"rendered":"<p>If you program with <abbr title=\"Visual Basic for Application\">VBA<\/abbr> to make certain tasks in the Microsoft Office world easier, you can&#8217;t avoid the fact that sometimes different data types have to be converted into a string, e.g. a date, integer numbers or double numbers. This article is about how to convert a boolean value, as the data type Boolean, which can accept <em>True<\/em> and <em>False<\/em>, into a <a title=\"C# number of string in another string: Count occurrences of a substring in a string\" href=\"https:\/\/ekiwi.de\/en\/index.php\/4658\/c-sharp-count-substring-in-string\/\" target=\"_blank\" rel=\"noopener\">string<\/a>.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi-blog.de\/en\/25161\/vba-convert-boolean-bool-to-string\/#Simple_solution_with_CStr\" >Simple solution with CStr()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi-blog.de\/en\/25161\/vba-convert-boolean-bool-to-string\/#Alternative_individual_conversion_If_statement\" >Alternative individual conversion: If statement<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Simple_solution_with_CStr\"><\/span>Simple solution with CStr()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To convert a Boolean variable into a string, simply use the <em><strong>CStr<\/strong> <\/em>function. This powerfully overloaded function is also suitable for other data types to convert them into a string.<\/p>\n<p>In this example, we simply set a bool value (<em>myBool<\/em>) to True, convert it to a string value, and assign it to the variable <em>myString<\/em>. This in turn we bring to display in a MessageBox.<\/p>\n<figure id=\"attachment_25162\" aria-describedby=\"caption-attachment-25162\" style=\"width: 508px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25162\" src=\"http:\/\/ekiwi-blog.de\/wp-content\/uploads\/2022\/08\/vba-convert-boolean-to-string.jpg\" alt=\"Screenshot VBA code converting boolean to string with CStr() function\" width=\"508\" height=\"188\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2022\/08\/vba-convert-boolean-to-string.jpg 508w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2022\/08\/vba-convert-boolean-to-string-300x111.jpg 300w\" sizes=\"auto, (max-width: 508px) 100vw, 508px\" \/><figcaption id=\"caption-attachment-25162\" class=\"wp-caption-text\">Convert Bool to String using CStr()-Function<\/figcaption><\/figure>\n<pre><code><span style=\"color: #0000ff;\">Sub<\/span> BooleanToString()\r\n    <span style=\"color: #0000ff;\">Dim<\/span> myBool <span style=\"color: #0000ff;\">As Boolean<\/span>\r\n    <span style=\"color: #0000ff;\">Dim<\/span> myString <span style=\"color: #0000ff;\">As String<\/span>\r\n    myBool = <span style=\"color: #0000ff;\">True<\/span>\r\n    myString = <mark><span style=\"color: #0000ff;\">CStr<\/span>(myBool)<\/mark>\r\n    MsgBox myString, vbOKOnly, \"Show Bool as String\"\r\n<span style=\"color: #0000ff;\">End Sub<\/span><\/code><\/pre>\n<p>If you are using another language package in Microsoft Office then localization intervenes here. So it is not simply &#8220;<em>True<\/em>&#8221; or &#8220;<em>False<\/em>&#8221; output, because &#8220;<em>True<\/em>&#8221; or &#8220;<em>False<\/em>&#8221; are translated to the chosen language. In this case <a title=\"forma excel cell as currency with using VBA\" href=\"https:\/\/ekiwi-blog.de\/en\/24933\/vba-excel-format-as-currency\/\">Excel<\/a> immediately carries out the translation from &#8220;<em>True<\/em>&#8221; to your set up language.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Alternative_individual_conversion_If_statement\"><\/span>Alternative individual conversion: If statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Admittedly, with such a conversion one can often do nothing directly with the values &#8220;<em>True<\/em>&#8221; or &#8220;<em>False<\/em>&#8220;. If necessary, these are still suitable for <a title=\"search and find backwards from bottom to top in Excel\" href=\"https:\/\/ekiwi-blog.de\/en\/25078\/excel-find-search-from-bottom-to-top-backwards-search\/\">filtering in Excel<\/a>. But with a simple <em><strong>If-Then-Else<\/strong><\/em> statement you can also make an individual conversion and assign your own values to the string variable depending on the Boolean variable.<\/p>\n<figure id=\"attachment_25156\" aria-describedby=\"caption-attachment-25156\" style=\"width: 487px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25156\" src=\"http:\/\/ekiwi-blog.de\/wp-content\/uploads\/2022\/08\/vba-convert-bool-to-string-using-if-then-else.jpg\" alt=\"Screenshot VBA-Code with bool value and string output\" width=\"487\" height=\"241\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2022\/08\/vba-convert-bool-to-string-using-if-then-else.jpg 487w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2022\/08\/vba-convert-bool-to-string-using-if-then-else-300x148.jpg 300w\" sizes=\"auto, (max-width: 487px) 100vw, 487px\" \/><figcaption id=\"caption-attachment-25156\" class=\"wp-caption-text\">Using If-Then-Else for converting Boolean to String<\/figcaption><\/figure>\n<pre><code><span style=\"color: #0000ff;\">Sub<\/span> BooleanToMyString()\r\n    <span style=\"color: #0000ff;\">Dim<\/span> myBool <span style=\"color: #0000ff;\">As Boolean<\/span>\r\n    <span style=\"color: #0000ff;\">Dim<\/span> myString <span style=\"color: #0000ff;\">As String<\/span>\r\n    myBool = <span style=\"color: #0000ff;\">False<\/span>\r\n    <span style=\"color: #0000ff;\">If<\/span> myBool = True <span style=\"color: #0000ff;\">Then<\/span>\r\n        myString = \"Okay!\"\r\n    <span style=\"color: #0000ff;\">Else<\/span>\r\n        myString = \"Not Okay!\"\r\n    <span style=\"color: #0000ff;\">End If<\/span>\r\n    MsgBox myString, vbOKOnly, \"Show Bool as String\"\r\n<span style=\"color: #0000ff;\">End Sub<\/span><\/code><\/pre>\n<p>This should be the preferred variant in many cases. One simply <a href=\"https:\/\/ekiwi-blog.de\/en\/54089\/vba-check-whether-a-folderdirectory-exists-folderexists\/\" title=\"VBA Check whether a folder\/directory exists (FolderExists)\">checks whether<\/a> a value is &#8220;True&#8221; and then outputs an individual text.<\/p>\n\n<div class=\"aawp\">\n\n            \n            \n<div class=\"aawp-product aawp-product--horizontal aawp-product--bestseller aawp-product--ribbon\"  data-aawp-product-asin=\"3367105732\" data-aawp-product-id=\"109254\" data-aawp-tracking-id=\"ekiwide0b-21\" data-aawp-product-title=\"VBA mit Excel  Das umfassende Handbuch f\u00fcr Einsteiger und fortgeschrittene Anwender Aktuell zu Excel 2024 und Microsoft 365 \u2013 Ausgabe 2025\">\n\n    <span class=\"aawp-product__ribbon aawp-product__ribbon--bestseller\">Bestseller No. 1<\/span>\n    <div class=\"aawp-product__thumb\">\n        <a class=\"aawp-product__image-link\"\n           href=\"https:\/\/www.amazon.de\/dp\/3367105732?tag=ekiwide0b-21&linkCode=ogi&th=1&psc=1&keywords=vba%20books\" title=\"VBA mit Excel: Das umfassende Handbuch f\u00fcr...\" rel=\"nofollow noopener sponsored\" target=\"_blank\">\n            <img decoding=\"async\" class=\"aawp-product__image\" src=\"https:\/\/m.media-amazon.com\/images\/I\/51EVPKCMSSL._SL160_.jpg\" alt=\"VBA mit Excel: Das umfassende Handbuch f\u00fcr...\"  \/>\n        <\/a>\n\n            <\/div>\n\n    <div class=\"aawp-product__content\">\n        <a class=\"aawp-product__title\" href=\"https:\/\/www.amazon.de\/dp\/3367105732?tag=ekiwide0b-21&linkCode=ogi&th=1&psc=1&keywords=vba%20books\" title=\"VBA mit Excel: Das umfassende Handbuch f\u00fcr...\" rel=\"nofollow noopener sponsored\" target=\"_blank\">\n            VBA mit Excel: Das umfassende Handbuch f\u00fcr...        <\/a>\n        <div class=\"aawp-product__description\">\n                    <\/div>\n    <\/div>\n\n    <div class=\"aawp-product__footer\">\n\n        <div class=\"aawp-product__pricing\">\n            \n                            <span class=\"aawp-product__price aawp-product__price--current\">49,90 EUR<\/span>\n            \n            <a href=\"https:\/\/www.amazon.de\/gp\/prime\/?tag=ekiwide0b-21\" title=\"Amazon Prime\" rel=\"nofollow noopener sponsored\" target=\"_blank\" class=\"aawp-check-prime\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/plugins\/aawp\/assets\/img\/icon-check-prime.svg\" height=\"16\" width=\"55\" alt=\"Amazon Prime\" \/><\/a>        <\/div>\n\n                <a class=\"aawp-button aawp-button--buy aawp-button--icon aawp-button--icon-black\" href=\"https:\/\/www.amazon.de\/dp\/3367105732?tag=ekiwide0b-21&#038;linkCode=ogi&#038;th=1&#038;psc=1&#038;keywords=vba%20books\" title=\"Buy on Amazon\" target=\"_blank\" rel=\"nofollow noopener sponsored\">Buy on Amazon<\/a>\n            <\/div>\n\n<\/div>\n\n            \n            \n<div class=\"aawp-product aawp-product--horizontal aawp-product--bestseller aawp-product--ribbon\"  data-aawp-product-asin=\"3658487151\" data-aawp-product-id=\"110422\" data-aawp-tracking-id=\"ekiwide0b-21\" data-aawp-product-title=\"Excel + VBA f\u00fcr Controller  Mit eigenen Prozeduren und Funktionen optimieren\">\n\n    <span class=\"aawp-product__ribbon aawp-product__ribbon--bestseller\">Bestseller No. 2<\/span>\n    <div class=\"aawp-product__thumb\">\n        <a class=\"aawp-product__image-link\"\n           href=\"https:\/\/www.amazon.de\/dp\/3658487151?tag=ekiwide0b-21&linkCode=ogi&th=1&psc=1&keywords=vba%20books\" title=\"Excel + VBA f\u00fcr Controller: Mit eigenen...\" rel=\"nofollow noopener sponsored\" target=\"_blank\">\n            <img decoding=\"async\" class=\"aawp-product__image\" src=\"https:\/\/m.media-amazon.com\/images\/I\/41tfeABCMKL._SL160_.jpg\" alt=\"Excel + VBA f\u00fcr Controller: Mit eigenen...\"  \/>\n        <\/a>\n\n            <\/div>\n\n    <div class=\"aawp-product__content\">\n        <a class=\"aawp-product__title\" href=\"https:\/\/www.amazon.de\/dp\/3658487151?tag=ekiwide0b-21&linkCode=ogi&th=1&psc=1&keywords=vba%20books\" title=\"Excel + VBA f\u00fcr Controller: Mit eigenen...\" rel=\"nofollow noopener sponsored\" target=\"_blank\">\n            Excel + VBA f\u00fcr Controller: Mit eigenen...        <\/a>\n        <div class=\"aawp-product__description\">\n                    <\/div>\n    <\/div>\n\n    <div class=\"aawp-product__footer\">\n\n        <div class=\"aawp-product__pricing\">\n            \n                            <span class=\"aawp-product__price aawp-product__price--current\">39,99 EUR<\/span>\n            \n            <a href=\"https:\/\/www.amazon.de\/gp\/prime\/?tag=ekiwide0b-21\" title=\"Amazon Prime\" rel=\"nofollow noopener sponsored\" target=\"_blank\" class=\"aawp-check-prime\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/plugins\/aawp\/assets\/img\/icon-check-prime.svg\" height=\"16\" width=\"55\" alt=\"Amazon Prime\" \/><\/a>        <\/div>\n\n                <a class=\"aawp-button aawp-button--buy aawp-button--icon aawp-button--icon-black\" href=\"https:\/\/www.amazon.de\/dp\/3658487151?tag=ekiwide0b-21&#038;linkCode=ogi&#038;th=1&#038;psc=1&#038;keywords=vba%20books\" title=\"Buy on Amazon\" target=\"_blank\" rel=\"nofollow noopener sponsored\">Buy on Amazon<\/a>\n            <\/div>\n\n<\/div>\n\n    \n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you program with VBA to make certain tasks in the Microsoft Office world easier, you can&#8217;t avoid the fact<\/p>\n","protected":false},"author":2,"featured_media":13630,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[2252,2251,1702,1703,1704,2168,1700,1705,1706,1558],"class_list":["post-25161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-code-en","tag-coding-en","tag-excel-en","tag-microsoft-en","tag-microsoft-office-en","tag-office-en","tag-programming","tag-vba-en","tag-visual-basic-for-application-en","tag-windows-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/25161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=25161"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/25161\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/13630"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=25161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=25161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=25161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}