Saturday, 6 February 2021

How to Insert Rows or Columns In Existing Data in Excel


In this blog post, you will learn about inserting rows or columns within the existing dataset.

Suppose we are working on a dataset and mistakenly we miss some data to enter. Now want to insert it within the existing dataset.

So for that, we will use the following ways to insert the data within the existing dataset. 


How to Insert Columns within the Existing Dataset


Here we are working on the dataset where we have a missing column between “Mar-21” and “May-21”.


See (Image-01) for example.

Now we need to insert one more column between Mar-21 and May-21, as Apr-21, to adjust our missing dataset.

Excel has a basic rule for inserting - new Rows, Columns, or Sheets always will be inserted just before the current or active cell or sheet.

So first of all, make our active cell where we want to insert a new column.


Here we want to insert a new column just before column May-21.



Image -01



This means we will put our cursor in just column “D” to insert a new column before it.

Put your cursor in the first cell of column “D”.


Go to the Home tab then the ‘Cells’ group at the extreme right side of the Ribbon.



Image-02


See the image above for help.


Click on the “Insert” drop-down, there is 4 option available here.


  1. Insert Cells
  2. Insert Sheet Rows
  3. Insert Sheet Column
  4. Insert Sheet

You have to choose from these options, as per your requirements.


For inserting a new column within the dataset, select one option from the above list.


So choose option “3 – Insert Sheet Columns”.

Click on this option, to insert a new column here before our active cell.


Here active cell column is D2, a new column will be inserted here, and the existing dataset pushed one column back.


See the image below:-



Image-03


A new column has been inserted.


And now, assign the name as Apr-21 to our newly inserted column and put our new data here.

See in the below dataset, our missing data is adjusted and looks like a complete dataset now.


Image -04



Insert New Rows within Existing Dataset.


In the same way, we can insert a new “Rows” within the existing dataset and adjust our missing dataset.

Sometimes we need to insert multiple rows and columns to adjust our missing data within the existing dataset.

Here we need to select multiple rows or columns, as per our requirement.

See the image below: -




Image -05




Follow the procedure below after selecting rows.

Go to the “Home” tab and go to the “Cells” group and click “Insert” drop-down and select one of the options from the below option:-


    1. Insert Cells
    2. Insert Sheet Rows    
    3. Insert Sheet Column
    4. Insert Sheet


Here we will select option “2 – Insert Sheet Rows” and Click on it.

New empty rows will be inserted, in place of selected rows.


See the image below:-



Image -06




Required blank rows inserted and here we can adjust our missing data.


So, here we learn to insert rows or columns within the existing dataset.

I hope you find this tutorial useful.

Please feel free to put your comments or suggestion in the given box below.



Thank you

Narendra Singh


Related Post


Easiest Way to Copy and Paste Data in Excel VBA

How to Change the Background Color in Excel VBA

How to use the MsgBox in Excel VBA

What is Current Region Property in VBA

How to Change or Rename the Sheet Name in Excel VBA


Monday, 25 January 2021

How to use ‘With Statements’ in VBA Macro

In this blog post, we will learn about ‘With Statements’ in VBA macro.

Use With Statement, when we need to apply multiple formatting in a single range of the dataset.

Suppose that we have a dataset and we need to apply many formatting by VBA in the dataset.

So we will use the following VBA code to change the following dataset formatting.

Here we will use the below dataset to apply multiple formatting:-



Image-01


In the above dataset, we will apply the following formatting.


We want to change the below formatting:-

-    To change the font size to 16

-    To make the font Bold

-    A font name as Times New Roman

-    Interior color as Green

-    Font color as Yellow

-    Cell border as Continuous


See the below code for the above formatting.


Sub Range_With_Statements()

Range("A1:A10").Font.Bold = True

Range("A1:A10").Font.Size = 16

Range("A1:A10").Font.Name = "Times New Roman"

Range("A1:A10").Interior.Color = vbYellow

Range("A1:A10").Font.Color = vbGreen

Range("A1:A10").Borders.LineStyle = xlContinuous

End Sub


See the image below for the above coding:-






Here in this VBA macro, we are writing a single line for single formatting.

Also notice that we are using the same range (“A1:A10”) for every formatting.

In this situation, we apply the ‘With Statements’ to change the formatting.

‘With Statements’ reduce the length of the code because this will use a range (“A1:A10”) only a single time.


Now we will change the above formatting with the help of ‘With Statements’.

See the below macro to change formatting with ‘With Statement’.

Now we will apply the same formatting with the help of “With Statement”.


See below macro code:-


Sub With_Statements()

            With Range("A1:A10")

                        .Font.Bold = True

                        .Font.Size = 16

                        .Font.Name = "Times New Roman"

                        .Interior.Color = vbYellow

                        .Font.Color = vbGreen

                        .Borders.LineStyle = xlContinuous

            End With

End Sub


See the image below for the above code:-




Here you can see that we are using range (“A1:A10”) in a single time and the rest of the macro coding will remain the same.

Now run the above macro by “F5” or “F8” by step by step execution.

Here you saw that how “With Statement” can reduce the length of macro coding.


Now, I hope that you understand the concept of ‘With Statements’ in VBA macro.

I hope you find this tutorial useful.

Please feel free to put your comments or suggestion in the below-given box.


Thank You

Narendra


Related Post


Easiest Way to Copy and Paste Data in Excel VBA

How to use the MsgBox in Excel VBA

What is Current Region Property in VBA

How to Change or Rename the Sheet Name in Excel VBA

How to Change Sheet Tab Color in Excel VBA

How to Change Font Color in Excel VBA

