×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Search for string in excel cells

Search for string in excel cells

Search for string in excel cells

(OP)
I'm trying to write a macro that will delete a column in excel if the title has a certain sequence of letters.  I can't seem to get the syntax right, or maybe im going about it in completely the wrong way as im an absolute novice when it comes to writing in VBA.  So far i've got:

Sub Test_Deleting()
    Dim Looking As Long
    Dim TotalColumns As Long
    Looking = 0
    TotalColumns = Application.WorksheetFunction.CountA(Range("1:1"))
    Do While Looking <> TotalColumns
        Looking = Looking + 1
        If Application.WorksheetFunction.IsError(Application.WorksheetFunction.Search("exp", Cells(1, Looking))) = False Then Selection.EntireColumn.Delete
    Loop
End Sub

I wasn't sure if there was a VBA function to search the active cell for a certain string, so i tried to use the excel functions.  I can get Excel to distinguish between those columns with the string in the title, and those without, but can't seem to get it to work in VBA.

Any help would be appreciated
Thanks SP

RE: Search for string in excel cells

Hello,

I think this should work

Sub DELETE_EXP()
For MY_COLS = Range("IV1").End(xlToLeft).Column To 1 Step -1
    CURRENT_CELL = Range("A1").Offset(0, MY_COLS - 1).Value
    If CURRENT_CELL Like "exp" Or CURRENT_CELL Like "*exp" Or CURRENT_CELL Like "exp*" Or CURRENT_CELL Like "*exp*" Then
        Columns(MY_COLS).Delete
    End If
Next MY_COLS
End Sub

Is it OK?

----------------------------------
Hope this helps.
----------------------------------

maybe only a drafter
but the best user at this company!

RE: Search for string in excel cells

(OP)
Cheers! With a little fiddling it now seems to work, but is there anyway to make it not case sensitive, because i found that it would delete "exp", but not "Exp".

Thanks again.

RE: Search for string in excel cells

Hello,

Can't think of a way immediately other than adding Or CURRENT_CELL Like "Exp" and all the other variants.

----------------------------------
Hope this helps.
----------------------------------

maybe only a drafter
but the best user at this company!

RE: Search for string in excel cells


Improved procedure:

Sub DELETE_EXP()
For MY_COLS = Range("IV1").End(xlToLeft).Column To 1 Step -1
    CURRENT_CELL = Lcase(Range("A1").Offset(0, MY_COLS - 1).Value)
    If CURRENT_CELL Like "exp" Or _
       CURRENT_CELL Like "*exp" Or _
       CURRENT_CELL Like "exp*" Or _
       CURRENT_CELL Like "*exp*" Then
          Columns(MY_COLS).Delete
    End If
Next MY_COLS
End Sub

by adding "Lcase()" to the procedure you will get all text in variable in Lover Case and you can compare it with with your strings. Text in cell is unchanged.


Vlado

RE: Search for string in excel cells

(OP)
Thanks Vlado!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources