473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Excel: How do I Delete Rows on Two Conditions

I am trying to delete rows that contain ZDEW in column H and do not start with W25G1 in column A. I used an asterik to show other letters/numbers may follow W25G1, but it's deleting all the ZDEW rows, including the ones that start with W25G1. Here is what I have. Where did I go wrong?

Expand|Select|Wrap|Line Numbers
  1. Sub DeleteZDEWs()
  2. Dim iLastRow As Long
  3.     Dim i As Long
  4.     iLastRow = Cells(Rows.Count, "H").End(xlUp).Row
  5. For i = iLastRow To 2 Step -1
  6.         If Cells(i, "H").Value = "ZDEW" And Cells(i, "A").Value <> "W25G1*" Then
  7.     Rows(i).Delete
  8.     End If
  9. Next i
  10.     'deletes row when cell in Column H is a ZDEW and cell in Column A does not start with W25G1
  11. End Sub
May 8 '12 #1

✓ answered by NeoPa

First off, line #4 is unreliable. It should say :
Expand|Select|Wrap|Line Numbers
  1. iLastRow = Range("A1").SpecialCells(xlLastCell).Row
Lines #6 & #7 could be redone in either of two ways as :
  1. Expand|Select|Wrap|Line Numbers
    1. If (Cells(i, "H") = "ZDEW") _
    2. And (Left(Cells(i, "A"), 5) <> "W25G1") Then Call Rows(i).Delete
  2. Expand|Select|Wrap|Line Numbers
    1. If (Cells(i, "H") = "ZDEW") _
    2. And (Cells(i, "A") Not Like "W25G1*") Then Call Rows(i).Delete

4 2076
TheSmileyCoder
2,322 Expert Mod 2GB
The asterisk cannot be used as a joker in that context. Instead you could use the leftfunction, that should suit your needs just fine.
May 8 '12 #2
NeoPa
32,556 Expert Mod 16PB
First off, line #4 is unreliable. It should say :
Expand|Select|Wrap|Line Numbers
  1. iLastRow = Range("A1").SpecialCells(xlLastCell).Row
Lines #6 & #7 could be redone in either of two ways as :
  1. Expand|Select|Wrap|Line Numbers
    1. If (Cells(i, "H") = "ZDEW") _
    2. And (Left(Cells(i, "A"), 5) <> "W25G1") Then Call Rows(i).Delete
  2. Expand|Select|Wrap|Line Numbers
    1. If (Cells(i, "H") = "ZDEW") _
    2. And (Cells(i, "A") Not Like "W25G1*") Then Call Rows(i).Delete
May 9 '12 #3
Thank ya'll so much! I am very new at this and don't know how to insert a function, so I couldn't test the Left function suggestion. However, the first option to fix lines 6 & 7 worked! (Also, I don't know how to add code tags.) A million thank yous!
May 9 '12 #4
NeoPa
32,556 Expert Mod 16PB
You're welcome Cloggy :-)

In fact, the first option, the one you chose, is an example of using the Left() function. It really is as easy as that.

Cloggy42:
(Also, I don't know how to add code tags.)
Please visit [code] Tags Must be Used and Before Posting (VBA or SQL) Code. They will fill you in on that and other important points on how to deal with code here.
May 9 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: rcamarda | last post by:
Hi, I need to delete rows from my user tables dependant upon there non existence from another table: delete student where student_id not in (select student_id from tblStudent) The reasons is...
11
by: suma | last post by:
Hello, I want to delete duplicate rows in a table when no primary key is defined. For eg: If we have table1 with data as below, Suma 23 100 Suma 23 100 I want to delete a row from this...
2
by: avanti | last post by:
Hi, I am trying to delete rows from a TableLayoutPanel at Runtime. I tried editing the RowStyles collection but doesn't do what I want it to. I have a table with 10 rows. I want to delete rows 2...
0
by: hafeez | last post by:
Hi, I have two mssql databases. I am able to create a linked server. I want to delete some of the rows using this linked server. Here HAFEEZ is one mssql database and DPVSQSL is another.So, i...
1
by: madhavi123 | last post by:
Hi All, I have tables in three hierarchy... I need to delete rows from the table which in 3rd level of hierarchy. For ex: Table 1 has primary key col1. Table 2 has primary key...
1
vikysaran
by: vikysaran | last post by:
Hi, i want to delete rows from two tables using one sql statement. One table is tblEmployee and other is tblUser and i want to delete all record in both of table whose UserID is EE00001
1
by: abdulkarim | last post by:
I have tried to delete rows in a table with same ID in access database by using the following vb code with a click on command button, but it deletes only a single row instead of entire rows with same...
3
by: lisles | last post by:
hey,i've got a page that displays rows from a db.im want a function to dynamically delete rows.i've put it 1,bt i get an error as follows: Parse error: syntax error, unexpected T_STRING, expecting...
0
by: aswaditya | last post by:
Hi, I am new to SSIS packages. I have 6 Excel workbooks with data spread over two worksheets each and i have to map individual data to individual SQL tables. Please note that each worksheet is...
1
by: Bikash Jena | last post by:
For Example: I have a table with 20 rows. I want to delete rows from 5 to 10 using query.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.