How would I write a macro so that when it is checking the cells data, it will be able to read that the cell has no data and will delete the entire row?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Reading Blank Data (English)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Reading Blank Data (English)
- This topic has 20 replies, 5 voices, and was last updated 23 years, 6 months ago.
Viewing 0 reply threadsAuthorReplies-
WSWassim
AskWoody LoungerOctober 24, 2001 at 4:31 pm #548404Ximon
Would that help? This would look for cells in Column A all the way down to the last row of your worksheet and if that cell is empty then it deletes the row.
Sub DeleteBlankRows()
Dim lCounter As LongWith ActiveSheet
For lCounter = 2 To .Rows.Count
If Len(CStr(Range(“A” & lCounter).Value)) = 0 Then
.Rows(lCounter).Select
‘/Cell is blanck so delete row.
.Rows(lCounter).Delete Shift:=xlUp
End If
Next lCounter
End With
End SubWassim
-
WSXimon
AskWoody Lounger -
WSXimon
AskWoody LoungerOctober 24, 2001 at 5:01 pm #548412Hi Wassim,
I tried the macro you wrote for me and it works very good, but there is one thing. When I delete the row I notice that the data from the previous row has moved up a row and so I don’t think I should be deleting entire rows. How would I only delete the data that is in the cells of colume B and C of the selected rowusing your example?
-
H. Legare Coleman
AskWoody PlusOctober 24, 2001 at 5:20 pm #548417I am not clear on what you want to do.
It sounds like you want to delete the data from the cell but if there is no data in the cell, there is nothing there to delete.
If a cell in Column B is empty, do you want to delete the cell and move the rest of the cells in column B up without affecting data that might be in other columns? If the cell in B is empty, do you want to delete the cell in column C even if it contains data, or do you want to only delete cells in column C if they are empty. What do you want to do if one of the cells is empty but the other is not?
-
WSXimon
AskWoody LoungerOctober 24, 2001 at 5:33 pm #548420 -
WSAndrew Cronnolly
AskWoody Lounger -
H. Legare Coleman
AskWoody PlusOctober 24, 2001 at 5:45 pm #548424Ok, I think I am beginning to understand what you want to do. However, one more question before we proceed. Are you really just trying to keep a zero or error from displaying in columns A and B if C is empty, or do you really need to delete the formulas? You can keep the zero or error from displaying by using a simple formula, and then you don’t have to reenter the formula if you later enter something in C. If the formula in A is =1/C1, which will produce an error is C is empty, then you can change the formula to:
=IF(C1="","",1/C1)
-
WSXimon
AskWoody Lounger -
WSJohnBF
AskWoody Lounger -
WSXimon
AskWoody Lounger -
WSJohnBF
AskWoody LoungerOctober 26, 2001 at 9:00 pm #548852You would use similar construction, except you have to check on the #N/A status cell by cell:
For Each rngCell In Selection.SpecialCells(xlCellTypeFormulas, xlErrors)
If rngCell.Value = CVErr(xlErrNA) ThenEnd If
Next rngCellDo you want a routine that only looks for functions that evaluate to #N/A and deletes them, or do you want a routine that includes the deletion of cells in columns A & B routine we already have, and if so do you want the #N/A formula cell in column C also deleted?
-
H. Legare Coleman
AskWoody Plus -
WSWassim
AskWoody LoungerOctober 24, 2001 at 6:25 pm #548436Ximon
There is a better solution, faster and easier:
OK the formula you would use is like this: =IF(C1=””,1/0,C1) You would fill this formula down to all the rows with data.
Now this formula will produce an error (#DIV/0!) where cell in C is empty.
Then you would need to delete these formulas:
Select columns A and B and Press F5 to activate the GoTo
Then select special
From the dialog select Formulas and uncheck all but Errors.This will select all the cells with errors and then you simply hit the delete key and you are done.
Now if you need to do this many times, you would need to possibly automate it.
As Legare had mentioned, my code will miss some rows, so you would need to run it twice or even more. The use of a For-Next loop in this case is not the best way of doing things.
OK hope all of us have given you some starting points to get to your optimal solution.
Wassim
-
WSXimon
AskWoody Lounger
-
-
-
WSWassim
AskWoody LoungerOctober 24, 2001 at 6:03 pm #548429Ximon
Try this version then:
Sub DeleteBlankCells()
Dim lCounter As LongWith ActiveSheet
For lCounter = 2 To .Rows.Count
If Len(CStr(Range(“A” & lCounter).Value)) = 0 Then
‘/Cell is blanck so delete row.
.Range(“B” & lCounter).Resize(1, 2).Delete Shift:=xlUp
End If
Next lCounter
End With
End SubI also used the resize option here just to give you an example of how you can resize a range that you pick. But I am afraid that anytime you delete rows like that, the data may become skewed, that is the rows don’t match. I don’t know what the data you are working with looks like, but I need to caution you on this issue.
Wassim
-
-
H. Legare Coleman
AskWoody Plus -
WSWassim
AskWoody LoungerOctober 24, 2001 at 6:13 pm #548432 -
H. Legare Coleman
AskWoody Plus
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Fedora Linux 40 will go end of life for updates and support on 2025-05-13.
by
Alex5723
12 minutes ago -
How a new type of AI is helping police skirt facial recognition bans
by
Alex5723
50 minutes ago -
Windows 7 ISO /Windows 10 ISO
by
ECWS
7 hours, 46 minutes ago -
No HP software folders
by
fpefpe
8 hours, 31 minutes ago -
Which antivirus apps and VPNs are the most secure in 2025?
by
B. Livingston
43 minutes ago -
Stay connected anywhere
by
Peter Deegan
13 hours, 54 minutes ago -
Copilot, under the table
by
Will Fastie
5 hours, 7 minutes ago -
The Windows experience
by
Will Fastie
20 hours, 8 minutes ago -
A tale of two operating systems
by
Susan Bradley
11 hours, 4 minutes ago -
Microsoft : Resolving Blue Screen errors in Windows
by
Alex5723
1 day, 1 hour ago -
Where’s the cache today?
by
Up2you2
1 day, 16 hours ago -
Ascension says recent data breach affects over 430,000 patients
by
Nibbled To Death By Ducks
1 day, 9 hours ago -
Nintendo Switch 2 has a remote killing switch
by
Alex5723
9 hours, 54 minutes ago -
Blocking Search (on task bar) from going to web
by
HenryW
1 day, 17 hours ago -
Windows 10: Microsoft 365 Apps will be supported up to Oct. 10 2028
by
Alex5723
2 days, 10 hours ago -
Add or Remove “Ask Copilot” Context Menu in Windows 11 and 10
by
Alex5723
2 days, 10 hours ago -
regarding april update and may update
by
heybengbeng
2 days, 11 hours ago -
MS Passkey
by
pmruzicka
1 day, 13 hours ago -
Can’t make Opera my default browser
by
bmeacham
2 days, 19 hours ago -
*Some settings are managed by your organization
by
rlowe44
2 days, 6 hours ago -
Formatting of “Forward”ed e-mails
by
Scott Mills
2 days, 18 hours ago -
SmartSwitch PC Updates will only be supported through the MS Store Going Forward
by
PL1
3 days, 13 hours ago -
CISA warns of hackers targeting critical oil infrastructure
by
Nibbled To Death By Ducks
3 days, 22 hours ago -
AI slop
by
Susan Bradley
1 day, 16 hours ago -
Chrome : Using AI with Enhanced Protection mode
by
Alex5723
4 days ago -
Two blank icons
by
CR2
1 day, 8 hours ago -
Documents, Pictures, Desktop on OneDrive in Windows 11
by
ThePhoenix
1 day, 10 hours ago -
End of 10
by
Alex5723
4 days, 11 hours ago -
Single account cannot access printer’s automatic duplex functionality
by
Bruce
3 days, 9 hours ago -
test post
by
gtd12345
4 days, 17 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.