Excel VBA Copy/Paste overwriting same row

George

So, after roughly 3 hours or more of searching google and for an answer, I can't seem to find an answer that fits my specific case. I've been grappling with a macro, and finally have it mostly working BUT getting it to copy/paste to a new sheet is vexing me to no ends. Here's the copy/paste lines (also the prior copy/paste that I tried to make work before I gave up on it) :

Sub Filtration()

Application.Goto Sheet1.Range("R1")
Application.ScreenUpdating = False

Dim writeRow As Integer
Dim percentage As Double

'to create skip conditions for row 1 & 2
counter = 1
For Each Cell In Sheets(1).Range("R:R")            
    'second part of skip condition
    If counter > 2 Then    
        'creates condition to ignore blank cells or cells with a zero or negative number
        If Cell.Value = "" Or Cell.Value <= 0 Then

        Else
            'creates a way to ignore offset cells if =< 0 (might need to add in for blank too)
            If Cell.Offset(, -2).Value <= 0 Then
                percentage = 0
            Else
                percentage = Cell.Value / Cell.Offset(, -2).Value                            
            End If    
            'divide the current cell's value by the the cell one column over's value and compare
            If percentage > 0.02 Then                
                Set Mastersheet = Worksheets("Sheet1")  ' Copy From this sheet
                Set Pastesheet = Worksheets("Sheet2")  ' to this sheet
                Cell.EntireRow.Copy  ' copy the row from column O that meets that requirements (above, 1 and also win in Q)
                'Pastesheet.Cells(lastRow + 1, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats

                Dim LastRow As Long
                With Pastesheet
                    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row ' get last row in column "A
                    .Cells(LastRow + 1, 1).PasteSpecial xlPasteValuesAndNumberFormats
                End With                
            End If
        End If

    End If

    'final part of skip condition to ignore the two headers - has to be here to work, before next but after the last End IF
    counter = counter + 1        
Next
Application.ScreenUpdating = True

End Sub

The commented copy/paste just errors out on me on the writeRow part and couldn't figure out why, and searching turned up no reasons either. The second half works but just overwrites the same row over and over, and all the answers and examples I found out there claim it should work, so I'm at a loss. Does anyone have any ideas?

Shai Rado

I guess you are after something like the code below (this section of code takes care only for the Paste section):

Dim LastRow As Long
Dim LastCell As Range

With Pastesheet
    ' safer way to get the last row
    Set LastCell = .Cells.Find(What:="*", Lookat:=xlPart, LookIn:=xlFormulas, _
                        searchorder:=xlByRows, searchdirection:=xlPrevious, MatchCase:=False)
    If Not LastCell Is Nothing Then
        LastRow = LastCell.Row
    End If

    .Cells(LastRow + 1, 1).PasteSpecial xlPasteValuesAndNumberFormats
End With

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Overwriting the same row with ajax

VBA - Skipping rows and overwriting the row

VBA Excel - Non-continuous cells in same row, loop by row

How to grab values from different columns in a row and write them to a single cell on a separate row without overwriting with excel vba

Overwriting row in same csv file using dictwriter

Ruby CSV - Write on same row without overwriting?

Why is Excel VBA overwriting these object values

Excel/VBA Summary sheet - overwriting data

Excel VBA -For Next Loop Copies Same Row Multiple Times

VBA Excel - Insert row below with same format including borders and frames

Excel VBA copy string in column A to B in the same row

Excel VBA Repeat same value for 3 row multiple time

Excel Row paste with VBA

Excel VBA inserting row

VBA: Next Empty Row Randomly Overwriting Some Rows

VBA Excel : Delete excel row

Reading from Distinct Ranges into Array and Overwriting to Same Ranges in VBA

Word VBA - CopyPaste Image-header in a new Document

How to find values for 1st cell of same row for a target in Excel VBA?

VBA Excel If some value occur in the column, copy it to another column in the same row

VBA excel: How do I get data in cells as an array up one row in the same column without selecting?

Copy value from same row but other Column (variable offset) Excel VBA

Excel VBA Lookup two values in same row based on values in three columns

Excel VBA: How do I PasteSpecial into same row of Cell location after Cell is changed?

VBA / Excel help: Want to add to a macro to paste formula from a row to the bottom of a range on the same sheet

Range, empty row in Excel vba

finding a row in excel vba issue

Excel VBA copy row automatically

Excel VBA row copy into archive