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 January 14th, 2009, 05:30 PM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Problem with Range.Find and CountA

Hello - here is what I'm trying to accomplish and hoping someone can assist:

I want to search the first row (header row) of my data for the cell which contains the text "xyz". I want to then count the number of cells in this column that contain data (CountA). The count result should be returned as an integer for use as a variable in a For/Next Loop.

Thanks much.
 
Old January 15th, 2009, 12:56 AM
Authorized User
 
Join Date: Nov 2007
Posts: 48
Thanks: 0
Thanked 4 Times in 4 Posts
Default

crugg1,

This should get you started in the right direction:

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Press and hold down the 'ALT' key, and press the 'F11' key.

On the 'Insert' menu, click 'Module'.

Copy the below code, and paste it into the Module (on the right pane).


Code:
 
Option Explicit
Sub Test()
    Dim LR, LC, xyzCol, xyzCountA, Ctr As Long
    Dim ColName As String
    Dim MyRng, MyRng2 As Range
    With ActiveSheet
        LC = .Cells(1, Columns.Count).End(xlToLeft).Column
        Set MyRng = Range(Cells(1, 1), Cells(1, LC))
        xyzCol = Application.WorksheetFunction.Match("xyz", MyRng, 0)
        ColName = Replace(Cells(1, xyzCol).Address(0, 0), 1, "")
        LR = .Cells(Rows.Count, xyzCol).End(xlUp).Row
        Set MyRng2 = .Range(Cells(2, xyzCol), Cells(LR, xyzCol))
        xyzCountA = Application.WorksheetFunction.CountA(MyRng2)
    
        MsgBox "The string 'xyz' in row '1', was found in column '" & ColName & "'," & vbCrLf & vbCrLf & _
            "the last row of data in column '" & ColName & "' is row '" & LR & "'," & vbCrLf & vbCrLf & _
            "and the count of cells with data in this range is '" & xyzCountA & "' cells."
    End With
End Sub

Then run the "Test()" macro.


Have a great day,
Stan
__________________
stanleydgromjr

Windows 8.1, Excel 2007.
 
Old January 15th, 2009, 06:31 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Quote:
Originally Posted by crugg1 View Post
Hello - here is what I'm trying to accomplish and hoping someone can assist:

I want to search the first row (header row) of my data for the cell which contains the text "xyz". I want to then count the number of cells in this column that contain data (CountA). The count result should be returned as an integer for use as a variable in a For/Next Loop.

Thanks much.
Here is a hint

Sub Check_XYX_ROWCnt()
Dim rFnd As Range
Set rFnd = Rows(1).Cells.Find("XYZ")
If Not rFnd Is Nothing Then
MsgBox (WorksheetFunction.CountA(Columns(rFnd.Column)))
End If
End Sub

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

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old January 15th, 2009, 10:34 AM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks!

Both work.

Thank you gentlemen.





Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Macro FIND THIS MONTH in a given range mvatoi Excel VBA 3 August 24th, 2007 08:12 AM
find days per month in a date range rojer MySQL 1 June 5th, 2007 07:36 PM
how to find text within range of two tags dipsut XSLT 3 May 25th, 2007 04:27 PM
How to find a date range between another date rang tayvonne Access 2 August 3rd, 2006 09:50 AM
find name and define range for Column qball Excel VBA 4 November 17th, 2004 03:55 PM





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