Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 470
Default Problem with For Each loop

I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,942
Default Problem with For Each loop

hi
you are getting a deceptive error. from your code i see you are missing an
end if
add that in and your next without for error should go away.
ie
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
End If
Next Cell

regards
FSt1

Regards
FSt1

"WLMPilot" wrote:

I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 470
Default Problem with For Each loop

Thanks, that was the problem.

Les

"FSt1" wrote:

hi
you are getting a deceptive error. from your code i see you are missing an
end if
add that in and your next without for error should go away.
ie
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
End If
Next Cell

regards
FSt1

Regards
FSt1

"WLMPilot" wrote:

I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Problem with For Each loop

You could use a worksheet function:

=countif(a1:a99,"*-inc")

You could use it in code, too:

dim BegExpRng as long
dim BegRng as long
dim EndRng as long

'test data
begrng = 33
endrng = 66

begexprng = application.countif _
(activesheet.range("a" & begrng & ":A" & endrng), "*-Inc")

WLMPilot wrote:

I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 470
Default Problem with For Each loop

Thanks! the option you gave to use in a macro is what I needed. I took the
loop out.

Along the same thought process (not using a loop), is there a macro command
that will look at a range, say I5:I56, and if the cell.value = "P", I can
change it to "" (null)?

Thanks again,
Les

"Dave Peterson" wrote:

You could use a worksheet function:

=countif(a1:a99,"*-inc")

You could use it in code, too:

dim BegExpRng as long
dim BegRng as long
dim EndRng as long

'test data
begrng = 33
endrng = 66

begexprng = application.countif _
(activesheet.range("a" & begrng & ":A" & endrng), "*-Inc")

WLMPilot wrote:

I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les


--

Dave Peterson



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Problem with For Each loop

Record a macro when you select a range and do an Edit|Replace. Make sure you
change the settings you want--match case, look at the whole cell, ...

You'll end up with code like

Range("I5:I56").Select
Selection.Replace What:="p", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

You can drop the selection and use:

Range("I5:I56").Replace What:="p", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False

(I dropped the SearchFormat and ReplaceFormat stuff. This stuff was added in
xl2002 (IIRC) and will break xl2k and below.)




WLMPilot wrote:

Thanks! the option you gave to use in a macro is what I needed. I took the
loop out.

Along the same thought process (not using a loop), is there a macro command
that will look at a range, say I5:I56, and if the cell.value = "P", I can
change it to "" (null)?

Thanks again,
Les

"Dave Peterson" wrote:

You could use a worksheet function:

=countif(a1:a99,"*-inc")

You could use it in code, too:

dim BegExpRng as long
dim BegRng as long
dim EndRng as long

'test data
begrng = 33
endrng = 66

begexprng = application.countif _
(activesheet.range("a" & begrng & ":A" & endrng), "*-Inc")

WLMPilot wrote:

I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les


--

Dave Peterson


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Problem with For Each loop

Missing an end if OR line continuation character
OR bring up to one line and you don't need selections

for each cell in Range("A" & begrng & ":A" & endrng)
If Right(Cell.Value, 4) = "-Inc" Then begexprng = begexprng + 1
Next Cell
msgbox begexprng

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"WLMPilot" wrote in message
...
I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There
are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Problem with For Each loop

You also may want to declare some variables if you haven't already. I'd
recommend adding

Option Explicit

Before your Sub ... line. This will force you to declare all variables.
--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.



"WLMPilot" wrote:

I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 470
Default Problem with For Each loop

Have already declared everything. Dave Peterson suggested an option that
worked great.

Thanks again,
Les

"Barb Reinhardt" wrote:

You also may want to declare some variables if you haven't already. I'd
recommend adding

Option Explicit

Before your Sub ... line. This will force you to declare all variables.
--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.



"WLMPilot" wrote:

I have the following code:

begexprng = 0
Range("A" & begrng & ":A" & endrng).Select
For Each Cell In Selection
If Right(Cell.Value, 4) = "-Inc" Then
begexprng = begexprng + 1
Next Cell

I am trying to select a group of cells in column A and count the ones that
have
"-Inc". It should count any cell with either "1-Inc" or "2-Inc" There are
other values in column A also.

The problem is I am getting a compile error: Next without For.

Any ideas?

Thanks,
Les

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
loop problem teepee[_3_] Excel Discussion (Misc queries) 3 December 31st 08 11:09 AM
Loop Within a Loop Problem [email protected] Excel Programming 3 December 5th 07 01:23 PM
Problem adding charts using Do-Loop Until loop Chris Bromley[_2_] Excel Programming 2 May 23rd 05 01:31 PM
Loop Problem Todd Huttenstine Excel Programming 10 April 12th 04 06:15 PM
loop problem joao Excel Programming 4 November 6th 03 02:01 PM


All times are GMT +1. The time now is 12:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"