+ Reply to Thread
Results 1 to 9 of 9

Clear contents of 2 cells

Hybrid View

  1. #1
    Registered User
    Join Date
    01-18-2012
    Location
    Los Angeles USA
    MS-Off Ver
    Excel Office 365
    Posts
    80

    Clear contents of 2 cells

    I am using the following code, and it works fine. It does what it is suppose to, if finds the row that has the value in the form control Act1 and deletes that whole row in the worksheet. Unfortunately, when the row gets deleted other data in other ranges on the worksheet also get deleted, I trying to avoid moving the list I want to delete from to a new sheet. What I would like to do instead of deleting the row is to find the row and clear the contents of the two columns, AF and AG, in the range and then sort the list, this will give me the effect I want, this range is for a dropdown list that I should have put somewhere else to begin with.

    Private Sub cmdDelete_Click()
    'declare the variables
        Dim findvalue As Range
                
        'check for values
        If Act1.Value = ""  Then
            MsgBox "There is no data to delete"
            Exit Sub
        End If
            'looks for Code and delete the row
            Set findvalue = Sheet3.Range("AF:AF").Find(What:=Act1, LookIn:=xlValues)
            findvalue.EntireRow.Delete
          
        'clear the controls
        For XP = 1 To 3
            Me.Controls("Act" & XP).Value = ""
        Next
        
        SortActivity
    End Sub
    Last edited by laguna92651; 03-24-2015 at 01:18 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,567

    Re: Clear contents of 2 cells

    You might want to try just changing .Delete to .ClearContents


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-18-2012
    Location
    Los Angeles USA
    MS-Off Ver
    Excel Office 365
    Posts
    80

    Re: Clear contents of 2 cells

    Thanks, that clears the contents for the whole role I only want to clear the contents for the entries in columns AF and AG.
    Al

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,567

    Re: Clear contents of 2 cells

        Range(Cells(findvalue.Row, "AF"), Cells(findvalue.Row, "AG")).ClearContents

  5. #5
    Registered User
    Join Date
    01-18-2012
    Location
    Los Angeles USA
    MS-Off Ver
    Excel Office 365
    Posts
    80

    Re: Clear contents of 2 cells

    Thanks so much, worked great. If I wanted to clear a wider range of cells and a non-adjacent cell, would this be the correct format?

    Range(Cells(findvalue.Row, "AF"): Cells(findvalue.Row, "AJ"),Cells(findvalue.Row, "AO")).ClearContents
    Thanks again,

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,567

    Re: Clear contents of 2 cells

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  7. #7
    Registered User
    Join Date
    01-18-2012
    Location
    Los Angeles USA
    MS-Off Ver
    Excel Office 365
    Posts
    80

    Re: Clear contents of 2 cells

    Thanks for the help, much appreciated.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,567

    Re: Clear contents of 2 cells

    No, you can't do that. Range expect a start cell and an end cell. There are different ways of providing that; in this case, two Cells() separated by a comma.

    You could use Union to build up a combined range, for example:

    ?Union(Cells(1, "AF"), Range(Cells(1, "AJ"),Cells(1, "AO"))).address
    $AF$1,$AJ$1:$AO$1

    Regards, TMS

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,567

    Re: Clear contents of 2 cells

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Clear contents of cells when another cell changes value
    By chrisd79 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2013, 05:22 AM
  2. Clear contents of cells that do not contain specific text, sort cells that do
    By feckless.lout in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-27-2010, 01:41 AM
  3. Clear contents of cells
    By talytech in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2007, 03:08 PM
  4. clear cells unless contents are in bold
    By John Jones in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-24-2005, 09:05 AM
  5. [SOLVED] Clear Contents - NonBold cells
    By Steve in forum Excel General
    Replies: 3
    Last Post: 02-13-2005, 08:06 PM

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