Copying data from one sheet, pasting into another then based on criteria in that sheet pasting other data into new sheet

blackmamba89

New Member
Joined
Jun 28, 2022
Messages
8
Office Version
  1. 2019
Platform
  1. MacOS
Hello, to give a brief run down: In Sheet1, ColumnA, Row 2 - the values are copied and pasted, then sorted alphabetically into Sheet2, ColumnB, Row 3. The code below reflects this. My next problem is that I want Sheet 1, ColumnB, Row2 to paste into Sheet2, ColumnE, Row3 based on the conditions within Sheet1, ColumnA. The images below hopefully helps. Each name is next to a category (H, H/R, H/R/I). When I paste into the new sheet, these categories are sorted alphabetically and I want the names to still match their categories (NOT BE SORTED) if that makes sense. Hopefully each image below helps visualize what I'm trying to do. The 1st image is what I start with and the 2nd is what I want the result to be. Feel free to change any code you deem necessary. This is just my attemp but the if else statements are not working. Any help will be greatly appreciated.
Screen Shot 2022-06-27 at 2.20.27 PM.png Screen Shot 2022-06-28 at 12.58.40 AM.png


VBA Code:
Private Sub Button1_Click()
    
'Declaration of variable
lastrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    ' 1. Define constants.
    
    ' Source
    Const sName As String = "Sheet1"
    Const sCol As String = "a"
    Const sfRow As Long = 2
    ' Destination
    Const dName As String = "Sheet2"
    Const dCol As String = "b"
    Const dfRow As Long = 3
    ' Other
    Const Msg As String = "Copied column sorted."
    
    ' 2. Reference the workbook ('wb')
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' 3. Reference the source range ('srg').
    
    ' Reference the source worksheet ('sws').
    Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
    ' Reference the source first cell ('sfCell').
    Dim sfCell As Range: Set sfCell = sws.Cells(sfRow, sCol)
    ' Using '.End(xlUp)', reference the source last cell ('slCell').
    Dim slCell As Range: Set slCell = sws.Cells(sws.Rows.Count, sCol).End(xlUp)
    ' Using the source first and last cells, reference the source range.
    Dim srg As Range: Set srg = sws.Range(sfCell, slCell)
    
    ' 4. Reference the destination range ('drg').
    
    ' Reference the destination worksheet ('dws').
    Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
    ' Reference the destination first cell ('dfCell')..
    Dim dfCell As Range: Set dfCell = dws.Cells(dfRow, dCol)
    ' Using '.Resize' and the number of rows of the source range
    ' ('srg.rows.count') on the destination first cell,
    ' make the destination range the same size as the source range.
    Dim drg As Range: Set drg = dfCell.Resize(srg.Rows.Count)
    
    ' 5. Copy the values from the source range to the destination range.
    ' This is the most efficient way to copy values and is called
    ' 'Copying by Assignment'.
    drg.Value = srg.Value
    
    ' 6. Sort the destination range.
    drg.Sort drg, xlAscending, , , , , , xlNo
    
    ' 7. Inform so you don't have to worry if you have clicked the button.
    MsgBox Msg, vbInformation
    
    For i = 2 To lastrow
    
        'Conditional if statement that copies "H" in Sheet 1, Column A and pastes in Sheet 2, Column B
        If Worksheets("Sheet1").Range("A" & i).Value = "H" Then
            Worksheets("Sheet1").Range("B" & i).Copy
            
            Worksheets("Sheet2").Activate
            lastrow2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
            
            Worksheets("Sheet2").Range("E" & i + 1).Select
                        
            ActiveSheet.Paste
            'Worksheets("Sheet2").Range("B" & i + 1).Interior.Color = vbCyan
            'Worksheets("Sheet2").Range("B" & i).Borders.Color = rbgBlack
    
        'Conditional if statement that copies "H/R" in Sheet 1, Column A and pastes in Sheet 2, Column B
        Else: Worksheets("Sheet1").Range("A" & i).Value = "H/R"
            Worksheets("Sheet1").Range("B" & i).Copy
            
            Worksheets("Sheet2").Activate
            lastrow2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
            
            Worksheets("Sheet2").Range("E" & i + 1).Select
                        
            ActiveSheet.Paste
            'Worksheets("Sheet2").Range("B" & i + 1).Interior.Color = vbCyan
            'Worksheets("Sheet2").Range("B" & i).Borders.Color = rbgBlack
        
        'Conditional if statement that copies "H/R/I" in Sheet 1, Column A and pastes in Sheet 2, Column B
        Else: Worksheets("Sheet1").Range("A" & i).Value = "H/R/I"
            Worksheets("Sheet1").Range("B" & i).Copy
            
            Worksheets("Sheet2").Activate
            lastrow2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
            
            Worksheets("Sheet2").Range("E" & i + 1).Select
                        
            ActiveSheet.Paste
            'Worksheets("Sheet2").Range("B" & i + 1).Interior.Color = vbCyan
            'Worksheets("Sheet2").Range("B" & i).Borders.Color = rbgBlack
        
        'Deletes empty cells and shifts "" upward
        Else: Worksheets("Sheet2").Range("E" & i).Value = ""
            Columns("E:E").Select
            Selection.SpecialCells(xlCellTypeBlanks).Select
            Selection.Delete Shift:=xlUp
            
        End If
      
    Next i
    
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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