VBA Code in Excel is Overwriting Previous Data Put on Worksheet

Jeffea

New Member
Joined
Aug 13, 2014
Messages
41
Hello,

I am kind of a beginner with VBA code and I would really appreciate some help with a problem I am having. I need code that will look for rows with a value of 25411013 or 25411033 and put them on Sheet2 UNLESS a value of BAS is in the same row.

I have written two sets of code for each. The first set is to take rows with a value of 25411013 and put on another worksheet as long as there is not a value of BAS in the same row. Then I added some code to delete rows with a value of 25411013 from the source worksheet (sheet1 for example). I did the same thing for 25411033.

I am wondering if there is some code that I could use that would look for 25411013 OR 25411033 AND does not have a value of BAS in the same row and put it on sheet2 for example?

The problem I am having now is the rows with a data value of 25411033 are overwriting the 25411013 row information on Sheet2 starting from cell A2 down.


Sample Code:

MR = Cells(Rows.Count, "E").End(xlUp).Row<o:p></o:p>

WithRange("E1:F" & MR)<o:p></o:p>
.AutoFilter<o:p></o:p>
.AutoFilterField:=2, Criteria1:="25411013"<o:p></o:p>
.AutoFilterField:=1, Criteria1:="<>BAS"<o:p></o:p>
Range("E2:F" & MR).SpecialCells(xlCellTypeVisible).EntireRow.CopyDestination:=Sheets("FED 457B").Range("A2")<o:p></o:p>
.AutoFilter<o:p></o:p>
End With<o:p></o:p>
<o:p> </o:p>
Dim LastrowD As Long<o:p></o:p>
Application.ScreenUpdating= False<o:p></o:p>
LastrowD =[E65536].End(xlUp).Row<o:p></o:p>
For e = LastrowD To 1Step -1<o:p></o:p>
If Cells(e, 6) ="25411013" Then Rows(e & ":" & e).EntireRow.Delete<o:p></o:p>
Next e<o:p></o:p>
<o:p> </o:p>
MR1 = Cells(Rows.Count, "E").End(xlUp).Row<o:p></o:p>
WithRange("E1:F" & MR1)<o:p></o:p>
.AutoFilter<o:p></o:p>
.AutoFilterField:=2, Criteria1:="25411033"<o:p></o:p>
.AutoFilterField:=1, Criteria1:="<>BAS"<o:p></o:p>
Range("E2:F" & MR1).SpecialCells(xlCellTypeVisible).EntireRow.CopyDestination:=Sheets("FED 457B").Range("A2")<o:p></o:p>
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Your 2nd code is a mirror of the 1st..
Is this what you are after... UNTESTED
Code:
Sub MM1()
Dim MR As Long
MR = Cells(Rows.Count, "E").End(xlUp).Row
    With Range("E1:F" & MR)
        .AutoFilter
        .AutoFilter Field:=2, Criteria1:="25411013"
        .AutoFilter Field:=1, Criteria1:="<>BAS"
        .SpecialCells(xlCellTypeVisible).EntireRow.Copy Destination:=Sheets("FED 457B").Range("A2")
        .SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With
End Sub
 
Upvote 0
Hello.. thank you for your help. I will try your code shortly. I could have worded my question better. After I thought about it some more I realized that I basically want the data for 25411033 to be put in the first empty cell on sheet 2 for example.. "which would be after the 25411013 data was copied on sheet 2."
 
Upvote 0
Thank you Michael.. the code you gave me worked. I just need some help with the syntax for writing code to place data from one sheet to another sheet on the first blank row. I get an error message of "object not defined" concerning the code in BOLD.

Dim NextRow As Range
Set NextRow = Sheets("Sheet5").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
MR1 = Cells(Rows.Count, "E").End(xlUp).Row
With Range("E1:F" & MR1)
.AutoFilter
.AutoFilter Field:=2, Criteria1:="25411033"
.AutoFilter Field:=1, Criteria1:="<>BAS"
Range("E2:F" & MR1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Destination:=Sheets("Sheet5").Range("A2:A & NextRow")
.AutoFilter
End With
 
Upvote 0
Michael...

I got it. I found the code I needed. Thank you again for your help.

Here is the winning code! Just in case it may be helpful for someone else...
Dim NextRow As Range
Set NextRow = Sheets("Sheet5").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
MR1 = Cells(Rows.Count, "E").End(xlUp).Row
With Range("E1:F" & MR1)
.AutoFilter
.AutoFilter Field:=2, Criteria1:="25411033"
.AutoFilter Field:=1, Criteria1:="<>BAS"
Range("E2:F" & MR1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Destination:=Sheets("Sheet5").Range("A" & Rows.Count).End(xlUp).Offset(1)
.AutoFilter
End With
 
Upvote 0

Forum statistics

Threads
1,216,114
Messages
6,128,913
Members
449,478
Latest member
Davenil

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