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? - Sub DeleteZDEWs()
-
Dim iLastRow As Long
-
Dim i As Long
-
iLastRow = Cells(Rows.Count, "H").End(xlUp).Row
-
For i = iLastRow To 2 Step -1
-
If Cells(i, "H").Value = "ZDEW" And Cells(i, "A").Value <> "W25G1*" Then
-
Rows(i).Delete
-
End If
-
Next i
-
'deletes row when cell in Column H is a ZDEW and cell in Column A does not start with W25G1
-
End Sub
First off, line #4 is unreliable. It should say : - iLastRow = Range("A1").SpecialCells(xlLastCell).Row
Lines #6 & #7 could be redone in either of two ways as : -
- If (Cells(i, "H") = "ZDEW") _
-
And (Left(Cells(i, "A"), 5) <> "W25G1") Then Call Rows(i).Delete
-
- If (Cells(i, "H") = "ZDEW") _
-
And (Cells(i, "A") Not Like "W25G1*") Then Call Rows(i).Delete
4 2076
The asterisk cannot be used as a joker in that context. Instead you could use the left function, that should suit your needs just fine.
NeoPa 32,556
Expert Mod 16PB
First off, line #4 is unreliable. It should say : - iLastRow = Range("A1").SpecialCells(xlLastCell).Row
Lines #6 & #7 could be redone in either of two ways as : -
- If (Cells(i, "H") = "ZDEW") _
-
And (Left(Cells(i, "A"), 5) <> "W25G1") Then Call Rows(i).Delete
-
- If (Cells(i, "H") = "ZDEW") _
-
And (Cells(i, "A") Not Like "W25G1*") Then Call Rows(i).Delete
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!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |