Bạn đang xem bài viết How To Delete Entire Row In Excel Using Vba (Examples) được cập nhật mới nhất trên website Hoisinhvienqnam.edu.vn. Hy vọng những thông tin mà chúng tôi đã chia sẻ là hữu ích với bạn. Nếu nội dung hay, ý nghĩa bạn hãy chia sẻ với bạn bè của mình và luôn theo dõi, ủng hộ chúng tôi để cập nhật những thông tin mới nhất.
Adding and deleting rows is part of everyday common tasks when working with Excel.
While you can do this easily from the worksheet itself, sometimes you may want to use the VBA route to delete rows in Excel. These could be deleting a specific row, multiple rows in the selection, deleting alternate rows or those that have a specific value in it.
In this tutorial, I will show you how to delete rows in Excel using VBA (multiple scenarios).
So let’s get started!
Delete an Entire Row using VBA
To delete an entire row in Excel using VBA, you need to use the EntireRow.Delete method.
For example, if you want to delete the entire first row in a worksheet, you can use the below code:
Sub DeleteEntireRow() Rows(1).EntireRow.Delete End SubThe above code first specifies the row that needs to be deleted (which is done by specifying the number in bracket) and then uses the EntireRow.Delete method to delete it.
You can also delete multiple rows by specifying these rows in the code.
For example, the below code will delete row number 1, 5 and 9 in the worksheet:
Sub DeleteEntireRow() Rows(9).EntireRow.Delete Rows(5).EntireRow.Delete Rows(1).EntireRow.Delete End SubThe above code uses the same logic, where it specifies the row numbers and Excel will delete these rows one by one.
IMPORTANT: When you’re deleting rows with something similar to the above code, remember to start deleting from the bottom and then go up. For example, in case you start at the top and delete row 1 first, all the rows below it would be shifted one row up and the numbering would be off (as row 5 would become row 4 and so on)
Delete All Rows in the Selection
In case you want to delete all the rows in a selected range of cells, you can use the VBA macro code below:
Sub DeleteEntireRow() Selection.EntireRow.Delete End SubThe above code applies to the EntireRow.Delete method to the entire selection.
Delete Alternate rows (or Delete Every Third/Fourth/Nth Row)
Sometimes, you may get a data dump where every second row (or third, fourth or Nth rows) is useless and needs to be deleted.
I used to work with financial data where every second row was empty and had to be deleted.
This is the type of scenario where VBA really shines.
Below is the VBA code that will go through all the rows in the selection and delete every second row:
Sub DeleteAlternateRows() RCount = Selection.Rows.Count For i = RCount To 1 Step -2 Selection.Rows(i).EntireRow.Delete Next i End SubLet me explain how this VBA code works.
First, I have used a variable RCount to get the total number of rows in the selection.
Then I have used a For Next loop to run this as many times as many rows are there. For example, if there are 12 rows, this loop will run from 12 to 1 (i.e., 12 times). It’s important to run this from the last row in the selection to the first as we don’t want the row numbers to change when a row is deleted.
Also, Step -2 is used since we need to delete every other row (from bottom to top). In case you want to delete every third row, you can use -3.
Within the VBA loop, I have used the Selection.Rows(i).EntireRow.Delete method to delete every alternate row.
Delete Blank Rows with VBA
You can also use the EntireRow.Delete method to delete all blank rows.
Below is the VBA code that will select blank cells in the selected dataset and delete the entire row.
Sub DeleteBlankRows() Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete End SubThe above code uses the SpecialCells property to select and delete all the cells that are blank. This is the same method that also allows us to use ‘Go To Special’ dialog box to select all blank cells.
Once these blank cells are identified using SpecialCell, these are then deleted using the EntireRow.Delete method.
Note: This method selects cells that are blank and don’t check whether the entire row is blank or not. So if anyone cell is empty in a row, this would still delete the entire row.
Delete Rows with a Specific Word/Value
You can also use a simple VBA code to go through each cell in the selected range and delete all the rows where a cell contains a specific text or value.
For example, suppose you have a dataset and I want to delete all cells that have the text Printer in column 2 of the selection.
Below is the code that will do this:
Sub DeleteRowswithSpecificValue() For i = Selection.Rows.Count To 1 Step -1 If Cells(i, 2).Value = "Printer" Then Cells(i, 2).EntireRow.Delete End If Next i End SubThe above code first counts the total number of rows in the selection. This will make sure the loop is run only these many times. It then uses the ‘For Next loop’ to go through all the cells in Column 2.
The IF THEN ELSE statement is then used to check the value in each cell in column 2. And in case the value/text matches the specified text (which is ‘Printer’ in this example).
In this example, I have checked whether the text matches a specific string or not. You can also do this with values. For example, you can delete all rows where the sale value is less than 1000 or more than 1000.
Note: An important thing to note here is that the loop runs from Selection.Rows.Count To 1 to make sure when a row is deleted, it doesn’t impact the rows above it.
How to Use This VBA Code
Now let me show you how to use all the codes mentioned in this tutorial to delete the entire row.
You need to copy and paste these codes in a module in Excel VB Editor. Once you have these codes copied, you can then run the macro codes.
Below are the steps to copy and paste these VBA codes in a module:
Hold the ALT key and press the F11 key (or Function + Option + F11 in Mac). This will open the VB Editor
Copy and Paste the above codes in the module.
I have also written a detailed tutorial on different ways to run VBA macro codes in Excel.
So these were some VBA codes that you can use to delete entire rows in Excel (in different scenarios). The same logic can also be applied in case you want to delete columns instead of rows (with the corresponding adjustment in the code examples).
Hope you found this tutorial useful!
You may also like the following Excel tutorials:
How To Quickly Delete All Autoshapes In Excel?
How to quickly delete all autoshapes in Excel?
Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.
Delete all autoshapes in active worksheet with Go To function in Excel
If there are only one kind of objects-autoshapes in a worksheet, you can delete all autoshapes with Go To function.
Note: Objects contain autoshapes, pictures, clip arts and so on. Applying this method will remove other objects besides auto shapes if there are not only autoshapes object in the worksheet.
Delete all autoshapes in active worksheet with VBA
Using VBA macro can help you delete all auto shapes in active worksheet quickly.
Step 1: Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
VBA: delete all shapes in active worksheet.
Step 3: Press the F5 key to run this macro.
Then you will see all shapes in active worksheet are deleted quickly.
Notes:
1. This VBA macro can only delete all shapes in active worksheet.
2. This VBA macro can delete all kinds of shapes in active worksheet, including pictures, ClipArt, Shapes, SmartArt, charts, Text boxes, etc.
Delete all autoshapes in worksheet or workbook with Kutools for Excel
Step 2: In Delete Tools dialog box, please check Auto shapes option, and check one of option in look in section. See the following screen shot:
If you check the Active sheet option, it will delete all auto shapes in active worksheet;
If you check the Selected sheets option, it will delete all auto shapes in selected worksheets;
If you check the All sheets option, it will delete all auto shapes in the whole workbook.
Relative articles:
Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails…
More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
Open and create multiple documents in new tabs of the same window, rather than in new windows.
How To Add And Delete A Watermark In Excel
The watermark is an image used in the background of the worksheet labeled as copy, draft, confidential or a picture of the logo. The Excel 2010 and later versions do not have built-in watermarks, therefore we need to learn how to add and delete a watermark using the Header & Footer tools.
Figure 1. Watermark
How to Add a Watermark
First of all, we need to save an image on our computer which we want to use as watermark. Alternately, we can create a watermark by using the WordArt or Microsoft Paint tools and save it as an image on the computer. We need to follow the below steps to add watermark to worksheet.
Go to the
View
tab and from
Workbook Views
group select the
Page Layout
.
Figure 3. Header & Footer Tools
From the
Header & Footer Elements
group, select the
Picture
element and browse the picture where we have saved it on the computer. Select the picture and press the
Insert
button
Figure 4. Insert the Watermark Picture
The Header box of worksheet appears with text
&[Picture]
to indicate that the header contains a picture now.
Figure 5. Header Box Containing Picture Text
Figure 6. Insert Watermark
Figure 7. Formatting the Watermark Picture
How to Delete a Watermark
To remove watermark we need to follow the below steps;
Select the text
&[Picture]
and press the Delete button
Figure 8. How to Remove Watermark
Instant Connection to an Expert through our Excelchat Service
Most of the time, the problem you will need to solve will be more complex than a simple application of a formula or function. If you want to save hours of research and frustration, try our live Excelchat service! Our Excel Experts are available 24/7 to answer any Excel question you may have. We guarantee a connection within 30 seconds and a customized solution within 20 minutes.
How To Hide And Unhide Rows In Excel
The tutorial shows three different ways to hide rows in your worksheets. It also explains how to show hidden rows in Excel and how to copy only visible rows.
If you want to prevent users from wandering into parts of a worksheet you don’t want them to see, then hide such rows from their view. This technique is often used to conceal sensitive data or formulas, but you may also wish to hide unused or unimportant areas to keep your users focused on relevant information.
On the other hand, when updating your own sheets or exploring inherited workbooks, you would certainly want to unhide all rows and columns to view all data and understand the dependencies. This article will teach you both options.
How to hide rows in Excel
Anyway, you begin with selecting the rows you’d like to hide:
To select multiple contiguous rows, drag across the row headings using the mouse. Or select the first row and hold down the Shift key while selecting the last row.
With the rows selected, proceed with one of the following options.
Hide rows using the ribbon
If you enjoy working with the ribbon, you can hide rows in this way:
Under Visibility, point to Hide & Unhide, and then select Hide Rows.
Either way, the selected rows will be hidden from view straight away.
If you’d rather not take your hands off the keyboard, you can quickly hide the selected row(s) by pressing this shortcut: Ctrl + 9
How to unhide rows in Excel
As with hiding rows, Microsoft Excel provides a few different ways to unhide them. Which one to use is a matter of your personal preference. What makes the difference is the area you select to instruct Excel to unhide all hidden rows, only specific rows, or the first row in a sheet.
Unhide rows by using the ribbon
Here is the Excel Unhide Rows shortcut: Ctrl + Shift + 9
Pressing this key combination (3 keys simultaneously) displays any hidden rows that intersect the selection.
In order to unhide all rows on a sheet, you need to select all rows. For this, you can either:
Press the Select All shortcut: Ctrl + A
Please note that in Microsoft Excel, this shortcut behaves differently in different situations. If the cursor is in an empty cell, the whole worksheet is selected. But if the cursor is in one of contiguous cells with data, only that group of cells is selected; to select all cells, press Ctrl+A one more time.
Once the entire sheet is selected, you can unhide all rows by doing one of the following:
Press Ctrl + Shift + 9 (the fastest way).
How to unhide all cells in Excel
To unhide all rows and columns, select the whole sheet as explained above, and then press Ctrl + Shift + 9 to show hidden rows and Ctrl + Shift + 0 to show hidden columns.
How to unhide specific rows in Excel
Depending on which rows you want to unhide, select them as described below, and then apply one of the unhide options discussed above.
To show one or several adjacent rows, select the row above and below the row(s) that you want to unhide.
To unhide multiple non-adjacent rows, select all the rows between the first and last visible rows in the group.
For example, to unhide rows 3, 7, and 9, you select rows 2 – 10, and then use the ribbon, context menu or keyboard shortcut to unhide them.
How to unhide top rows in Excel
Hiding the first row in Excel is easy, you treat it just like any other row on a sheet. But when one or more top rows are hidden, how do you make them visible again, given that there is nothing above to select?
The clue is to select cell A1. For this, just type A1 in the Name Box, and press Enter.
Tips and tricks for hiding and unhiding rows in Excel
As you have just seen, hiding and showing rows in Excel is quick and straightforward. In some situations, however, even a simple task can become a challenge. Below you will find easy solutions to a few tricky problems.
How to hide rows containing blank cells
To hide rows that contain any blank cells, proceed with these steps:
Select the range that contains empty cells you want to hide.
Press Ctrl + 9 to hide the corresponding rows.
This method works well when you want to hide all rows that contain at least one blank cell, as shown in the screenshot below:
If you want to hide blank rows in Excel, i.e. the rows where all cells are blank, then use the COUNTBLANK formula explained in How to remove blank rows to identify such rows.
How to hide rows based on cell value
To hide and show rows based on a cell value in one or more columns, use the capabilities of Excel Filter. It provides a handful of predefined filters for text, numbers and dates as well as an ability to configure a custom filter with your own criteria (please follow the above link for full details).
To unhide filtered rows, you remove filter from a specific column or clear all filters in a sheet, as explained here.
Hide unused rows so that only working area is visible
In situations when you have a small working area on the sheet and a whole lot of unnecessary blank rows and columns, you can hide unused rows in this way:
Press Ctrl + Shift + Down arrow to extend the selection to the bottom of the sheet.
Press Ctrl + 9 to hide the selected rows.
In a similar fashion, you hide unused columns:
Select an empty column that comes after the last column with data.
Press Ctrl + Shift + Right arrow to select all other unused columns to the end of the sheet.
Press Ctrl + 0 to hide the selected columns. Done!
If you decide to unhide all cells later, select the entire sheet, then press Ctrl + Shift + 9 to unhide all rows and Ctrl + Shift + 0 to unhide all columns.
If your worksheet contains hundreds or thousands of rows, it can be hard to detect hidden ones. The following trick makes the job easy.
This will select all visible cells and mark the rows adjacent to hidden rows with a white border:
How to copy visible rows in Excel
Supposing you have hidden a few irrelevant rows, and now you want to copy the relevant data to another sheet or workbook. How would you go about it? Select the visible rows with the mouse and press Ctrl + C to copy them? But that would also copy the hidden rows!
To copy only visible rows in Excel, you’ll have to go about it differently:
Select visible rows using the mouse.
Press Ctrl + C to copy the selected rows.
Press Ctrl + V to paste the visible rows.
Cannot unhide rows in Excel
If you have troubles unhiding rows in your worksheets, it’s most likely because of one of the following reasons.
1. The worksheet is protected
Whenever the Hide and Unhide features are disabled (greyed out) in your Excel, the first thing to check is worksheet protection.
2. Row height is small, but not zero
In case the worksheet is not protected but specific rows still cannot be unhidden, check the height of those rows. The point is that if a row height is set to some small value, between 0.08 and 1, the row seems to be hidden but actually it is not. Such rows cannot be unhidden in the usual way. You have to change the row height to bring them back.
To have it done, perform these steps:
Select a group of rows, including a row above and a row below the problematic row(s).
This will make all hidden rows visible again.
If the row height is set to 0.07 or less, such rows can be unhidden normally, without the above manipulations.
3. Trouble unhiding the first row in Excel
If someone has hidden the first row in a sheet, you may have problems getting it back because you cannot select the row before it. In this case, select cell A1 as explained in How to unhide top rows in Excel and then unhide the row as usual, for example by pressing Ctrl + Shift + 9.
If none of the above tips has worked for you, there is a chance that the hidden rows are a result of filtering. In this case, clear the filters, as explained in How to remove filter in Excel.
This is how you hide and undie rows in Excel. I thank you for reading and hope to see you on our blog next week!
You may also be interested in
Cập nhật thông tin chi tiết về How To Delete Entire Row In Excel Using Vba (Examples) trên website Hoisinhvienqnam.edu.vn. Hy vọng nội dung bài viết sẽ đáp ứng được nhu cầu của bạn, chúng tôi sẽ thường xuyên cập nhật mới nội dung để bạn nhận được thông tin nhanh chóng và chính xác nhất. Chúc bạn một ngày tốt lành!