Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 6th, 2008, 07:46 AM
Authorized User
 
Join Date: Mar 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Emptying cells

I'm busy with a sheet where values are filled in - no text.
Is there a way to empty cells in a certain range if the content is not numeric?

Thanks for the help :)


 
Old October 6th, 2008, 09:28 AM
Registered User
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I hope I understood your issue...I am assuming you were doing this AFTER data was entered to the sheet and not at data entry time (although there is a way to do this so that only numeric values are entered in the first place).

I would create a named range to include the target cells. I am not sure of your worksheet design, but the following example assumes a square grid of cells with the named range "rngToClear" covering the entire grid.

Here is the macro:

Option Explicit
Option Compare Text
Sub ClearNonNumeric()
    Dim intMinCol As Integer
    Dim intMaxCol As Integer
    Dim i As Integer
    Dim j As Integer

    Dim lngMinRow As Long
    Dim lngMaxRow As Long

    Dim objSheet As Worksheet

    Set objSheet = ThisWorkbook.Worksheets(1)

    With objSheet
        intMinCol = .Range("rngToClear").Column
        intMaxCol = .Range("rngToClear").Columns.Count
        lngMinRow = .Range("rngToClear").Row
        lngMaxRow = .Range("rngToClear").Rows.Count
        For i = lngMinRow To lngMaxRow
            For j = intMinCol To intMaxCol
                If Not IsNumeric(.Cells(i, j).Value) Then
                    .Cells(i, j).ClearContents
                End If
            Next j
        Next i
    End With

End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
emptying contents of temp folder using vb wingers Visual Basic 2008 Essentials 1 June 27th, 2008 10:38 AM
Locked Cells dkr72 Excel VBA 1 December 13th, 2004 05:04 AM
Datagrid and cells haroon .NET Framework 2.0 1 October 11th, 2004 10:46 AM
Cells doubleclick mushu Excel VBA 6 September 29th, 2004 02:52 AM
Cells MattLeek Excel VBA 2 December 12th, 2003 11:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.