Forum

Delete , Cut , Past...
 
Notifications
Clear all

Delete , Cut , Paste with criteria

8 Posts
3 Users
0 Reactions
123 Views
(@tvb)
Posts: 4
Active Member
Topic starter
 

Hello!

When I select any particular entire row and run vba then it should delete

data in that row from B,C,D,E then J,K and S,T,U,V only. This is for delete.

If entire row is not selected then vba first inform to select the row.

 

Same system for Cut and Paste

 

Tried to search a lot on the net but did not get any answer.

If anyone can help me

Thanks

 
Posted : 21/12/2019 5:56 am
(@purfleet)
Posts: 412
Reputable Member
 

Assuming you want to just delete the contents of the cells?

Sub DeleteSelectedInRow()

Dim r As Long

Dim ColsCount As Long

ColsCount = Selection.Columns.Count

Debug.Print ColsCount

If ColsCount <> 16384 Then
MsgBox "Please select entire row before continuing", vbCritical
Exit Sub
Else
End If

r = ActiveCell.Row

Range("b" & r).ClearContents
Range("c" & r).ClearContents
Range("d" & r).ClearContents
Range("e" & r).ClearContents
Range("j" & r).ClearContents
Range("k" & r).ClearContents
Range("s" & r).ClearContents
Range("t" & r).ClearContents
Range("u" & r).ClearContents

End Sub

 

Not sure where you want to paste for the copy and paste one

 
Posted : 21/12/2019 8:00 am
(@tvb)
Posts: 4
Active Member
Topic starter
 

Thanks Purfleet

 

Its working fine for Delete.

For Cut and Paste it is similar.

First select the entire row ( if not selected then message) and then Cut.

After that select the other row and paste special. The cells data will be the same as in Delete.

 

Thanks again

 
Posted : 24/12/2019 12:12 am
(@purfleet)
Posts: 412
Reputable Member
 

Okay, so the below code will copy the cells from the selected row as ask you to type in a destination row number

Sub CutNPasteSelectedRow()

Dim cRow As Long

Dim ColsCount As Long

Dim pRow As Long

ColsCount = Selection.Columns.Count

If ColsCount <> 16384 Then
MsgBox "Please select entire row before continuing", vbCritical
Exit Sub
Else
End If

cRow = ActiveCell.Row

pRow = InputBox("Please select the row to paste the results", vbCritical)

Range("b" & cRow).Cut Range("b" & pRow)
Range("c" & cRow).Cut Range("c" & pRow)
Range("d" & cRow).Cut Range("d" & pRow)
Range("e" & cRow).Cut Range("e" & pRow)
Range("j" & cRow).Cut Range("j" & pRow)
Range("k" & cRow).Cut Range("k" & pRow)
Range("s" & cRow).Cut Range("s" & pRow)
Range("t" & cRow).Cut Range("t" & pRow)
Range("u" & cRow).Cut Range("u" & pRow)

End Sub

 
Posted : 24/12/2019 3:27 am
(@tvb)
Posts: 4
Active Member
Topic starter
 

Thanks a lot Purfleet. Saved a lot of time because of the code.

Only in case of Delete code can it be made for multiple selection. 

However even this is ok.

Since the querry is solved do i need to tick mark the Useful Answer.

 

Thanks Again and Merry Christmas to you and all the other members.

SmileSmileSmile

 
Posted : 25/12/2019 1:42 am
(@purfleet)
Posts: 412
Reputable Member
 

Glad it was okay, could be made more efficent but as long as it works okay it will give you a start.

What is the issue with the delete part you mention 'Only in case of Delete code can it be made for multiple selection.' ?

 
Posted : 26/12/2019 10:18 am
(@tvb)
Posts: 4
Active Member
Topic starter
 

Hello and sorry for delay in reply. Was on leave.

Can this topic be slightly extended more to make the working code more flexible.

As I started working on the above code I came across these issues.

The Delete part where pressing ctrl key I select multiple rows in a sheet and run the code.

The Cut & Paste is working fine since it is required in the same sheet at present.

But if I want to cut from the existing sheet and paste in the row in another sheet named

GENERAL. A separate code if want paste in another sheet.

 

Thanks

Happy New Year.

 
Posted : 05/01/2020 2:37 am
(@davesexcel)
Posts: 6
Active Member
 

Normally you would have the code search a range to find a criteria. If criteria is found, then do something.

 

Attach a sample workbook showing your layout and your desired results.

 
Posted : 05/01/2020 9:18 am
Share: