2 sheets with different finalrow variables

ShaunD30

Board Regular
Joined
Jun 19, 2008
Messages
172
Ok...Trying to get a "do until" loop.

I have

Finalrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Finalrow2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

I need my "Do until" loop to run until the activecell in sheet1 =finalrow

I tried "Do until activecell.row=Finalrow", however that didn't work because it ended my macro too soon.

I tried "Do until sheets("Sheet1").Activecell.row=Finalrow
But it won't accept that.

any idea on how I need to word this so it works?

Shaun
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
additional info........

The code I am working on takes a value in say.."G2" on sheet 1..
And searches for all records with that value in sheet2, and pastes those records into sheet 3
Once the code has reached finalrow2 in sheet 2, it is supposed to go back to sheet 1 and offset 1 and continue searching for the next records.

I don't want this loop to end until sheet1 has reached finalrow.
 
Upvote 0
What you need to do fundamentally is to not select/activate cells in your loop. You can loop without selecting like this:

Code:
For i = 1 To LastRow
    If Range("A" & i).Value = 1 Then 'do something
Next i
 
Upvote 0
Yeah I got to thinkin about that after I posted..and I came up with

Code:
Sub GetData()
Sheets("Sheet1").Select
Finalrow = Cells(Rows.Count, 1).End(xlUp).Row
Finalrow2 = Sheets("Sheet4").Cells(Rows.Count, 1).End(xlUp).Row

Range("B2").Select
Curzip = ActiveCell.Value
Do Until ActiveCell.Row = Finalrow
For I = 2 To Finalrow2
Sheets("Sheet4").Select
If Cells(I, 7).Value = Curzip Then
    Cells(I, 2).Copy
    Sheets("Sheet3").Select
    Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Select
        ActiveSheet.Paste
        Else
    End If
    Next I
Sheets("Sheet1").Select
ActiveCell.Offset(1, 0).Select
Curzip = ActiveCell.Value
Loop
End Sub

I think it works..testing now
 
Upvote 0
Does this work?

Code:
Sub GetData()
Dim Finalrow As Long, Finalrow2 As Long, i As Long, j As Long
Dim Curzip As Variant
With Sheets("Sheet1")
    Finalrow = .Cells(Rows.Count, 1).End(xlUp).Row
    Finalrow2 = Sheets("Sheet4").Cells(Rows.Count, 1).End(xlUp).Row
    For j = 2 To Finalrow
        Curzip = .Range("B" & i).Value
        For i = 2 To Finalrow2
            If Sheets("Sheet4").Cells(i, 7).Value = Curzip Then
                Sheets("Sheet4").Cells(i, 2).Copy Destination:=Sheets("Sheet3").Range("A1").End(xlDown).Offset(1)
            End If
        Next i
    Next j
End With
End Sub
 
Upvote 0
I get a

"Run-time error '1004':

Application-defined or object-defined error"

And it highlights

Code:
Curzip = .Range("B" & i).Value

Any ideas? I get your solution..just not sure why it's giving an error here.

Shaun
 
Upvote 0
I fixed..should be "Curzip=.range("b" & j).Value

Thanks VoG! I didn't know you could do multiple for/next like that.

Appreciate your help as always

Shaun
 
Upvote 0
Am getting the same error message for this-

Code:
Sheets("Sheet4").Cells(i, 2).Copy Destination:=Sheets("Sheet3").Range("A1").End(xlDown).Offset(1)
 
Upvote 0
This should work

Code:
Sub GetData()
Dim Finalrow As Long, Finalrow2 As Long, i As Long, j As Long
Dim Curzip As Variant
With Sheets("Sheet1")
    Finalrow = .Cells(Rows.Count, 1).End(xlUp).Row
    Finalrow2 = Sheets("Sheet4").Cells(Rows.Count, 1).End(xlUp).Row
    For j = 2 To Finalrow
        Curzip = .Range("B" & j).Value
        For i = 2 To Finalrow2
            If Sheets("Sheet4").Cells(i, 7).Value = Curzip Then
                Sheets("Sheet4").Cells(i, 2).Copy Destination:=Sheets("Sheet3").Range("A1").End(xlDown).Offset(1)
            End If
        Next i
    Next j
End With
End Sub
 
Upvote 0
Still getting same error with the destination= line.

am running 2003 if that helps.

Thanks

Shaun
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top