Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default If-Else Simple Loop function

hi,

i am trying to use a If-Else Loop or If-ElseIf-Else loop,but the VB editor
keep prompting me a mistake with Use "Next..without For" comment. Is it
something wrong with my if-else function or otherwise. Pls advise.

My Example:

Sub COMPAREWH()

Dim LastRow

Range("A65536").Select
Selection.End(xlUp).Select

LastRow = ActiveCell.Row
LR$ = LastRow

Range("A2").Select
Sheets("Tabelle1").Select

'Start of For...Next Loop
For i = ActiveCell.Row To LastRow

COMPARE$ = Cells(i, 1).Value
Vlook$ = Cells(i, 4).Value

If COMPARE$ = "1" Then GoTo Skiptohere

ElseIf COMPARE$ = "2" Then
Cells(i, ActiveCell.Column).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With

Else: GotoSkiptohere
End If

Next i

Skiptohe
ActiveCell.Offset(1, 0).Range("A1").Select


End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default If-Else Simple Loop function


It appears to me that you are prematurely exiting the For. . .Next loop
when you go to Skiptohere.

Steve


--
smcferrin
------------------------------------------------------------------------
smcferrin's Profile: http://www.excelforum.com/member.php...o&userid=25761
View this thread: http://www.excelforum.com/showthread...hreadid=391715

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default If-Else Simple Loop function

Hi Junior728,

Try this adaptation of your code:

Sub COMPAREWH()
Dim LastRow As Long
Dim sh As Worksheet
Dim vCompare As Variant
Dim vVlook As Variant
Dim i As Long

Set sh = ThisWorkbook.Sheets("Tabelle")

LastRow = sh.Cells(Rows.Count, "A").End(xlUp).Row

'Start of For...Next Loop
For i = 2 To LastRow
vCompare = Cells(i, 1).Value
vVlook = Cells(i, 4).Value

If vCompare = 1 Then
Cells(i + 1, "A").Select
Exit For
ElseIf vCompare = 2 Then
With Cells(i, "A").Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
End If
Next i

End Sub

Note that I have removed all selections (except for one). It is rarely
necessary, and usualyy less efficient, to make physical selections.

Test the revised code on a *copy* of your workbook to ensure that my
intepretation properly reflects your requirements.

---
Regards,
Norman



"Junior728" wrote in message
...
hi,

i am trying to use a If-Else Loop or If-ElseIf-Else loop,but the VB editor
keep prompting me a mistake with Use "Next..without For" comment. Is it
something wrong with my if-else function or otherwise. Pls advise.

My Example:

Sub COMPAREWH()

Dim LastRow

Range("A65536").Select
Selection.End(xlUp).Select

LastRow = ActiveCell.Row
LR$ = LastRow

Range("A2").Select
Sheets("Tabelle1").Select

'Start of For...Next Loop
For i = ActiveCell.Row To LastRow

COMPARE$ = Cells(i, 1).Value
Vlook$ = Cells(i, 4).Value

If COMPARE$ = "1" Then GoTo Skiptohere

ElseIf COMPARE$ = "2" Then
Cells(i, ActiveCell.Column).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With

Else: GotoSkiptohere
End If

Next i

Skiptohe
ActiveCell.Offset(1, 0).Range("A1").Select


End Sub




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default If-Else Simple Loop function

Haven't tested it, but this line

If COMPARE$ = "1" Then GoTo Skiptohere

is self contianed, there can be no Else associated with it beacuse you have
included the action on the same line. Your code whould be like

If COMPARE$ = "1" Then
GoTo Skiptohere
ElseIf COMPARE$ = "2" Then

or better still

If COMPARE$ = "1" Then
Exit For
ElseIf COMPARE$ = "2" Then

--
HTH

Bob Phillips

"Junior728" wrote in message
...
hi,

i am trying to use a If-Else Loop or If-ElseIf-Else loop,but the VB editor
keep prompting me a mistake with Use "Next..without For" comment. Is it
something wrong with my if-else function or otherwise. Pls advise.

My Example:

Sub COMPAREWH()

Dim LastRow

Range("A65536").Select
Selection.End(xlUp).Select

LastRow = ActiveCell.Row
LR$ = LastRow

Range("A2").Select
Sheets("Tabelle1").Select

'Start of For...Next Loop
For i = ActiveCell.Row To LastRow

COMPARE$ = Cells(i, 1).Value
Vlook$ = Cells(i, 4).Value

If COMPARE$ = "1" Then GoTo Skiptohere

ElseIf COMPARE$ = "2" Then
Cells(i, ActiveCell.Column).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With

Else: GotoSkiptohere
End If

Next i

Skiptohe
ActiveCell.Offset(1, 0).Range("A1").Select


End Sub




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
simple loop needed please Alan Excel Discussion (Misc queries) 1 May 5th 10 06:33 PM
I need a simple loop with a 4+ row added in. pgarcia Excel Discussion (Misc queries) 5 July 19th 07 07:42 PM
Simple question on For...Next loop Alex Excel Programming 1 July 26th 05 03:30 PM
Some help w/ simple loop, please? terry b Excel Programming 6 February 6th 05 06:17 PM
simple loop macro jim27 Excel Programming 2 October 17th 04 06:28 PM


All times are GMT +1. The time now is 01:39 AM.

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

About Us

"It's about Microsoft Excel"