+ Reply to Thread
Results 1 to 7 of 7

Delete all cell content in column except for desired string

Hybrid View

  1. #1
    Registered User
    Join Date
    01-16-2013
    Location
    Guatemala
    MS-Off Ver
    Excel 2007
    Posts
    8

    Delete all cell content in column except for desired string

    Hello all, having troubles here and can't find an answer to help in the forum.

    I have a code here that I want to cycle through all values in column E. If the value in column E is anything other than 'Volunteer & Internship' I want to clear the cell content. I have tried various codes, but here is the last. I keep getting a mismatch error for this one and don't know where the mismatch is. Maybe it is more than just that error too, not sure. This is what I have, anyone able to correct me?

    Sub Ministry_Info_Organization()
    
    Dim p As String
    
    Do Until Range("A:A").Value = ""
        
        p = sht.Range("E:E").String
        If p <> "Volunteer & Internship" Then
            cell.ClearContents
        End If
        
    Loop  
    
    End Sub

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Delete all cell content in column except for desired string

    Hi,

    try this:
    Option Explicit
    
    Sub Ministry_Info_Organization()
        Dim p As String
        Dim lngRow As Long
        With ActiveSheet
            For lngRow = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
                If .Cells(lngRow, 1).Value <> "Volunteer & Internship" Then .Cells(lngRow, 1).Value = vbNullString
            Next lngRow
        End With
    End Sub
    Regards
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    01-16-2013
    Location
    Guatemala
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Delete all cell content in column except for desired string

    Sorry, I don't really know if I understand what the code you offered is doing... It doesn't seem to specify anywhere that this should be done in Column E and that the cell content in column E only is to be cleared if it doesn't have 'Volunteer & Internship'. Could you please explain? Thanks.

  4. #4
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Delete all cell content in column except for desired string

    Hi,

    and that the cell content in column E only is to be cleared if it doesn't have 'Volunteer & Internship'
    thats done here:
    If .Cells(lngRow, 5).Value <> "Volunteer & Internship" Then .Cells(lngRow, 5).Value = vbNullString
    It doesn't seem to specify anywhere that this should be done in Column E
    That's my mistake, did it for column A, here is it for column E in the current activated worksheet
    The column is specified as the index number of the column, 5 for E
    Option Explicit
    
    Sub Ministry_Info_Organization()
        Dim lngRow As Long
        With ActiveSheet
            For lngRow = 1 To .Cells(.Rows.Count, 5).End(xlUp).Row
                If .Cells(lngRow, 5).Value <> "Volunteer & Internship" Then .Cells(lngRow, 5).Value = vbNullString
            Next lngRow
        End With
    End Sub
    it's running from row 1 till the last filled row in column E

    Regards

  5. #5
    Registered User
    Join Date
    01-16-2013
    Location
    Guatemala
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Delete all cell content in column except for desired string

    Thank you for explaining. I know understand what it is doing and got it to work!

  6. #6
    Registered User
    Join Date
    01-16-2013
    Location
    Guatemala
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Delete all cell content in column except for desired string

    Okay, so here is another question pertaining to this, because I can't seem to get it to work. Can you ask for it to look for two different strings 'volunteer & internship' or 'Adventure & Culture' and then clear any cells that do not have one or the other?

    I put the following and now it clears everything. Why?

    With ActiveSheet
        For lngRow = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
            If .Cells(lngRow, 5).Value <> "Volunteer & Internship" _
            Or .Cells(lngRow, 5).Value <> "Adventure & Culture" Then .Cells(lngRow, 5).Value = vbNullString
        Next lngRow
    End With

  7. #7
    Registered User
    Join Date
    01-16-2013
    Location
    Guatemala
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Delete all cell content in column except for desired string

    Scratch that, thought about it and put in 'and' instead and it solved the problem.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1