+ Reply to Thread
Results 1 to 8 of 8

Deleting content from a range of cells

Hybrid View

  1. #1
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Post Deleting content from a range of cells

    Hi guys
    I need your support.

    i have this ceiling code, but if a select a range of cells to delete the content, the macro pop up an error.
    how to delete a range of a cells without having error from macro?
    Option Explicit
    Private Sub Worksheet_Change(ByVal target As Range)
    Dim cell As Range
    Dim r As Range
    'Stop
    ' cater for clearing entire column(s)
    If target.Rows.Count = Me.Rows.Count Then Exit Sub
    For Each r In target.Cells
    If Not (Application.Intersect(target, Range("C:D")) _
    Is Nothing) Then
        For Each cell In target
            With cell
                If Not .HasFormula Then
                    Application.EnableEvents = False
                    .Value = UCase(.Value)
                    Application.EnableEvents = True
                End If
            End With
        Next 'cell
    End If
       If Not (Application.Intersect(r, Range("E5:E10005")) Is Nothing) Then
                With r
                    If Not .HasFormula Then
                        Application.EnableEvents = False
                        .Value = "0:" & Application.Ceiling(Round(.Value * 60, 0), 15)
                        Application.EnableEvents = True
                    End If
                End With
            End If
        Next r
    
    
    End Sub

  2. #2
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50

    Re: Deleting content from a range of cells

    Hi, you have ' near your first Next, disabling the "cell". For each cell - - Next cell
    Could that be the problem?

  3. #3
    Registered User
    Join Date
    08-27-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Deleting content from a range of cells

    cell is commented out

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Deleting content from a range of cells

    Hi,

    Which line of code is highlighted when you get the Debug and what's the error message it returns?

    Be cautious when using Resume Next, you may get unexpected and unwanted results unless you've fully analysed the various permutations of events which trigger an error and you're happy to ignore them all.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Valued Forum Contributor Kamboj's Avatar
    Join Date
    09-25-2014
    Location
    India
    MS-Off Ver
    2003 - 2010
    Posts
    430

    Re: Deleting content from a range of cells

    use in the code
    on error resume next
    Kamboj
    _________________________________________________________________________________
    Mark the thread as SOLVED if my answer satisfy you.

  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
    44,767

    Re: Deleting content from a range of cells

    You'll get a Type MisMatch, error 13, if you put a non-numeric character on column E.

    Maybe this:

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim cell As Range
    Dim r As Range
    
    'Stop
    
    ' cater for clearing entire column(s)
    If Target.Rows.Count = Me.Rows.Count Then Exit Sub
    
    For Each r In Target.Cells
        If Not Intersect(Target, Range("C:D")) Is Nothing Then
            For Each cell In Intersect(Target, Range("C:D"))
                With cell
                    If Not .HasFormula Then
                        Application.EnableEvents = False
                        .Value = UCase(.Value)
                        Application.EnableEvents = True
                    End If '.HasFormula
                End With 'cell
            Next 'cell
        End If
        If Not Intersect(r, Range("E5:E10005")) Is Nothing Then
            With r
                If Not .HasFormula Then
                    Application.EnableEvents = False
                    On Error Resume Next
                    .Value = "0:" & Application.Ceiling(Round(.Value * 60, 0), 15)
                    On Error GoTo 0
                    Application.EnableEvents = True
                End If '.HasFormula
            End With 'r
        End If
    Next 'r
    
    End Sub

    Proper indenting and some "white space" will make it easier to read and check that all the blocks are started and ended correctly.

    As has been suggested, On Error Resume Next will cater for the non-numeric entry in column E.

    Commenting out the "cell" on "Next cell" has no effect. It is my understanding that the Next does not require a parameter and it is, I believe, more efficient to leave the parameter/variable out. However, for clarity, it is good practice to leave it there and comment it out rather than simply remove it.

    Regards, TMS
    Last edited by TMS; 11-09-2015 at 08:41 AM. Reason: Edit code
    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


  7. #7
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Re: Deleting content from a range of cells

    Thank you so much, it works perfectly.


    Quote Originally Posted by TMS View Post
    You'll get a Type MisMatch, error 13, if you put a non-numeric character on column E.

    Maybe this:

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim cell As Range
    Dim r As Range
    
    'Stop
    
    ' cater for clearing entire column(s)
    If Target.Rows.Count = Me.Rows.Count Then Exit Sub
    
    For Each r In Target.Cells
        If Not Intersect(Target, Range("C:D")) Is Nothing Then
            For Each cell In Intersect(Target, Range("C:D"))
                With cell
                    If Not .HasFormula Then
                        Application.EnableEvents = False
                        .Value = UCase(.Value)
                        Application.EnableEvents = True
                    End If '.HasFormula
                End With 'cell
            Next 'cell
        End If
        If Not Intersect(r, Range("E5:E10005")) Is Nothing Then
            With r
                If Not .HasFormula Then
                    Application.EnableEvents = False
                    On Error Resume Next
                    .Value = "0:" & Application.Ceiling(Round(.Value * 60, 0), 15)
                    On Error GoTo 0
                    Application.EnableEvents = True
                End If '.HasFormula
            End With 'r
        End If
    Next 'r
    
    End Sub

    Proper indenting and some "white space" will make it easier to read and check that all the blocks are started and ended correctly.

    As has been suggested, On Error Resume Next will cater for the non-numeric entry in column E.

    Commenting out the "cell" on "Next cell" has no effect. It is my understanding that the Next does not require a parameter and it is, I believe, more efficient to leave the parameter/variable out. However, for clarity, it is good practice to leave it there and comment it out rather than simply remove it.

    Regards, TMS

  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
    44,767

    Re: Deleting content from a range of 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. Replies: 1
    Last Post: 07-29-2015, 02:32 PM
  2. [SOLVED] Deleting content of unlocked cells
    By mikerules in forum Excel General
    Replies: 4
    Last Post: 03-21-2014, 01:53 AM
  3. Deleting a range of cells based on a deletion of a cell outside that range
    By Carlos90 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2013, 03:07 PM
  4. Deleting specified cells based on their content
    By cm22v07 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-25-2010, 09:41 AM
  5. Deleting cells depending on content of another cell
    By drgogo in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-18-2008, 10:16 PM
  6. Deleting content in cells based on criteria
    By morris5984 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2008, 11:14 AM
  7. Deleting Cells where content equals "0" or null
    By bm4466 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-03-2006, 02:16 PM

Tags for this Thread

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