Conditional Copy/Paste Macro

GrahamVincent

New Member
Joined
Mar 3, 2009
Messages
5
Hi all

I currently have some code that copies and pastes rows, a variable number of times dependant on data in Supervisor Name field, to a new sheet.
What I am looking for is a way of adding a cell prior to the pasted data, populated with 'Call 1, Call 2 etc. which references the number of times the row has been copied.

thanks in advance for any help.


Current code achieves the following

Source Data (Sheet 'A')
Employee
Termination Date
Supervisor Name
Teamlead Name
Smith, Bob
Jones, Sue
Brown, Henry
Jones, Sue
Brown, Henry

<tbody>
</tbody>

If there is an entry in Supervisor Name field, this Row is copied 5 times
If there is no entry in Supervisor Name field, this Row is copied 1 time
Which gives this result;

Sheet 'B'
Employee
Termination Date
Supervisor Name
Teamlead Name
Smith, Bob
Jones,Sue
Brown, Henry
Smith, Bob
Jones,Sue
Brown, Henry
Smith, Bob
Jones,Sue
Brown, Henry
Smith, Bob
Jones,Sue
Brown, Henry
Smith, Bob
Jones,Sue
Brown, Henry
Jones, Sue
Brown, Henry

<tbody>
</tbody>



Code needed to provide;
Call Number
Employee
Termination Date
Supervisor Name
Teamlead Name
Call 1
Smith, Bob
Jones,Sue
Brown, Henry
Call 2
Smith, Bob
Jones,Sue
Brown, Henry
Call 3
Smith, Bob
Jones,Sue
Brown, Henry
Call 4
Smith, Bob
Jones,Sue
Brown, Henry
Call 5
Smith, Bob
Jones,Sue
Brown, Henry
Call 1
Jones, Sue
Brown, Henry

<tbody>
</tbody>




Current Code


Code:
Sub CallCalculation()
   ' Macro to copy and paste a variable number of rows dependant on T/L or Supervisor to a new sheet.
     
    Dim rngSinglecell As Range
    Dim rngSupervisorCells As Range
    Dim intCount As Integer
     
     ' This sets the range for the Supervisor column.
    With Worksheets("A")
        Set rngSupervisorCells = .Range("C2", .Range("C2:C1000")) '.End(xlDown))
    End With
    
    For Each rngSinglecell In rngSupervisorCells
         
         ' Checks if Supervisor cell contains a value
        If IsEmpty(rngSinglecell.Value) = False Then
              ' Copy this row 5 times
                For intCount = 1 To 5
                     ' Copy the columns A,B,C into the next empty row in sheet(B)
                    Sheets("B").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(1, 3).Value = rngSinglecell.Offset(0, -2).Resize(1, 3).Value
                     
                     
                Next
                
          ' Checks if Supervisor cell contains a value
        ElseIf IsEmpty(rngSinglecell.Value) = True Then
              ' Copy this row once
                For intCount = 1 To 1
                     ' Copy the columns A,B,C,D into the next empty row in sheet(B)
                    Sheets("B").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(1, 4).Value = rngSinglecell.Offset(0, -2).Resize(1, 4).Value
                     
                                                
                Next
            
            
        End If
               
        
    Next
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
How about
Code:
Sub CallCalculation()
   ' Macro to copy and paste a variable number of rows dependant on T/L or Supervisor to a new sheet.
     
    Dim rngSinglecell As Range
    Dim rngSupervisorCells As Range
    Dim intCount As Integer
     
     ' This sets the range for the Supervisor column.
    With Worksheets("A")
        Set rngSupervisorCells = .Range("C2", .Range("C" & Rows.Count).End(xlUp))
    End With
    
    For Each rngSinglecell In rngSupervisorCells
         
         ' Checks if Supervisor cell contains a value
        If IsEmpty(rngSinglecell.Value) = False Then
                     ' Copy the columns A,B,C into the next empty row in sheet(B)
                    With Sheets("B").Range("A" & Rows.Count).End(xlUp)
                        .Offset(1, 1).Resize(5, 4).Value = rngSinglecell.Offset(0, -2).Resize(1, 4).Value
                        .Offset(1).Value = "Call 1"
                        .Offset(1).AutoFill .Offset(1).Resize(5)
                     End With
                
          ' Checks if Supervisor cell contains a value
        ElseIf IsEmpty(rngSinglecell.Value) = True Then
              ' Copy this row once
                     ' Copy the columns A,B,C,D into the next empty row in sheet(B)
               With Sheets("B").Range("A" & Rows.Count).End(xlUp)
                   .Offset(1, 1).Resize(1, 4).Value = rngSinglecell.Offset(0, -2).Resize(1, 4).Value
                   .Offset(1).Value = "Call 1"
               End With
        End If
    Next
End Sub
 
Upvote 0
Thanks, that's got most of the way there.

It has the same problem with a previous version of my code that stops when the last value in column 'C' is found.
If there are values in column 'A' after this, these are ignored.

I 'fixed' this by having a static range

Code:
[With Worksheets("A")
        Set rngSupervisorCells = .Range("C2", .Range("C2:C1000")) '.End(xlDown))
    End With
/CODE]


Is there a better way than this?
 
Upvote 0
Try this
Code:
Set rngSupervisorCells = .Range("C2", .Range("A" & Rows.Count).End(xlUp).Offset(, 2))
This will look for the last used cell in col A, to determine when to stop.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

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