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 July 31st, 2009, 09:41 AM
Registered User
 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?
 
Old July 31st, 2009, 09:51 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

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
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 1st, 2009, 07:51 AM
Registered User
 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old August 1st, 2009, 09:20 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

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
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 2nd, 2009, 01:54 AM
Registered User
 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default CountIf in vba

Thank you - very helpful.

David
 
Old October 14th, 2014, 12:40 PM
Registered User
 
Join Date: Oct 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks

Shure thing.

Thanks. Victor





Similar Threads
Thread Thread Starter Forum Replies Last Post
Countif in VBA xbenx Excel VBA 9 November 5th, 2011 11:15 PM
COUNTIF Function can't reference other workbooks kuznickic Excel VBA 1 October 5th, 2007 04:35 AM
Code works in Excel VBA but not Access VBA fossx Access VBA 2 May 21st, 2007 08:00 AM
WorksheetFunction.COUNTIF fails on the second pass Flower Access VBA 3 April 25th, 2007 02:45 AM
Excel VBA to SQL & back to VBA edesousa Excel VBA 1 June 1st, 2004 02:39 AM





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