Searching for data in column, if found Cut it paste on other sheet

Theglyde

New Member
Joined
May 29, 2020
Messages
34
Office Version
  1. 365
Platform
  1. Windows
I want to search Column "b" for "98" and if found (could be multiples line) I want those lines to be cut and pasted in a new sheet called "Diesel Discount"
This is what I got so far. If it Highlighs the first End With as error...

VBA Code:
Sub Move_Diesel_Discount()
Dim LR As Long, i As Long
With Sheets("Discount Sheet")
LR = .Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With .Range("B" & i)
If .Value = "98" Then
.EntireRow.Cut Destination:=Sheets("Diesel Discount").Range("A" & Row.Count).End(xlUp).Offset(1)
End With
Next i
End With
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You got a few errors, such as missing the "End If" line and typing "Row.Count" instead of "Rows.Count".
And if 98 is an actual numeric value, you do NOT want double-quotes around it. Double-quotes are used to denote text entries, not numeric ones.

Try this updated code:
VBA Code:
Sub Move_Diesel_Discount()

    Dim LR As Long, i As Long

    With Sheets("Discount Sheet")
        LR = .Range("B" & .Rows.Count).End(xlUp).Row
        For i = 1 To LR
            With .Range("B" & i)
                If .Value = 98 Then
                    .EntireRow.Cut Destination:=Sheets("Diesel Discount").Range("A" & Rows.Count).End(xlUp).Offset(1)
                End If
            End With
        Next i
    End With
    
End Sub
 
Upvote 0
I did all the changes removed the S removed added the End if thank you, it is now coming back to
LR = .Range("B" & .Row.Count).End(xlUp).Row
comes back Highlighted

I put images under, first is the raw data, then macro button sheet, when I click it it automatically deletes unwanted rows and pastes good data in "discount sheet"

from there you see the error in last picture that should transfer all rows that has 98 in column b to a new page called Diesel Discount but there is a bug to it

Thanks again for your help

VBA Code:
Sub Move_Diesel_Discount()

    Dim LR As Long, i As Long

    With Sheets("Discount Sheet")
        LR = .Range("B" & .Row.Count).End(xlUp).Row
        For i = 1 To LR
            With .Range("B" & i)
                If .Value = 98 Then
                    .EntireRow.Cut Destination:=Sheets("Diesel Discount").Range("A" & Rows.Count).End(xlUp).Offset(1)
                End If
            End With
        Next i
    End With
  
End Sub
[ATTACH type="full"]99841[/ATTACH]
[ATTACH type="full"]99841[/ATTACH]
macro button.png


discount sheet.png
code error.png
 

Attachments

  • table.png
    table.png
    103.6 KB · Views: 9
Last edited:
Upvote 0
Why didn't you copy/paste my code "as-is"?
It looks like you tried to type it, and missed one of the errors I told you about.
You got a few errors, such as missing the "End If" line and typing "Row.Count" instead of "Rows.Count".

Your line of code:
Rich (BB code):
LR = .Range("B" & .Row.Count).End(xlUp).Row
My line of code:
Rich (BB code):
LR = .Range("B" & .Rows.Count).End(xlUp).Row
See the difference?

My advice is to ALWAYS use Copy/Paste, if someone has posted code for you.
It avoids any typos.
 
Upvote 0
I did remove the S in both rows.count even if images show them. I have the same error, sorry about that I removed the first and then took the images and realized I did same on both and corrected other but forgot to redo pics. But in the end I have the same result.
 
Upvote 0
I totally misread. Having a real bad week and I apologize on that. Here is image of your code exactly as it was
Error .png
 
Upvote 0
I did remove the S in both rows.count even if images show them. I have the same error, sorry about that I removed the first and then took the images and realized I did same on both and corrected other but forgot to redo pics. But in the end I have the same result.
No, you don't need to remove them, you need to add them back in!

Please copy/paste the code, EXACTLY as I have typed in post 2 (make no edits!) and try that.
If you get any error messages, tell us exactly what the error message says.
 
Upvote 0
No, you don't need to remove them, you need to add them back in!

Please copy/paste the code, EXACTLY as I have typed in post 2 (make no edits!) and try that.
If you get any error messages, tell us exactly what the error message says.
yes I did and replied just above yours, again sorry about that. Still getting an error
 
Upvote 0
OK, slow down a little, and carefully read my replies so you see what I am asking, and can answer my questions (help me to help you!).
If you get any error messages, tell us exactly what the error message says.
 
Upvote 0
OK, slow down a little, and carefully read my replies so you see what I am asking, and can answer my questions (help me to help you!).
yes, I have copy pasted exactly your post 2 and the last image I posted is the error it came back as
which I will attach here again
Error .png
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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