{"id":12140,"date":"2024-02-03T10:59:56","date_gmt":"2024-02-03T10:59:56","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1803"},"modified":"2024-02-03T10:59:56","modified_gmt":"2024-02-03T10:59:56","slug":"how-to-declare-a-public-variable-in-vba","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/","title":{"rendered":"How to Declare a Public Variable in VBA"},"content":{"rendered":"

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program.<\/p>\n

Use of Public Variable: <\/strong>Let’s say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be modified in any of the Functions during the execution. But whenever you want to use that variable, you want to latest value. In such a case Public Variable is useful.
\n
\nPublic Variable must be defined under any Module. It can be defined under any Sheet Code. See the Syntax below:<\/p>\n

\nSyntax:<\/strong> Public<\/font> <Variable Name> As <\/font><Variable Type>\n<\/div>\n
\"Public

Public Variable in Excel VBA<\/p><\/div>\n<\/span>","protected":false},"excerpt":{"rendered":"

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let’s say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be modified in any of the Functions during […]<\/p>\n","protected":false},"author":45,"featured_media":242496,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1676],"tags":[],"class_list":["post-12140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel-tips"],"yoast_head":"\nHow to Declare a Public Variable in VBA - Let's excel in Excel<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Declare a Public Variable in VBA\" \/>\n<meta property=\"og:description\" content=\"While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let’s say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be modified in any of the Functions during […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"Let's excel in Excel\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:author\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-03T10:59:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"797\" \/>\n\t<meta property=\"og:image:height\" content=\"422\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vishwamitra Mishra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/learnexcelmacro\" \/>\n<meta name=\"twitter:site\" content=\"@learnexcelmacro\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishwamitra Mishra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"How to Declare a Public Variable in VBA\",\"datePublished\":\"2024-02-03T10:59:56+00:00\",\"dateModified\":\"2024-02-03T10:59:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/\"},\"wordCount\":129,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"image\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png\",\"articleSection\":[\"Excel Tips\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/\",\"name\":\"How to Declare a Public Variable in VBA - Let's excel in Excel\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png\",\"datePublished\":\"2024-02-03T10:59:56+00:00\",\"dateModified\":\"2024-02-03T10:59:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#primaryimage\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png\",\"width\":797,\"height\":422,\"caption\":\"Public Variable in Excel VBA\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel Tips\",\"item\":\"https:\/\/vmlogger.com\/excel\/excel-tips\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Declare a Public Variable in VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\",\"url\":\"https:\/\/vmlogger.com\/excel\/\",\"name\":\"Let's excel in Excel\",\"description\":\"Let's share knowledge\",\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vmlogger.com\/excel\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\",\"name\":\"Vishwamitra Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"width\":528,\"height\":560,\"caption\":\"Vishwamitra Mishra\"},\"logo\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\"},\"description\":\"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.\",\"sameAs\":[\"http:\/\/www.learnexcelmacro.com\",\"http:\/\/www.facebook.com\/vmlogger\",\"https:\/\/x.com\/https:\/\/www.twitter.com\/learnexcelmacro\",\"https:\/\/www.youtube.com\/c\/VMLogger\"],\"url\":\"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Declare a Public Variable in VBA - Let's excel in Excel","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/","og_locale":"en_US","og_type":"article","og_title":"How to Declare a Public Variable in VBA","og_description":"While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let’s say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be modified in any of the Functions during […]","og_url":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/","og_site_name":"Let's excel in Excel","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2024-02-03T10:59:56+00:00","og_image":[{"width":797,"height":422,"url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png","type":"image\/png"}],"author":"Vishwamitra Mishra","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/learnexcelmacro","twitter_site":"@learnexcelmacro","twitter_misc":{"Written by":"Vishwamitra Mishra","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"How to Declare a Public Variable in VBA","datePublished":"2024-02-03T10:59:56+00:00","dateModified":"2024-02-03T10:59:56+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/"},"wordCount":129,"commentCount":0,"publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"image":{"@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png","articleSection":["Excel Tips"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/","url":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/","name":"How to Declare a Public Variable in VBA - Let's excel in Excel","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#primaryimage"},"image":{"@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png","datePublished":"2024-02-03T10:59:56+00:00","dateModified":"2024-02-03T10:59:56+00:00","breadcrumb":{"@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#primaryimage","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2012\/06\/Public-Variable1-1.png","width":797,"height":422,"caption":"Public Variable in Excel VBA"},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/excel\/2024\/02\/how-to-declare-a-public-variable-in-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel Tips","item":"https:\/\/vmlogger.com\/excel\/excel-tips\/"},{"@type":"ListItem","position":3,"name":"How to Declare a Public Variable in VBA"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/excel\/#website","url":"https:\/\/vmlogger.com\/excel\/","name":"Let's excel in Excel","description":"Let's share knowledge","publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vmlogger.com\/excel\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5","name":"Vishwamitra Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","width":528,"height":560,"caption":"Vishwamitra Mishra"},"logo":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/"},"description":"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.","sameAs":["http:\/\/www.learnexcelmacro.com","http:\/\/www.facebook.com\/vmlogger","https:\/\/x.com\/https:\/\/www.twitter.com\/learnexcelmacro","https:\/\/www.youtube.com\/c\/VMLogger"],"url":"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/"}]}},"_links":{"self":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/12140"}],"collection":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/comments?post=12140"}],"version-history":[{"count":0,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/12140\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media\/242496"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media?parent=12140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/categories?post=12140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/tags?post=12140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}