How to Change the Background Color in Excel VBA

Monday, 18 January 2021

How to Delete Rows and Columns in VBA Macro

 

In this blog post, you will learn about how to delete rows and columns in VBA macro.

Sometimes, we face the situation when we need to delete some unwanted rows or columns as per our dataset required.

In Excel, we delete the unnecessary rows and columns by selecting the whole rows or whole columns and right-click from the mouse.

Then choose the delete option from the Options pop-up and delete the unnecessary rows and columns.

Suppose we have a dataset (see below image), there is a blank row and a column in it. 

Now we will delete the unwanted empty rows and columns with the help of the VBA macro.



Image-01



How to Delete Unwanted Rows By VBA Macro


Here in the above image, row (4) is blank and unwanted and need to delete from it.

We will use this row number in the VBA macro code.  

We will use the following VBA macro to remove or delete row (4) from the above dataset.

 

Sub Delete_Rows()

Range("4:4").EntireRow.Delete

'this code will delete row (4)

End Sub



See the image below:-



Image-02


Now execute the above code by pressing F5 or F8 for step by step execution.

Now suppose that we have to delete multiple rows and columns from the dataset then we will use the following VBA macro code:-

Here rows from 4 to 6 are blank and unwanted, which we need to delete or remove.


Sub Delete_Rows()

Range("4:6").EntireRow.Delete

'this code will delete row “4”

End Sub


How to Delete Unwanted Columns By VBA Macro


Now the same way when we want to delete the unwanted column from the dataset, then we will use the below given VBA code:-


Sub Delete_Columns()

Range("C:C").EntireColumn.Delete

'this code will delete the unwanted column

End Sub


See the image below:-


Now run the above VBA macro to delete the unwanted column by pressing F5 or F8 for step by step execution.



Image-03

Once we execute the above VBA code, column (C) will be deleted.

Suppose that we want to delete multiple columns within the dataset, then we need to use the below-given code:-


Sub Delete_Columns()

Range("C:E").EntireColumn.Delete

'this code will delete the unwanted columns

End Sub


When we execute this code by pressing F5 or F8 (for step by step execution), the mentioned columns will be removed.

So I hope you find this tutorial useful.

Please feel free to put your comments or suggestion in the below-given box.


Thanks

Narendra


Related Post For Beginners


Easiest Way to Copy and Paste Data in Excel VBA

How to use the MsgBox in Excel VBA

How to Change the Background Color in Excel VBA

What is Current Region Property in VBA

How to Change Font Color in Excel VBA

How to Change Sheet Tab Color in Excel VBA


Saturday, 16 January 2021

How to Insert Rows and Columns in Excel VBA


In this blog post, you will learn about inserting rows and columns in Excel with the help of VBA.


Inserting Rows and Columns in Excel


Basically, when we insert rows and columns in Excel, we follow the below procedure.

Select the cell or row or column, before which we want to insert a new Row or a Column.

Press Right-click on the cell and select the “Insert” option from the right-click properties and then Excel asks whether to insert rows or columns.


If we want to insert rows then we select rows or if columns then we will select the column option.

But here in this blog post, we are going to learn about inserting rows and columns with the help of VBA.

How to Insert Columns in Excel By VBA


See in the below image, we have a dataset and we are missing one column here which we want to name as “North” and enter the value under this column that belongs to it.

See below:



Image -01

Here, you can see that we have data for the zones, East, West, and South and now we want to insert a new column for zone name "North". Which we will insert with the help of VBA.

So for inserting a new column we will use the following steps in VBA.

Go to the Developer tab in Excel and click “Visual Basic”, click on “Insert” and Click on “Module” for writing a VBA code.

As per the rules, Excel will always insert rows or columns before the current or active column or rows.


Here we want to insert a new column before “C” titled “South”.

 

Sub Insert_New_Columns()

Range("C:C").Insert

'this code will insert a new column

End Sub







Just execute the above code by pressing “F5” for a single time execution or press “F8” again and again for step by step execution.


See the result below:




See in the above image, a new column inserted, column “C” and pushed our dataset "South" just one column next, i.e. in column “D”.


Here, we inserted a single column but suppose we need to insert multiple columns here, then our VBA code for that will be as follows:


Sub Inset_New_Column()

Range("C:E").Insert

'this will insert 3 (C,D,E) new columns

End Sub


After executing the code, see the image below:




Three columns("C", "D"and "E")  have been inserted within the dataset.


How to Insert Rows Within Dataset by Excel VBA


Now we will insert the rows within the given dataset.

Suppose that we have a dataset where we need to insert single or multiple rows within the dataset, see the code below for single-row insert:


Sub Insert_New_Rows()

Range("4:4").Insert

'this code will insert new row "4".

End Sub

 

See the result after executing the above code:




This code is made for inserting multiple rows such as 3 or 5 within the dataset. 

See below the VBA code for the above-mentioned result:


Sub Insert_New_Rows()

Range("4:6").Insert

'this code will insert new row "4 to 6".

End Sub


See the result below after executing this VBA code.




So we learned here to insert rows and columns in Excel with the help of VBA.

I hope you find this tutorial helpful.

Please feel free to put your comments and suggestion.


Thanks

Narendra


How to use the MsgBox in Excel VBA

Easiest Way to Copy and Paste Data in Excel VBA


How to Change the Background Color in Excel VBA


What is Current Region Property in VBA


What is Current Region Property in VBA


How to Change Font Color in Excel VBA



Featured post

Pivot Tables

Pivot Tables are one of the most useful tool in excel, and most excel experts says that this tool is the magic tool for quickly prepar...