VBA Paste Special

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,066
Office Version
  1. 365
Platform
  1. Windows
Trying to automate the copying of some rows, fails on the paste special section, and I cant figure out why!!

Code:
Sub test()
Dim LR As Long, i As Long
With Sheets("Analysis")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 12 To LR
        If .Range("K" & i).Value = "CORB01" Then .Rows(i).Copy
    Sheets("Sheet6").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    Sheets("Analysis").Select
                       
    Next i
End With
End Sub

Any help would be great.

Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Your problem is that the paste should only happen if the copy happens.
If K12 does not contain CORB01 then the Copy does not happen. Yet with your code it still tries to paste to the other sheet. Tricky since it hasn't copied anything to paste. :)

Try this. I've made a couple of changes but the critical thing is to get the Paste inside the IF ... End If block.
Code:
Sub test()
Dim LR As Long, i As Long
With Sheets("Analysis")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    For i = 12 To LR
        If .Range("K" & i).Value = "CORB01" Then
          .Rows(i).Copy
          Sheets("Sheet6").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        End If
    Next i
    Application.ScreenUpdating = True
End With
End Sub
 
Upvote 0
Hi,
try updates shown in RED

Rich (BB code):
Sub test()
Dim LR As Long, i As Long
With Sheets("Analysis")
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    For i = 12 To LR
        If .Range("K" & i).Value = "CORB01" Then
            .Rows(i).Copy
    Sheets("Sheet6").Range("A" & Sheets("Sheet6").Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    Application.CutCopyMode = False
       End If
                       
    Next i
End With
End Sub

Dave
 
Upvote 0
I tested it with small amount of lines (and nothing after column K) and it works, so I guess you reach the limit of lines in sheet 6
column A
when you transpose full lines several times
 
Upvote 0
I tested it with small amount of lines (and nothing after column K) and it works, so I guess you reach the limit of lines in sheet 6
column A
when you transpose full lines several times
If you are referring to the original code, try it again with cell K12 of 'Analysis' blank
 
Upvote 0
Thanks all, got it working now!

One other amendment - where it is searching for "CORB01", I need it to copy if it finds "CORB01", "SEVE01" OR "RUGE01".

Is that an easy amendment?

Thanks
 
Upvote 0
Thanks all, got it working now!

One other amendment - where it is searching for "CORB01", I need it to copy if it finds "CORB01", "SEVE01" OR "RUGE01".

Is that an easy amendment?

Thanks


untested but try

Code:
Sub test()
Dim LR As Long, i As Long
With Sheets("Analysis")
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    For i = 12 To LR
        Select Case .Range("K" & i).Value
        Case "CORB01", "SEVE01", "RUGE01"
            .Rows(i).Copy
        Sheets("Sheet6").Range("A" & Sheets("Sheet6").Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        Application.CutCopyMode = False
       End Select
    Next i
End With
End Sub

Dave
 
Upvote 0
If it is only a few values to search for, you could also keep the same structure I posted before with just this one changed line. If it is a largish number of values to search for then the Select Case suggested by Dave would certainly be less cumbersome
Code:
<del>If .Range("K" & i).Value = "CORB01" Then</del>
If InStr(1, "|CORB01|SEVE01|RUGE01|", "|" & .Range("K" & i).Value & "|") > 0 Then
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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