Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 149
Default Delete Column Containing String

I'm deleting a column if a cell contains the variable "sString" below. How
can I modify this sub so it will find the string in a cell even if it has
other characters in the cell besides "sString"? Basically, I need to check
if "sString" is in a cell, regardless of other words or spaces.

any help?

Sub DeleteColumnswString(ByVal sString As String)

Dim LastCol As Long
Dim r As Long
LastCol = Range("IV1").End(xlToLeft).Column
Application.ScreenUpdating = False
For r = LastCol To 1 Step -1
If Application.CountIf(Columns(r), sString) < 0 _
Then Columns(r).Delete
Next r
Application.ScreenUpdating = True

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Delete Column Containing String

Try this...

Sub DeleteRows(ByVal strToFind As String)
Dim rngFound As Range

Set rngFound = Cells.Find(strToFind, , , xlPart, , , False)
Do While Not rngFound Is Nothing
rngFound.EntireColumn.Delete
Set rngFound = Cells.Find(strToFind, , , xlPart, , , False)
Loop

End Sub

HTH

"scott" wrote:

I'm deleting a column if a cell contains the variable "sString" below. How
can I modify this sub so it will find the string in a cell even if it has
other characters in the cell besides "sString"? Basically, I need to check
if "sString" is in a cell, regardless of other words or spaces.

any help?

Sub DeleteColumnswString(ByVal sString As String)

Dim LastCol As Long
Dim r As Long
LastCol = Range("IV1").End(xlToLeft).Column
Application.ScreenUpdating = False
For r = LastCol To 1 Step -1
If Application.CountIf(Columns(r), sString) < 0 _
Then Columns(r).Delete
Next r
Application.ScreenUpdating = True

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Delete Column Containing String

if you want to delete the column if the sString is found anywhere in the
column: (even as a substring) then
Sub DeleteColumnswString(ByVal sString As String)

Dim LastCol As Long
Dim r As Long
LastCol = Range("IV1").End(xlToLeft).Column
Application.ScreenUpdating = False
For r = LastCol To 1 Step -1
If Application.CountIf(Columns(r), "*" & sString & "*") < 0 _
Then Columns(r).Delete
Next r
Application.ScreenUpdating = True

End Sub

If you want to delete the column if sString is found anywhere in the cell in
the first row of that column (even as a substring) then

Sub DeleteColumnswString(ByVal sString As String)

Dim LastCol As Long
Dim r As Long
LastCol = Range("IV1").End(xlToLeft).Column
Application.ScreenUpdating = False
For r = LastCol To 1 Step -1
If Application.CountIf(Cells(1,r), "*" & sString & "*") < 0 _
Then Columns(r).Delete
Next r
Application.ScreenUpdating = True

End Sub

--
Regards,
Tom Ogilvy


"scott" wrote in message
...
I'm deleting a column if a cell contains the variable "sString" below. How
can I modify this sub so it will find the string in a cell even if it has
other characters in the cell besides "sString"? Basically, I need to check
if "sString" is in a cell, regardless of other words or spaces.

any help?

Sub DeleteColumnswString(ByVal sString As String)

Dim LastCol As Long
Dim r As Long
LastCol = Range("IV1").End(xlToLeft).Column
Application.ScreenUpdating = False
For r = LastCol To 1 Step -1
If Application.CountIf(Columns(r), sString) < 0 _
Then Columns(r).Delete
Next r
Application.ScreenUpdating = True

End Sub




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Delete Column Containing String

using your method:

Public Sub DeleteColumnswString(ByVal sString As String)
Dim LastCol As Long
Dim r As Long
LastCol = Range("IV1").End(xlToLeft).Column
Application.ScreenUpdating = False
For r = LastCol To 1 Step -1
If Application.CountIf(Columns(r), "*" & sString & "*") < 0 _
Then Columns(r).Delete
Next r
Application.ScreenUpdating = True
End Sub



You might also look at VBA's Find method, using LookAt:=xlPart


In article ,
"scott" wrote:

I'm deleting a column if a cell contains the variable "sString" below. How
can I modify this sub so it will find the string in a cell even if it has
other characters in the cell besides "sString"? Basically, I need to check
if "sString" is in a cell, regardless of other words or spaces.

any help?

Sub DeleteColumnswString(ByVal sString As String)

Dim LastCol As Long
Dim r As Long
LastCol = Range("IV1").End(xlToLeft).Column
Application.ScreenUpdating = False
For r = LastCol To 1 Step -1
If Application.CountIf(Columns(r), sString) < 0 _
Then Columns(r).Delete
Next r
Application.ScreenUpdating = True

End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy column header to next column, delete & delete every nth colum genehunter New Users to Excel 1 June 2nd 09 03:57 PM
Delete a string Joe Gieder Excel Worksheet Functions 5 October 3rd 08 07:39 PM
DELETE ROWS FROM XLS IF THE ROW CONTAINS THE STRING [email protected] Excel Discussion (Misc queries) 1 September 30th 06 01:21 PM
Column Delete based on String scott Excel Programming 19 February 22nd 05 06:53 PM
Delete characters from a string Ron[_18_] Excel Programming 7 February 19th 04 04:24 PM


All times are GMT +1. The time now is 04:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"