sort cut paste macro

Tonyk1051

Board Regular
Joined
Feb 1, 2021
Messages
132
Office Version
  1. 2019
Platform
  1. Windows
Hi,

i tried recording a macro of me just sorting cutting and pasting...didnt quite work well (the cut and paste part) when i tested the macro out -__-

There will be text in column H, (in tab1) i go to data, check the box to use my headers and sort by column H which brings up everything to the top, any line that has a text/anything at all in column H, i cut and paste to line A2 to tab 4 (cat tab) and then delete the empty space from tab 1

when i repeat the process, the 2nd batch i try to cut and paste over to tab 4, it covers the original and brings in lines that dont have anything in column H...which i dont want. so for example lets say i did cut and paste to tab 4, 20 lines. the 2nd batch should be pasted onto line A21 not line A2

the amount of data always varies, sometimes theres 2 lines that have something in column H sometimes its a couple 100...
sorry if this is confusing


tester.xlsm tester file
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I am unclear as to what needs to happen to the data in the original sheet.
ie Cut and Paste indicates those rows should be removed but it sounds like you may not want the other remaining rows either.
I have given 2 options towards the end of the code but both are currently commented out.

On a copy of your workbook, see if this does what you are after.
PS: your cat sheet has an additional blank column at column Y, I have assumed that is an error and ignored it.

VBA Code:
Sub CopyPaste()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim srcLastRow As Long, destLastRow As Long, srcLastCopyRow As Long
    Dim srcLastCol As Long
    Dim srcRng As Range, destRng As Range, srcCopyRng As Range
   
   
    Set srcSht = Worksheets("Not on a Category")
    With srcSht
        srcLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        If srcLastRow = 1 Then
            MsgBox "No Date in Source Sheet"
            Exit Sub
        End If
       
        srcLastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        Set srcRng = .Range(.Cells(1, "A"), .Cells(srcLastRow, srcLastCol))
    End With
   
    Set destSht = Worksheets("cat")
    With destSht
        destLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        Set destRng = .Range("A" & destLastRow)
    End With
   
    Application.ScreenUpdating = False
   
    srcSht.Sort.SortFields.Clear
    srcRng.Sort Key1:=srcSht.Range("H2"), _
         Order1:=xlAscending, Header:=xlYes
    srcLastCopyRow = srcRng.Cells(srcRng.Rows.Count, "H").End(xlUp).Row
   
    If srcLastCopyRow = 1 Then
        MsgBox "Nothing to Copy"
        GoTo CleanExit
    End If
    Set srcCopyRng = srcRng.Resize(srcLastCopyRow - 1).Offset(1)
   
    srcCopyRng.Copy Destination:=destRng.Offset(1)
   
   
    ' XXX Optional determine what to do with initial data
    ' Option 1 - Delete copied rows
    ' srcCopyRng.EntireRow.Delete
   
    ' Option 2 - Clear all data
    ' srcRng.Offset(1).ClearContents

CleanExit:
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
I am unclear as to what needs to happen to the data in the original sheet.
ie Cut and Paste indicates those rows should be removed but it sounds like you may not want the other remaining rows either.
I have given 2 options towards the end of the code but both are currently commented out.

On a copy of your workbook, see if this does what you are after.
PS: your cat sheet has an additional blank column at column Y, I have assumed that is an error and ignored it.

VBA Code:
Sub CopyPaste()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim srcLastRow As Long, destLastRow As Long, srcLastCopyRow As Long
    Dim srcLastCol As Long
    Dim srcRng As Range, destRng As Range, srcCopyRng As Range
  
  
    Set srcSht = Worksheets("Not on a Category")
    With srcSht
        srcLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        If srcLastRow = 1 Then
            MsgBox "No Date in Source Sheet"
            Exit Sub
        End If
      
        srcLastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        Set srcRng = .Range(.Cells(1, "A"), .Cells(srcLastRow, srcLastCol))
    End With
  
    Set destSht = Worksheets("cat")
    With destSht
        destLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        Set destRng = .Range("A" & destLastRow)
    End With
  
    Application.ScreenUpdating = False
  
    srcSht.Sort.SortFields.Clear
    srcRng.Sort Key1:=srcSht.Range("H2"), _
         Order1:=xlAscending, Header:=xlYes
    srcLastCopyRow = srcRng.Cells(srcRng.Rows.Count, "H").End(xlUp).Row
  
    If srcLastCopyRow = 1 Then
        MsgBox "Nothing to Copy"
        GoTo CleanExit
    End If
    Set srcCopyRng = srcRng.Resize(srcLastCopyRow - 1).Offset(1)
  
    srcCopyRng.Copy Destination:=destRng.Offset(1)
  
  
    ' XXX Optional determine what to do with initial data
    ' Option 1 - Delete copied rows
    ' srcCopyRng.EntireRow.Delete
  
    ' Option 2 - Clear all data
    ' srcRng.Offset(1).ClearContents

CleanExit:
    Application.ScreenUpdating = True

End Sub
Hi, yes column y is mistake, apologies i should have deleted that. i tried both codes, they both do as intended except for one thing...it didnt cut and paste, it copied the data. once the data is cut and pasted to cat tab i manually go back to tab one and delete the empty space/lines
 
Upvote 0
technically i dont need cut and paste, i just need to make sure there no duplicate data in the workback. so if the macro is just copy and pasting the data to cat tab. so long as what is copied in tab one is deleted and the copy of it is in cat tab, thats fine also. whichever makes it easier for you to create
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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