Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default for loop exiting out early

It runs the If statement for me. Sounds like "intCounter" is not getting
1 for you.

HTH, Shockley



"jimmy" wrote in message
...
I have the following piece of code that contains 1 for
loop with two imbedded for loops in it. The problem i am
having is that the deepest for loop (ws2) when finishing
breaks out of both outer for loops (r, ws) and exits to
the end. I am not understanding why. the program doesnt
even run through to the if statement (intCounter), just
breaks when for loop (ws2) completes. Any suggestions....

For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If


For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value
intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets
intCounter = intCounter +
Application.WorksheetFunction.CountIf(ws2.Columns( 1), V)
Next ws2

If intCounter 1 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r


Next ws



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default for loop exiting out early

are you serious....great i dont know whats going on, i
copy and pasted that code from the script i have, and it
worked for you? when i step into it, it goes through
twice, like it should, the intCounter is at 15,000
something like it should be. but after it runs through
the loop twice, it exits out of everything...any
clue...here is my entire script, its supposed to delete
duplicate entries across multiple sheets. jouni helped me
write the tough parts...thanks again..

Public Sub DeleteDuplicateRows()
'
' This macro deletes duplicate rows in the selection.
Duplicates are
' counted in the COLUMN of the active cell.

Dim Col As Integer
Dim r As Long
Dim C As Range
Dim N As Long
Dim V As Variant
Dim Rng As Range

Dim ws As Worksheet, ws2 As Worksheet
Dim intCounter As Integer

On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Col = ActiveCell.Column

N = 0

For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If


For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value

intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets

intCounter = intCounter +
Application.WorksheetFunction.CountIf(ws2.Columns( 1), V)

Next ws2

If intCounter 1 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r


Next ws

EndMacro:

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

-----Original Message-----
It runs the If statement for me. Sounds

like "intCounter" is not getting
1 for you.

HTH, Shockley



"jimmy" wrote in message
...
I have the following piece of code that contains 1 for
loop with two imbedded for loops in it. The problem i

am
having is that the deepest for loop (ws2) when finishing
breaks out of both outer for loops (r, ws) and exits to
the end. I am not understanding why. the program

doesnt
even run through to the if statement (intCounter), just
breaks when for loop (ws2) completes. Any

suggestions....

For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If


For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value
intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets
intCounter = intCounter +
Application.WorksheetFunction.CountIf(ws2.Columns( 1), V)
Next ws2

If intCounter 1 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r


Next ws



.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default for loop exiting out early

If you want to remove all duplicates then the condition should be

intCounter 0 rather than
intCounter 1

Also, you are checking each worksheet against itself, so your script will
remove all values on all sheets. You need to increment the counter within
an If statement like this:

If ws2.Name < ws.Name Then
intCounter = intCounter + xTest
End If

I ran the macro with these changes and it works as I believe you want it to.
(Revised macro below)

HTH,
Shockley


Sub Tester()
For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If

For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value
intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets
If ws2.Name < ws.Name Then _
intCounter = intCounter + _
Application.WorksheetFunction. _
CountIf(ws2.Columns(1), V)

Next ws2

If intCounter 0 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r

Next ws
End Sub




"jimmy" wrote in message
...
are you serious....great i dont know whats going on, i
copy and pasted that code from the script i have, and it
worked for you? when i step into it, it goes through
twice, like it should, the intCounter is at 15,000
something like it should be. but after it runs through
the loop twice, it exits out of everything...any
clue...here is my entire script, its supposed to delete
duplicate entries across multiple sheets. jouni helped me
write the tough parts...thanks again..

Public Sub DeleteDuplicateRows()
'
' This macro deletes duplicate rows in the selection.
Duplicates are
' counted in the COLUMN of the active cell.

Dim Col As Integer
Dim r As Long
Dim C As Range
Dim N As Long
Dim V As Variant
Dim Rng As Range

Dim ws As Worksheet, ws2 As Worksheet
Dim intCounter As Integer

On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Col = ActiveCell.Column

