{"id":3086,"date":"2018-09-22T17:24:41","date_gmt":"2018-09-22T17:24:41","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=3086"},"modified":"2024-03-29T15:48:44","modified_gmt":"2024-03-29T15:48:44","slug":"sum-function","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/","title":{"rendered":"Sum Function in VBA"},"content":{"rendered":"\n

As you know, you can use the SUM function or the autosum button<\/a> to sum a range inside an Excel worksheet. But you can also do it by using the VBA code.<\/p>\n\n\n\n

There are a few ways you can do it. Take a look at the following examples. The sum of all cells equals 21.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Normally, you can use the following Excel function.<\/p>\n\n\n\n

=SUM(A1:B3)<\/pre>\n\n\n\n

If you want to use this range and return the value in an active cell, you have to modify the code. First, open the VBA editor by pressing Alt + F11<\/strong>.<\/p>\n\n\n\n

Using the Excel SUM function<\/h2>\n\n\n\n

The easiest way to do it is to use the function we created earlier and put it into parentheses.<\/p>\n\n\n\n

Sub SumVBA1()\n    ActiveCell.Value = \"=Sum(A1:B3)\"\nEnd Sub\n<\/pre>\n\n\n\n

If you execute this code, it will return 21.<\/p>\n\n\n\n

You can also specify the row and column number.<\/p>\n\n\n\n

Sub SumVBA2()\n    ActiveCell.Value = \"=SUM(\" & Range(Cells(1, 1), Cells(3, 2)).Address(False, False) & \")\"\nEnd Sub\n<\/pre>\n\n\n\n

This time, instead of using reference, we specified a range between row 1 and column 1, and between row 3 and column 2.<\/p>\n\n\n\n

Using the VBA SUM function<\/h2>\n\n\n\n

This time instead of using the Excel SUM function<\/a> in parentheses, we will do it using the proper way, by using the VBA Sum function.<\/p>\n\n\n\n

The following code is a VBA alternative of the first example.<\/p>\n\n\n\n

Sub SumVBA3()\n    ActiveCell.Value = WorksheetFunction.Sum(Range(\"A1:B3\"))\nEnd Sub\n<\/pre>\n\n\n\n

This code is the alternative to the second example, where you can specify the row and column number.<\/p>\n\n\n\n

Sub SumVBA4()\n    ActiveCell.Value = Application.Sum(Range(Cells(1, 1), Cells(3, 2)))\nEnd Sub\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

As you know, you can use the SUM function or the autosum button to sum a range inside an Excel worksheet. But you can…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[190],"yoast_head":"\nSum Function in VBA<\/title>\n<meta name=\"description\" content=\"Learn to sum a range inside an Excel worksheet using VBA code. Modify the code and return the value in an active cell. Examples included.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sum Function in VBA\" \/>\n<meta property=\"og:description\" content=\"Learn to sum a range inside an Excel worksheet using VBA code. Modify the code and return the value in an active cell. Examples included.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-22T17:24:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-29T15:48:44+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png\" \/>\n<meta name=\"author\" content=\"Tomasz Decker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tomasz Decker\" \/>\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:\/\/officetuts.net\/excel\/vba\/sum-function\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"Sum Function in VBA\",\"datePublished\":\"2018-09-22T17:24:41+00:00\",\"dateModified\":\"2024-03-29T15:48:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/\"},\"wordCount\":216,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png\",\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/\",\"name\":\"Sum Function in VBA\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png\",\"datePublished\":\"2018-09-22T17:24:41+00:00\",\"dateModified\":\"2024-03-29T15:48:44+00:00\",\"description\":\"Learn to sum a range inside an Excel worksheet using VBA code. Modify the code and return the value in an active cell. Examples included.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#primaryimage\",\"url\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png\",\"contentUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sum Function in VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sum Function in VBA","description":"Learn to sum a range inside an Excel worksheet using VBA code. Modify the code and return the value in an active cell. Examples included.","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:\/\/officetuts.net\/excel\/vba\/sum-function\/","og_locale":"en_US","og_type":"article","og_title":"Sum Function in VBA","og_description":"Learn to sum a range inside an Excel worksheet using VBA code. Modify the code and return the value in an active cell. Examples included.","og_url":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/","article_published_time":"2018-09-22T17:24:41+00:00","article_modified_time":"2024-03-29T15:48:44+00:00","og_image":[{"url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png"}],"author":"Tomasz Decker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tomasz Decker","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"Sum Function in VBA","datePublished":"2018-09-22T17:24:41+00:00","dateModified":"2024-03-29T15:48:44+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/"},"wordCount":216,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png","keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/","url":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/","name":"Sum Function in VBA","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png","datePublished":"2018-09-22T17:24:41+00:00","dateModified":"2024-03-29T15:48:44+00:00","description":"Learn to sum a range inside an Excel worksheet using VBA code. Modify the code and return the value in an active cell. Examples included.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/sum-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#primaryimage","url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png","contentUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/09\/sum-function-example.png"},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/sum-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Sum Function in VBA"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/3086"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=3086"}],"version-history":[{"count":6,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/3086\/revisions"}],"predecessor-version":[{"id":12682,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/3086\/revisions\/12682"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=3086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=3086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=3086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}