Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 35

Thread: Clear contents of cells

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Clear contents of cells

    What is the correct way to clear the contents of a range of cells based on the value in column A when using a loop or CASE statement? Thanks
    Peace of mind is found in some of the strangest places.

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What do you mean by the correct way?

    Generally to clear a range

    Range("A1:A10").ClearContents
    What code do you have already?

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    ok..Say you have column A and you want to loop through looking for "PAGE" or "BILL" and if the string is there clear the contents of cells A thru F on that row.?
    Peace of mind is found in some of the strangest places.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by austenr
    ok..Say you have column A and you want to loop through looking for "PAGE" or "BILL" and if the string is there clear the contents of cells A thru F on that row.?
    Off the top, untested

    Dim cLastRow As Long
    Dim i As Long 
    cLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To cLastRow
    If Cells(, "A").Value = "PAGE" Or _
    Cells(i, "A").Value = "Bill" Then
    Cells(i, "A").Resize(, 6).ClearContents
    End If
    Next

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Perhaps something like this.

    LastRow = Range("A65536").End(xlUp).Row
    
    For I = 1 to LastRow
    
    Set rng = Range("A" & I)
    
    Select Case rng.Value
    
    Case "PAGE", "BILL"
    
    rng.Resize(1,6).ClearContents
    
    Case Else
    
    ' Do nothing
    End Select
    Next I

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Why loop?

    Option Explicit
    
    Sub ClearPageOrBill()
        Dim rngClear As Range
        Set rngClear = Range("A2:F" & Range("F65536").End(xlUp).Row)
        rngClear.AutoFilter field:=1, Criteria1:="=PAGE", Operator:=xlOr, Criteria2:="=BILL"
        Intersect(rngClear, rngClear.SpecialCells(xlCellTypeVisible)).ClearContents
    End Sub

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Two more things to add, for error checking, if you'd like.

    1)
    On Error Resume Next
    Add this if you don't want to error out if no cells were found.

    2)[
    ActiveSheet.Cells.AutoFilter
    Add this if you don't want AutoFilter to show when done. You can check for it at start if you'd like with some other code. This also assumes you will be running it on the activesheet, as no sheet is explicitly set.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by firefytr
    Why loop?
    Why Intersect?

    Sub ClearPageOrBill()
    Dim rngClear As Range
    Set rngClear = Range("A1:F" & Cells(Rows.Count,"A").End(xlUp).Row)
    rngClear.AutoFilter field:=1, Criteria1:="=PAGE", Operator:=xlOr, Criteria2:="=BILL"
    rngClear.SpecialCells(xlCellTypeVisible).ClearContents
    End Sub
    You are better advised to get the row count from A, F might already be clear.

    Also, a bit of evangelising . I am on a personal crusade to convince VBAers not to use
    Range("A65536")
    but to use
    Cells(Rows.Count,"A")
    instead. It looks better, you don't have to remember the number, and it caters for all Excel versions (remember 32767 rows).

  9. #9
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thanks xld & Fire...xld got it for me. Thanks Solved
    Peace of mind is found in some of the strangest places.

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by xld
    Why Intersect
    Good point. Force of habit I guess.

    Quote Originally Posted by xld
    You are better advised to get the row count from A, F might already be clear.
    Really dependent on the user. Six one, half a dozen the other.

    Quote Originally Posted by xld
    Cells(Rows.Count,"A")
    I like that. It does look better. The compatability cannot be refuted either.

    Quote Originally Posted by xld
    (remmeber 32767 rows).
    LOL!

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by xld
    Why intersect?
    Just teasing, mimicing your reply (the sincerest form of flattery they say)

    Quote Originally Posted by firefytr
    Really dependent on the user. Six one, half a dozen the other.
    Well it didn't work in my test <G>

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by xld
    Well it didn't work in my test <G>
    But point well given!

    And one can't be faulted for setting up a pertinent negative. <vbg>

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by firefytr
    And one can't be faulted for setting up a pertinent negative. <vbg>
    Yeah, I have a dilemma there. I seem to have a perspective that looks a skew at something, which makes me a good tester, but I haaaaate testing ..........

  14. #14
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Quote Originally Posted by xld
    Also, a bit of evangelising . I am on a personal crusade to convince VBAers not to use
    Range("A65536")
    but to use
    Cells(Rows.Count,"A")
    instead. It looks better, you don't have to remember the number, and it caters for all Excel versions (remember 32767 rows).
    Hi xld,

    very interesting approach! I must admit I always used Range("A65536") because always intendend to run my macros in XL 2003, but now I got a doubt. Talking about performance are they equal or is one approach faster than the other?
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Paleo
    very interesting approach! I must admit I always used Range("A65536") because always intendend to run my macros in XL 2003, but now I got a doubt. Talking about performance are they equal or is one approach faster than the other?
    Hi Paleo,

    The Rows.Count will be faster, because it is using built-in constant values, VBA will calculate at start for the workbook, which will be quicker than parsing your code to get the number, convert it iand store it in memory, etc. But it will be so infinitesimal, no-one will notice, and it's not the sort of thing you are likely to do a million times in an application, which might then reap the benefits.

    I do it more as a style thing, I just think it looks nicer.

    Interestingly, from my old coding days I was always taught to declare constant variables, even for 0 and 1 for loops etc., and that is still good advice IMO in a serious app that is busy. In those sort of apps you should be aware of all performance savings. Interesting though because although I do it in VB, I don't in VBA (guess I don't write the big apps in VBA) .

  16. #16
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Using last 6 characters of a cell for its value

    I want to use this code to loop through column A and use the last 6 characters of the cell for its value only if it is a number and not purely alpha. Am having a problem getting it to skip the alpha only cells and those that are less than 10 characters. Also want to skip cells that contain +----" Any help would be appreciated.

    Sub CheckForNumeric()
    Dim newString As String
    Dim I As Integer
    I = 1
    While Worksheets("Sheet2").Range("A" & I) > "0"
    newString = Worksheets("Sheet2").Range("A" & I)
    newString = Right(newString, Len(newString) - 10)
    Worksheets("Sheet2").Range("A" & I) = newString
    Wend
    End Sub

    Sample data attached.
    Peace of mind is found in some of the strangest places.

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by austenr
    I want to use this code to loop through column A and use the last 6 characters of the cell for its value only if it is a number and not purely alpha. Am having a problem getting it to skip the alpha only cells and those that are less than 10 characters. Also want to skip cells that contain +----" Any help would be appreciated.
    Is this what you mean?

    Sub CheckForNumeric()
    Dim newString As String
    Dim I As Integer
    I = 1
    With Worksheets("Sheet2")
    For I = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row
    If Len(.Cells(I, "A").Value) >= 10 Then
    If IsNumeric(Right(.Cells(I, "A").Value)) Then
    newString = .Range("A" & I)
    newString = Right(newString, Len(newString) - 10)
    .Range("A" & I) = newString
    End If
    End If
    Next I
    End With
    End Sub

  18. #18
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by xld
    Also, a bit of evangelising . I am on a personal crusade to convince VBAers not to use
    Range("A65536")
    but to use
    Cells(Rows.Count,"A")
    instead. It looks better, you don't have to remember the number, and it caters for all Excel versions (remember 32767 rows).
    Like everyone else, it seems, I'm also intrigued by this. When was there only 32767 rows, Excel 5? Fortunately I came to programming a bit later than that! LOL! On the other hand, who knows when MS will give us more that 65536 rows too, so forward compatible code is good too!

    This will mean I have to retrain myself now... sometimes hard to do!

    Thanks for posting that, Bob! Good advice!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  19. #19
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi xld, I could be wrong, but from reading what's said in the text I think he only needs something like this mod
    
    Sub CheckForNumeric()
    Dim newString As String
    Dim I As Integer
    I = 1
    
    With Worksheets("Sheet2")
    For I = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row If IsNumeric(.Cells(I, "A").Value) Then .Range("A" & I) = Right(.Range("A" & I), 6) End If Next I End With End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  20. #20
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    My turn for preaching!


    Dim I As Integer


    I would suggest using Long here, not Integer. Since all Integer's have to be converted to Long's, Long is faster (although maybe not noticeable). The real issue here, though, is that if there are more that 32767 rows, this will fail, as an Integer can only go that high. Long's can deal with numbers as high as
    2,147,483,647, so give lots of room.

    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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