N = 0

For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If


For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value

intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets

intCounter = intCounter +
Application.WorksheetFunction.CountIf(ws2.Columns( 1), V)

Next ws2

If intCounter 1 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r


Next ws

EndMacro:

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

-----Original Message-----
It runs the If statement for me. Sounds

like "intCounter" is not getting
1 for you.

HTH, Shockley



"jimmy" wrote in message
...
I have the following piece of code that contains 1 for
loop with two imbedded for loops in it. The problem i

am
having is that the deepest for loop (ws2) when finishing
breaks out of both outer for loops (r, ws) and exits to
the end. I am not understanding why. the program

doesnt
even run through to the if statement (intCounter), just
breaks when for loop (ws2) completes. Any

suggestions....

For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If


For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value
intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets
intCounter = intCounter +
Application.WorksheetFunction.CountIf(ws2.Columns( 1), V)
Next ws2

If intCounter 1 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r


Next ws



.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default for loop exiting out early

If you want to remove all duplicates then the condition should be

intCounter 0 rather than
intCounter 1

Also, you are checking each worksheet against itself, so your script will
remove all values on all sheets. You need to increment the counter within
an If statement like this:

If ws2.Name < ws.Name Then
intCounter = intCounter + xTest
End If

I ran the macro with these changes and it works as I believe you want it to.
(Revised macro below)

HTH,
Shockley


Sub Tester()
For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If

For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value
intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets
If ws2.Name < ws.Name Then _
intCounter = intCounter + _
Application.WorksheetFunction. _
CountIf(ws2.Columns(1), V)

Next ws2

If intCounter 0 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r

Next ws
End Sub



"jimmy" wrote in message
...
are you serious....great i dont know whats going on, i
copy and pasted that code from the script i have, and it
worked for you? when i step into it, it goes through
twice, like it should, the intCounter is at 15,000
something like it should be. but after it runs through
the loop twice, it exits out of everything...any
clue...here is my entire script, its supposed to delete
duplicate entries across multiple sheets. jouni helped me
write the tough parts...thanks again..

Public Sub DeleteDuplicateRows()
'
' This macro deletes duplicate rows in the selection.
Duplicates are
' counted in the COLUMN of the active cell.

Dim Col As Integer
Dim r As Long
Dim C As Range
Dim N As Long
Dim V As Variant
Dim Rng As Range

Dim ws As Worksheet, ws2 As Worksheet
Dim intCounter As Integer

On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Col = ActiveCell.Column

N = 0

For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If


For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value

intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets

intCounter = intCounter +
Application.WorksheetFunction.CountIf(ws2.Columns( 1), V)

Next ws2

If intCounter 1 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r


Next ws

EndMacro:

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

-----Original Message-----
It runs the If statement for me. Sounds

like "intCounter" is not getting
1 for you.

HTH, Shockley



"jimmy" wrote in message
...
I have the following piece of code that contains 1 for
loop with two imbedded for loops in it. The problem i

am
having is that the deepest for loop (ws2) when finishing
breaks out of both outer for loops (r, ws) and exits to
the end. I am not understanding why. the program

doesnt
even run through to the if statement (intCounter), just
breaks when for loop (ws2) completes. Any

suggestions....

For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If


For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value
intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets
intCounter = intCounter +
Application.WorksheetFunction.CountIf(ws2.Columns( 1), V)
Next ws2

If intCounter 1 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r


Next ws



.



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
formula help - avtive early j5b9721 Excel Worksheet Functions 0 August 28th 09 04:42 AM
15 minutes early Glenn_H Excel Discussion (Misc queries) 4 January 31st 08 06:44 PM
Early Login, Last Logout junoon Excel Worksheet Functions 3 May 9th 07 04:04 PM
Follow up For loop exiting out early jimmy[_2_] Excel Programming 1 September 26th 03 02:20 AM
Loop ends early BrianB Excel Programming 0 August 15th 03 12:13 PM


All times are GMT +1. The time now is 02:19 PM.

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"