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
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.
What do you mean by the correct way?
Generally to clear a range
What code do you have already?Range("A1:A10").ClearContents
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.
Off the top, untestedOriginally Posted by austenr
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
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
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
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables
Two more things to add, for error checking, if you'd like.
1)Add this if you don't want to error out if no cells were found.On Error Resume Next
2)[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.ActiveSheet.Cells.AutoFilter
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables
Why Intersect?Originally Posted by firefytr
You are better advised to get the row count from A, F might already be clear.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
Also, a bit of evangelising . I am on a personal crusade to convince VBAers not to use
but to useRange("A65536")
instead. It looks better, you don't have to remember the number, and it caters for all Excel versions (remember 32767 rows).Cells(Rows.Count,"A")
Thanks xld & Fire...xld got it for me. Thanks Solved
Peace of mind is found in some of the strangest places.
Good point. Force of habit I guess.Originally Posted by xld
Really dependent on the user. Six one, half a dozen the other.Originally Posted by xld
I like that. It does look better. The compatability cannot be refuted either.Originally Posted by xld
LOL!Originally Posted by xld
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables
Just teasing, mimicing your reply (the sincerest form of flattery they say)Originally Posted by xld
Well it didn't work in my test <G>Originally Posted by firefytr
But point well given!Originally Posted by xld
And one can't be faulted for setting up a pertinent negative. <vbg>
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables
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 ..........Originally Posted by firefytr
Hi xld,Originally Posted by 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
Hi Paleo,Originally Posted by 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) .
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.
Is this what you mean?Originally Posted by austenr
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
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!Originally Posted by xld
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!
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 = 1With 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.
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!