Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
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 May 29th, 2013, 12:12 AM
Registered User
 
Join Date: May 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using a countif function in vba code

Hi Everyone,

I am relatively new to vb and as such have been using the macro recorder to help me take my baby steps along the way. As such my code is exeptionally slow. I have been given the advice that to speed it up use variables instead of cells in the workbook itself. I am trying to get my countif function working using variables, however I have gotten stuck trying to count the total number of rows in the worksheet. I have done this before successfully and have literally copied code across but it's not working. Can anyone provide some help on what's going wrong? I have posted my code below.

Any other feedback or tips on how to get my code running faster would be much appreciated.

Sub Countif_trial()

Dim x As Long, var1 As Long, var2 As String

Sheets("Sheet2").Select
var2 = Range("A2").Value

With ThisWorkbook.Sheets("Sheet1")

x = .Cells(.Rows.count, 1).End(x1Up).Rows.count
var1 = Application.WorksheetFunction.CountIf(Range("A2:A" & x), var2)

End With


MsgBox var1


End Sub

Thanks,

Matt
 
Old May 29th, 2013, 06:21 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

You can try using


i1 = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell) .Row

to get the last row

Also you can try any of the methods listed below for improving macro performance

http://vbadud.blogspot.in/search?q=Speed
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 30th, 2013, 12:08 AM
Registered User
 
Join Date: May 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. It's moving much faster.

I have run into another problem with the countif returning 0. I also have the same formula ie =countif(A2:A5000,D1) which comes back with 4. Is there a reason that might happen? Perhaps the string isn't working in that instance? I have been returning the string and it seems to be the correct value.

Any help is much appreciated, Thanks,

Matt

Last edited by Ructile; May 30th, 2013 at 12:24 AM..
 
Old May 30th, 2013, 11:13 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Matt

My wild guess is the code is taking the values from the another workbook/sheet that might be active

You can avoid this by adding

Code:
Application.WorksheetFunction.CountIf(ActiveSheet.Range("A2:A" & x), var2)
or

Code:
 
Application.WorksheetFunction.CountIf(Workbooks(1).Sheets(2).Range("A2:A" & x), var2)
etc

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using CountIf in vba DavidSCowan Excel VBA 5 October 14th, 2014 12:40 PM
Countif in VBA xbenx Excel VBA 9 November 5th, 2011 11:15 PM
Mimicking function find using object oriented VBA code forest2425 Excel VBA 1 March 26th, 2010 10:36 AM
COUNTIF Function can't reference other workbooks kuznickic Excel VBA 1 October 5th, 2007 04:35 AM
TRIM, ISTEXT function won't work in Excel VBA code munzer1 Excel VBA 1 May 29th, 2007 09:35 AM





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