Looping


Posted by Sid on October 18, 2001 12:42 AM

Hi, I am trying to condense the code below by looping the if statement. Also I need to return to the active cell on the sheet that calls this macro

Mysearch ()
Dim Sht As Worksheet
Dim sCurrentSheet As String
sCurrentSheet = ActiveSheet.Name
For Each Sht In Application.Worksheets
Sht.Activate
sCurrentSheet = ActiveSheet.Name

FinalRow = Range("b65536").End(xlUp).Row

For x = 3 To FinalRow

-values defined here-
If -numerous value definitions here-And positionvalue = "A**" Then
Range("b" & x).Copy
Sheets("positions").Select
NextRow = Range("a**65536").End(xlUp).Row + 1
Range("A**" & NextRow).Select
ActiveSheet.Paste
Sheets(sCurrentSheet).Select
End If

If -repeat above with **changed to B,
etc
Next x


End If
Next Sht
-Code to go to inital sheet/activecell that called this sub
End Sub

The loop would need to change the letters marked ** above from A thro' to Z

Thanx Sid

Posted by Mark O'Brien on October 18, 2001 4:56 AM

For x = 3 To FinalRow -values defined here- End If

Sid,

I think I understand what you are needing. I would suggest using the "offset" command in your loop.

Here is a quick example, you should be able to incorporate it into you code.

It will go through cells A1 to C10 putting in a number. It saves you having to change the A to a B and the B to a C etc etc.


Public Sub OffsetExample()

Dim i As Integer
Dim j As Integer
Dim FirstCell As Range

Set FirstCell = Sheets("Sheet1").Range("A1")

For i = 0 To 9 'Row count
For j = 0 To 2 'Col count
FirstCell.Offset(i, j).Value = i * j
Next
Next

End Sub

If you need any help incorporating this into your code just repost.

Posted by sid on October 18, 2001 8:27 AM

For x = 3 To FinalRow -values defined here- End If


Mark,
don't know how i could apply your suggestion, how can i loop thro values A to Z.
It has to be applied to this code as this is copying/pasting values across worksheets

so i am guessing something like

for i = A to Z

If positionvalue = "(i)" Then 'positionvalue is defined from column c:c holding values between A & Z
Range("b" & x).Copy
Sheets("positions").Select
NextRow = Range("(i)65536").End(xlUp).Row + 1
Range("(i)" & NextRow).Select
ActiveSheet.Paste
Sheets(sCurrentSheet).Select
Next i



Posted by Mark O'Brien on October 18, 2001 9:02 AM

Sid try something like this:

Dim i, j As Integer
Dim FirstRange As Range
Dim FinalRow

FinalRow = Range("b65536").End(xlUp).Row

Set FirstRange = Range("A65536")

For i = 1 To FinalRow
For j = 0 To 25 'count for A to Z
'....You're "if..then" conditions, copy etc etc

NextRow = FirstRange.Offset(0, j).End(xlUp).Row + 1 'go to next column
Range((j + 1), NextRow).Select ' Use "Cells" instead of range. Cells(1,1) = Range("A1")


'...the rest of your code
Next
Next

Main points are:

1. Declare the first range that you're going to use to get the NextRow. i.e. "A65536" Then use Offset to change it to "B65536" etc etc.

2. Use "Cells" instead of "Range" this allows you to use numerical referencing instead of "A1" style.