 |
| 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
|
|
|
|

July 31st, 2009, 09:41 AM
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Using CountIf in vba
Hi
Could someone help please. I am new to VBA and this is my first post.
I am trying simply to delete a column if a particular cell in that column contains the word "Total". I thought this would be easy!
Sub RemoveColumns()
finalcolumn = Cells(4, Columns.Count).End(xlToLeft).Column
For k = finalcolumn To 2 Step -1
If Application.WorksheetFunction.CountIf(Cells(4, k).Value, "*Total*") = 1 Then
Cells(1, k).EntireColumn.Delete
End If
Next k
End Sub
When I run this I get "Run Time Error 424 Object Required".
What am I doing wrong?
|
|

July 31st, 2009, 09:51 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
Hi
Welcome to Wrox!
The error occurs as Countif expects a Range and you have given the Value. The following code should work
Code:
Sub RemoveColumns_CountIf()
Dim finalcolumn
Dim k
finalcolumn = Cells(4, Columns.Count).End(xlToLeft).Column
For k = finalcolumn To 2 Step -1
If Application.WorksheetFunction.CountIf(Cells(4, k), "*Total*") = 1 Then
Cells(1, k).EntireColumn.Delete
End If
Next k
End Sub
Since you are checking each cell for a value, you can do it using VBA's Instr function. Here is a sample of that
Code:
Sub RemoveColumns_Instr()
Dim finalcolumn
Dim k
finalcolumn = Cells(4, Columns.Count).End(xlToLeft).Column
For k = finalcolumn To 2 Step -1
If InStr(1, Cells(4, k).Value, "Total", vbTextCompare) = 1 Then
Cells(1, k).EntireColumn.Delete
End If
Next k
End Sub
Cheers
Shasur
|
|

August 1st, 2009, 07:51 AM
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Using CountIf in VBA
Hi Shasur
Thank you very much for this. I was being a bit silly I am afraid not to notice that a range not a value was required. Also thank you for introducing me to InStr.
Could I ask a supplementary?
I have more than one criterion for deleting the column and have the following code:
Sub RemoveColumns()
finalcolumn = Cells(5, Columns.Count).End(xlToLeft).Column
For k = finalcolumn To 1 Step -1
If Application.WorksheetFunction.CountIf(Cells(5, k), "*Total*") = 1 Then
Cells(1, k).EntireColumn.Delete
End If
If Application.WorksheetFunction.CountIf(Cells(5, k), "*52 Weekly*") = 1 Then
Cells(1, k).EntireColumn.Delete
End If
Next k
End Sub
Is there a more elegant way of writing this? I feel I ought to be able to use With End-With but don't see how to use it in this case.
Thanks again
David
|
|

August 1st, 2009, 09:20 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
You can tweak your code a bit like:
Code:
Sub RemoveColumns()
Dim finalcolumn, k
finalcolumn = Cells(5, Columns.Count).End(xlToLeft).Column
For k = finalcolumn To 1 Step -1
If Application.WorksheetFunction.CountIf(Cells(5, k), "*Total*") = 1 Or Application.WorksheetFunction.CountIf(Cells(5, k), "*52 Weekly*") = 1 Then
Cells(1, k).EntireColumn.Delete
End If
Next k
End Sub
Select case might be helpful if you are checking against the entire contents of the cell.
Cheers
Shasur
|
|

August 2nd, 2009, 01:54 AM
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
CountIf in vba
Thank you - very helpful.
David
|
|

October 14th, 2014, 12:40 PM
|
|
Registered User
|
|
Join Date: Oct 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks
Shure thing.
Thanks. Victor
|
|
 |