Finding last row VBA

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
Hi all,

I'm currently having an issue with the below vba - The below works perfectly up until the below line;

VBA Code:
            Cells(last_row_Active, 2).PasteSpecial xlPasteValuesAndNumberFormats

In want the vba to loop through data in sheet2 and copy rows with the text "Outstanding" and paste the result onto sheet5. The issue i'm having is that the results found from sheet2 are copied and pasted onto sheet5 but in the same row (So it's overwriting the previous result). If i then run the macro again, the above line finds the next empty row but still pastes all results in the same row once again. Here's the full code;

VBA Code:
Sub Create_Report()

    Dim i, last_row_Data, last_row_Active As Integer
    Dim Records As Long

    Records = Sheet1.Range("N15").Value
    last_row_Data = Application.WorksheetFunction.CountA(Sheet2.Range("A:A")) + 1 'Finds last row on 'All-Data' tab
    last_row_Active = Application.WorksheetFunction.CountA(Sheet5.Range("B:B")) + 8 'Finds last row on 'Report' tab
    
    For i = 2 To last_row_Data
        If Sheet2.Range("M" & i).Text = "Outstanding" Then
            Records = Records + 1
            Sheet2.Activate
            rng = "B" & i & ", C" & i & ", E" & i & ", H" & i & ", I" & i & ", N" & i
            Sheet2.Range(rng).Copy
            Sheet5.Activate
            Cells(last_row_Active, 2).PasteSpecial xlPasteValuesAndNumberFormats
        End If
     Next i
    
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,860
Office Version
  1. 2010
Platform
  1. Windows
change this line:
VBA Code:
Cells(last_row_Active, 2).PasteSpecial xlPasteValuesAndNumberFormats
to
VBA Code:
Cells(last_row_Active, 2).PasteSpecial xlPasteValuesAndNumberFormats
last_row_Active=last_row_Active+1
 

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
change this line:
VBA Code:
Cells(last_row_Active, 2).PasteSpecial xlPasteValuesAndNumberFormats
to
VBA Code:
Cells(last_row_Active, 2).PasteSpecial xlPasteValuesAndNumberFormats
last_row_Active=last_row_Active+1
Lovely! - Works perfect, thanks :)
 

Watch MrExcel Video

Forum statistics

Threads
1,133,271
Messages
5,657,764
Members
418,411
Latest member
Excellency

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
Top