VBA Code To Copy Data Into Next Blank Column

excelbytes

Active Member
Joined
Dec 11, 2014
Messages
291
Office Version
  1. 365
Platform
  1. Windows
What I am trying to do is copy the data and format from O11:O31 into V11:V31 if V11 is empty. But if there is data in V11, or any consecutive column to the right, find the next blank cell in row 11 the paste the data there.
What is happening is when V11 is empty it works. When V11 is not empty it pastes it in W11, which again is correct. But after that, it keeps pasting the values over in W11.

Here is my code:
VBA Code:
Public Sub NextColumn()

Range("O11:O31").Copy

If IsEmpty(Range("V11")) Then
Range("V11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
Else
Range("V11").Select
ActiveCell.End(xlToRight).Offset(0, 1).Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats

End If

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:
VBA Code:
Public Sub NextColumn()
    Application.ScreenUpdating = False
    Dim lCol As Long
    If Range("V11") = "" Then
        Range("O11:O31").Copy Range("V11")
    Else
        lCol = Cells(11, Columns.Count).End(xlToLeft).Column + 1
        Range("O11:O31").Copy Cells(11, lCol)
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Public Sub NextColumn()
    Application.ScreenUpdating = False
    Dim lCol As Long
    If Range("V11") = "" Then
        Range("O11:O31").Copy Range("V11")
    Else
        lCol = Cells(11, Columns.Count).End(xlToLeft).Column + 1
        Range("O11:O31").Copy Cells(11, lCol)
    End If
    Application.ScreenUpdating = True
End Sub
Almost - it's pasting the formulas from O11:O31. I need it to paste the values and format only.
 
Upvote 0
Try:
VBA Code:
Public Sub NextColumn()
    Application.ScreenUpdating = False
    Dim lCol As Long
    If Range("V11") = "" Then
        Range("O11:O31").Copy
        With Range("V11")
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
        End With
    Else
        lCol = Cells(11, Columns.Count).End(xlToLeft).Column + 1
        Range("O11:O31").Copy
        With Cells(11, lCol)
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
        End With
    End If
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Public Sub NextColumn()
    Application.ScreenUpdating = False
    Dim lCol As Long
    If Range("V11") = "" Then
        Range("O11:O31").Copy
        With Range("V11")
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
        End With
    Else
        lCol = Cells(11, Columns.Count).End(xlToLeft).Column + 1
        Range("O11:O31").Copy
        With Cells(11, lCol)
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
        End With
    End If
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
Thanks. That seems to be working. Do you know why my code didn't work?
 
Upvote 0
You are very welcome. :)
This line of code is looking for data to the right of V11. Since there is no data to the right of V11, an error is generated.
VBA Code:
ActiveCell.End(xlToRight).Offset(0, 1).Select
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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