I am using below code to copy 4 columns of data from range A:D till the end of that row by checking a condition on B column whether its primary or backup. If primary category code will paste the data into columns K:N, if backup category paste the entire row to P:S columns.
But when executing I am getting Object Defined Error. Can anybody help with whats wrong in this code ? Thanks
Public Sub CopyData()
Dim rngSinglecell As Range
Dim rngQuantityCells As Range
Dim intCount As Integer
Range("K2:S1400").Clear
Set rngQuantityCells = Range("B120", Range("B120").End(xlDown))
For Each rngSinglecell In rngQuantityCells
If rngSinglecell.Value = "Primary" Then
Range("K" & Rows.Count).End(xlUp).Offset(1).Resize(rngSinglecell.Value, 14).Value = _
Range(Range("A" & rngSinglecell.Row), Range("D" & rngSinglecell.Row)).Value
ElseIf rngSinglecell.Value = "Backup" Then
Range("P" & Rows.Count).End(xlUp).Offset(1).Resize(rngSinglecell.Value, 19).Value = _
Range(Range("A" & rngSinglecell.Row), Range("D" & rngSinglecell.Row)).Value
End If
Next
End Sub
Try with Copy :
Public Sub CopyData()
Dim rngSinglecell As Range
Dim rngQuantityCells As Range
Dim intCount As Integer
Range("K2:S1400").Clear
Set rngQuantityCells = Range("B120", Range("B120").End(xlDown))
For Each rngSinglecell In rngQuantityCells
If rngSinglecell.Value = "Primary" Then
Range(Range("A" & rngSinglecell.Row), Range("D" & rngSinglecell.Row)).Copy Range("K" & Rows.Count).End(xlUp).Offset(1)
ElseIf rngSinglecell.Value = "Backup" Then
Range(Range("A" & rngSinglecell.Row), Range("D" & rngSinglecell.Row)).Copy Range("P" & Rows.Count).End(xlUp).Offset(1)
End If
Next
End Sub
No need to resize.
Well, among other things, you're using rngSingleCell.value
in two completely different ways, and one of them isn't working.
rngSingleCell
is the cell that contains Primary/Backup
, correct? But you're trying to use that value in a resize
method, which expects a numeric value. This is definitely going to throw you off.
Of course, without seeing the actual layout of your data it's hard to be sure, but it's not clear why you're using a method a method like Range().end(xlup).offset(1)
to define your copying range; it's going to keep on copying the same data that way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.