PC Review


Reply
Thread Tools Rate Thread

How to check if the cell contains number 1 by moving down thecolumns?

 
 
cyberdude
Guest
Posts: n/a
 
      17th Oct 2009
Hi,

Suppose the cursor is on a certain row (say row 3 or 40), I want to
check if each cell from column B to column AF contains the number 1.
If it does, the counter i is incremented by 1. I want to do it by a
for next loop such that the column number is a variable. Can it be
done this way? Thank you.

Mike
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      17th Oct 2009
You can loop through the columns.

Dim myRow as long
dim iCol as long
dim FirstCol as long
dim LastCol as long
dim myCount as long

with activesheet
firstcol = .range("B1").column 'I know that column B is 2
lastcol = .range("AF1").column 'I hate looking at column AF

myCount = 0
myrow = activcell.row
for icol = firstcol to lastcol
if .cells(myrow,icol).value = 1 then
mycount = mycount + 1
end if
next icol
end with

=======
Another way to do this is to use Excel's =countif() function:

Dim myRow as long
dim myCount as long

with activesheet
myrow = activcell.row
mycount = application.countif(.cells(myrow,1).range("b1:Af1"), 1)
end with



cyberdude wrote:
>
> Hi,
>
> Suppose the cursor is on a certain row (say row 3 or 40), I want to
> check if each cell from column B to column AF contains the number 1.
> If it does, the counter i is incremented by 1. I want to do it by a
> for next loop such that the column number is a variable. Can it be
> done this way? Thank you.
>
> Mike


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      17th Oct 2009
ps.

Fix my typing error(s).

Activcell should be ActiveCell.

(There may be more!)

cyberdude wrote:
>
> Hi,
>
> Suppose the cursor is on a certain row (say row 3 or 40), I want to
> check if each cell from column B to column AF contains the number 1.
> If it does, the counter i is incremented by 1. I want to do it by a
> for next loop such that the column number is a variable. Can it be
> done this way? Thank you.
>
> Mike


--

Dave Peterson
 
Reply With Quote
 
Mike Fogleman
Guest
Posts: n/a
 
      17th Oct 2009
Dave, suppose the cell value is 695173. Will it find the 1 ?

Mike F
"Dave Peterson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> ps.
>
> Fix my typing error(s).
>
> Activcell should be ActiveCell.
>
> (There may be more!)
>
> cyberdude wrote:
>>
>> Hi,
>>
>> Suppose the cursor is on a certain row (say row 3 or 40), I want to
>> check if each cell from column B to column AF contains the number 1.
>> If it does, the counter i is incremented by 1. I want to do it by a
>> for next loop such that the column number is a variable. Can it be
>> done this way? Thank you.
>>
>> Mike

>
> --
>
> Dave Peterson



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      17th Oct 2009
Nope.

Did you want it to?

If yes, you could use:

If InStr(1, .Cells(myrow, iCol).Value, 1, vbTextCompare) > 0 Then
instead of:
if .cells(myrow,icol).value = 1 then


Mike Fogleman wrote:
>
> Dave, suppose the cell value is 695173. Will it find the 1 ?
>
> Mike F
> "Dave Peterson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > ps.
> >
> > Fix my typing error(s).
> >
> > Activcell should be ActiveCell.
> >
> > (There may be more!)
> >
> > cyberdude wrote:
> >>
> >> Hi,
> >>
> >> Suppose the cursor is on a certain row (say row 3 or 40), I want to
> >> check if each cell from column B to column AF contains the number 1.
> >> If it does, the counter i is incremented by 1. I want to do it by a
> >> for next loop such that the column number is a variable. Can it be
> >> done this way? Thank you.
> >>
> >> Mike

> >
> > --
> >
> > Dave Peterson


--

Dave Peterson
 
Reply With Quote
 
Mike Fogleman
Guest
Posts: n/a
 
      17th Oct 2009
The op wasn't too specific about that, but did say "contains the number 1".

Mike F
"Dave Peterson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Nope.
>
> Did you want it to?
>
> If yes, you could use:
>
> If InStr(1, .Cells(myrow, iCol).Value, 1, vbTextCompare) > 0 Then
> instead of:
> if .cells(myrow,icol).value = 1 then
>
>
> Mike Fogleman wrote:
>>
>> Dave, suppose the cell value is 695173. Will it find the 1 ?
>>
>> Mike F
>> "Dave Peterson" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > ps.
>> >
>> > Fix my typing error(s).
>> >
>> > Activcell should be ActiveCell.
>> >
>> > (There may be more!)
>> >
>> > cyberdude wrote:
>> >>
>> >> Hi,
>> >>
>> >> Suppose the cursor is on a certain row (say row 3 or 40), I want to
>> >> check if each cell from column B to column AF contains the number 1.
>> >> If it does, the counter i is incremented by 1. I want to do it by a
>> >> for next loop such that the column number is a variable. Can it be
>> >> done this way? Thank you.
>> >>
>> >> Mike
>> >
>> > --
>> >
>> > Dave Peterson

>
> --
>
> Dave Peterson



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      17th Oct 2009
If a cell holds: 23451234
Then it contains the numeral (or digit or character) 1.

But it doesn't contain the number 1.

(Just to be argumentative <vbg>.)



Mike Fogleman wrote:
>
> The op wasn't too specific about that, but did say "contains the number 1".
>
> Mike F
> "Dave Peterson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Nope.
> >
> > Did you want it to?
> >
> > If yes, you could use:
> >
> > If InStr(1, .Cells(myrow, iCol).Value, 1, vbTextCompare) > 0 Then
> > instead of:
> > if .cells(myrow,icol).value = 1 then
> >
> >
> > Mike Fogleman wrote:
> >>
> >> Dave, suppose the cell value is 695173. Will it find the 1 ?
> >>
> >> Mike F
> >> "Dave Peterson" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> > ps.
> >> >
> >> > Fix my typing error(s).
> >> >
> >> > Activcell should be ActiveCell.
> >> >
> >> > (There may be more!)
> >> >
> >> > cyberdude wrote:
> >> >>
> >> >> Hi,
> >> >>
> >> >> Suppose the cursor is on a certain row (say row 3 or 40), I want to
> >> >> check if each cell from column B to column AF contains the number 1.
> >> >> If it does, the counter i is incremented by 1. I want to do it by a
> >> >> for next loop such that the column number is a variable. Can it be
> >> >> done this way? Thank you.
> >> >>
> >> >> Mike
> >> >
> >> > --
> >> >
> >> > Dave Peterson

> >
> > --
> >
> > Dave Peterson


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
check if a cell contains a number, not text shy1162 Microsoft Excel Programming 2 6th Sep 2010 07:14 AM
Is there a way to check the number of different cell formats? Clay2007 Microsoft Excel Crashes 0 12th Dec 2008 10:07 PM
Check if the value of one cell can be found in a number of other c James T Kirk Microsoft Excel Programming 2 5th Nov 2008 03:35 PM
check if a cell has date or a number mona Microsoft Excel Misc 5 15th May 2008 02:39 PM
Re: Check boxes in Excel: Not moving with cell... Dave Peterson Microsoft Excel Misc 1 11th Jan 2007 05:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:27 AM.