Solved

How to delete cells that begin with using VBA?

Posted on 2014-12-10
6
94 Views
Last Modified: 2014-12-10
Hi,

Is it possible in VBA to delete all the cells that begin with a certain letter/word/number? I use the following but it works more like if the whole cell contains the values, another problem I have is that if i have a cell that begins with a date for example "12/12/2014 text" the below code would not work.

Set rngH = wst.Range("H1:H" & wst.Range("H65536").End(xlUp).Row)

For Each rng In rngH
    bnlFoundInvalid = False
    bnlFoundValid = False
   For intCounter = 0 To 12
        If InStr(1, rng.Offset(0, intCounter).Value, "Set", vbTextCompare) > 0 Or _
            InStr(1, rng.Offset(0, intCounter).Value, "New", vbTextCompare) > 0 Or _
            InStr(1, rng.Offset(0, intCounter).Value, "1", vbTextCompare) > 0 Or _
            IsNumeric(rng.Offset(0, intCounter).Value) Then
            If bnlFoundInvalid = False Then
                'An invalid column.
                intColInValid = intCounter
            End If
            bnlFoundInvalid = True
            rng.Offset(0, intCounter).ClearContents
        Else
            '1st valid column.
            If bnlFoundValid = False Then
                intColValid = intCounter
            End If
            bnlFoundValid = True
        End If
        If bnlFoundInvalid = True Then
            If bnlFoundValid = True Then
                If intColValid > 0 Then
                    'Col H had an invalid value.
                    rng.Value = rng.Offset(0, intColValid).Value
                End If
            End If
        End If
    Next intCounter
Next rng

Open in new window

0
Comment
Question by:Legolas786
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40491152
Change

rng.Offset(0, intCounter).Value

in line 7 to

left(rng.Offset(0, intCounter).Value,len("Set"))

if you are looking for a general approach.
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40491227
Hi,

pls try

If Rng.Offset(0, intCounter).Value Like "Set*" Or _
    Rng.Offset(0, intCounter).Value Like "New*" Or _
    (Rng.Offset(0, intCounter).Value Like "1*" And _
    Not Rng.Offset(0, intCounter).Value Like "##/##/####*") Or _
    IsNumeric(Rng.Offset(0, intCounter).Value) Then

Open in new window

Regards
0
 

Author Comment

by:Legolas786
ID: 40491526
Rgonzo1971
Hi,

i ahve tried your coding but it doesnt seem to clear the cell, for example if a cell contains "the colour is red", in your code i have the following and it does not clear it?

rng.Offset(0, intCounter).Value Like "the*" Or _

Open in new window


also some times there might be a space before the word so for example " the colour is red" and i have tried

rng.Offset(0, intCounter).Value Like " the*" Or _

Open in new window


but again it does not work?

Phillip Burton

can you please provide the full line? as basing with what you have said the following wont work?

If InStr(1, left(rng.Offset(0, intCounter).Value,len("Set")), "Set", vbTextCompare) > 0 Or _

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40491555
then use the function trim

If Trim(Rng.Offset(0, intCounter).Value) Like "Set*" Or _
    Trim(Rng.Offset(0, intCounter).Value) Like "New*" Or _
    (Trim(Rng.Offset(0, intCounter).Value) Like "1*" And _
    Not Trim(Rng.Offset(0, intCounter).Value) Like "##/##/####*") Or _
    IsNumeric(Trim(Rng.Offset(0, intCounter).Value)) Then

You could also use LTrim ( to only delete the left-sided blanks )

Regards
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40491617
I think both of our suggestions will work - I think you need to post your spreadsheet.
0
 

Author Comment

by:Legolas786
ID: 40491660
Sorry it was my mistake, i was searching the wrong range - schoolboy error.  thanks guys!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question