{"id":340,"date":"2018-06-27T17:33:22","date_gmt":"2018-06-27T17:33:22","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=340"},"modified":"2024-03-29T23:56:42","modified_gmt":"2024-03-29T23:56:42","slug":"inserting-a-row-with-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/","title":{"rendered":"Inserting a Row With VBA"},"content":{"rendered":"\n

In the lesson about inserting rows and columns<\/a>, I presented how you can insert a new row in Excel. Here, I will show you how you can add a new row with VBA.<\/p>\n\n\n\n

Creating a macro<\/h2>\n\n\n\n

You don\u2019t have to remember the code which will allow you to insert a new row. In this example, we will use the macro recorder and cut the code that will be useful for us.<\/p>\n\n\n\n

First, click the button to record a macro<\/a>.<\/p>\n\n\n\n

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

Name your macro and click OK.<\/strong> Right-click any row number and choose Insert. Stop the macro.<\/p>\n\n\n\n

Executing the macro<\/h2>\n\n\n\n

Go to VIEW >> Macros >> Macros<\/strong> and open the macro window. Choose your macro and hit Run. As you can see a new row is inserted<\/a> into the worksheet.<\/p>\n\n\n\n

CAUTION<\/h3>\n\n\n\n

When you use a macro to insert a new row, you can\u2019t use undo (Ctrl + Z<\/span><\/strong>).<\/p>\n\n\n\n

Use Left Alt + F11<\/span><\/strong> to open the VBA code window. Double-click the module icon from Project Explorer (if you can\u2019t see it, use Ctrl + R<\/strong>).<\/p>\n\n\n\n

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

Analyzing the code<\/h2>\n\n\n\n

Take a look at the following code.<\/p>\n\n\n\n

Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove<\/pre>\n\n\n\n
    \n
  1. Selection.Insert Shift:=xlDown shifts down the data from the selected cell(s) and from the cells below.<\/li>\n\n\n\n
  2. CopyOrigin:=xlFormatFromLeftOrAbove uses the formatting from the cell above or from the cell to the left. xlFormatFromLeftOrAbove takes one of two values, 0 or 1. 0 is the default. So you can also write CopyOrigin:=0 or even drop the code. It will use the formatting from the cell above by default. If you want to get rid of the formatting use CopyOrigin:=1<\/li>\n<\/ol>\n\n\n\n

    This code will only insert a new row when you click the row number. If you use it on a cell It will shift down data inside this cell.<\/p>\n\n\n\n

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

    If you want the macro to work in a way that it will be adding a new row not only when you choose a row letter, but also any cell inside this row then you can use the following code.<\/p>\n\n\n\n

    ActiveCell.EntireRow.Insert<\/pre>\n\n\n\n

    If you want to insert a row above the selected cell, use offset.<\/p>\n\n\n\n

    ActiveCell.EntireRow.Offset(1, 0).Insert<\/pre>\n\n\n\n

    It will give you the following result.<\/p>\n\n\n\n

    \"\"<\/figure>\n","protected":false},"excerpt":{"rendered":"

    In the lesson about inserting rows and columns, I presented how you can insert a new row in Excel. Here, I will show you…<\/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":"\nInserting a Row With VBA<\/title>\n<meta name=\"description\" content=\"Learn how to add a new row in Excel with VBA code by recording a macro and analyzing its code. Use caution as undo won't work when using a macro.\" \/>\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\/inserting-a-row-with-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inserting a Row With VBA\" \/>\n<meta property=\"og:description\" content=\"Learn how to add a new row in Excel with VBA code by recording a macro and analyzing its code. Use caution as undo won't work when using a macro.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-27T17:33:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-29T23:56:42+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"Inserting a Row With VBA\",\"datePublished\":\"2018-06-27T17:33:22+00:00\",\"dateModified\":\"2024-03-29T23:56:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/\"},\"wordCount\":364,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png\",\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/\",\"name\":\"Inserting a Row With VBA\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png\",\"datePublished\":\"2018-06-27T17:33:22+00:00\",\"dateModified\":\"2024-03-29T23:56:42+00:00\",\"description\":\"Learn how to add a new row in Excel with VBA code by recording a macro and analyzing its code. Use caution as undo won't work when using a macro.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#primaryimage\",\"url\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png\",\"contentUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inserting a Row With 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":"Inserting a Row With VBA","description":"Learn how to add a new row in Excel with VBA code by recording a macro and analyzing its code. Use caution as undo won't work when using a macro.","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\/inserting-a-row-with-vba\/","og_locale":"en_US","og_type":"article","og_title":"Inserting a Row With VBA","og_description":"Learn how to add a new row in Excel with VBA code by recording a macro and analyzing its code. Use caution as undo won't work when using a macro.","og_url":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/","article_published_time":"2018-06-27T17:33:22+00:00","article_modified_time":"2024-03-29T23:56:42+00:00","og_image":[{"url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png"}],"author":"Tomasz Decker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tomasz Decker","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"Inserting a Row With VBA","datePublished":"2018-06-27T17:33:22+00:00","dateModified":"2024-03-29T23:56:42+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/"},"wordCount":364,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png","keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/","url":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/","name":"Inserting a Row With VBA","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png","datePublished":"2018-06-27T17:33:22+00:00","dateModified":"2024-03-29T23:56:42+00:00","description":"Learn how to add a new row in Excel with VBA code by recording a macro and analyzing its code. Use caution as undo won't work when using a macro.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#primaryimage","url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png","contentUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/06\/the-macro-recorder.png"},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/inserting-a-row-with-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Inserting a Row With 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\/340"}],"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=340"}],"version-history":[{"count":6,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"predecessor-version":[{"id":13083,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/340\/revisions\/13083"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}