next empty row

Ossian13

New Member
Joined
Oct 21, 2016
Messages
46
Hello everyone,

can anyone tell me the following:
I have a sheet with a header, and my macro is copying rows to the respective sheet but it keeps pasting over my header as i caould not find a solution to move to the next available empty row.

I have tried this but it has no effect
Code:
nextrow = Sheets("OnlyV").Range("A" & Rows.Count).End(xlUp).Row + 1

THank you,
Ossian
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
That code will find the the next available row in column A, but the question then, is how exactly are you trying to use this calculated value in your pasting code?
You need to post more of your code so we can see how you are trying to use it.
 
Upvote 0
Code:
Range("A1").ActivateActiveCell.EntireRow.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "OnlyV"
ActiveSheet.Paste
Cells.EntireColumn.AutoFit


Application.Worksheets("Sheet1").Activate


    Cells.Find(What:="Michael", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Select
    ActiveCell.EntireRow.Copy
    Application.Worksheets("OnlyV").Activate
   nextrow = Sheets("OnlyV").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
   ActiveSheet.Paste

Here it is. I want every row which contains the name Michael to be copied to the sheet "OnlyV", and everytime on the next row not overwrite over the 1st row.
Hope this helps.
Thanks,
Ossian.
 
Upvote 0
Just as I thought. You are calculating the value of the nextrow, but then not doing anything with it!
Storing the value in a variable does not move or select anything.

Try changing this line:
Code:
nextrow = Sheets("OnlyV").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
to
Code:
Sheets("OnlyV").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
which actually select the cell that you want to paste to.

If you had wanted to use the variable, you could do:
Code:
nextrow = Sheets("OnlyV").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Sheets("OnlyV").Range("A" & nextRow).Activate
but that is an unnecessary extra step.
 
Last edited:
Upvote 0
You could actually simplify the whole code to this:
Code:
    Range("A1").EntireRow.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Name = "OnlyV"
    ActiveSheet.Paste
    Cells.EntireColumn.AutoFit

    Worksheets("Sheet1").Activate
    Cells.Find(What:="Michael", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.EntireRow.Copy Sheets("OnlyV").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
 
Last edited:
Upvote 0
You are welcome!

I hope my explanation of why it didn't work initially makes sense.
 
Upvote 0
I would like to ask one more thing, perhaps you @Joe4 can help me with it.
How do I insert a range of columns where the find function to look only? For example i want the macro to search for the name Michael starting from column AO to column AT and not all over the entire sheet.
Thanks!
 
Upvote 0
Just change the range reference in your Find function:
Code:
[COLOR=#ff0000]Cells[/COLOR].Find(What:="Michael", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
to
Code:
[COLOR=#FF0000]Range("AO:AT")[/COLOR].Find(What:="Michael", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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