How to use VBA to copy from one column to the next blank column?

DayTraderKevin

New Member
Joined
Dec 23, 2022
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hello. I'm pretty new to VBA and would like to copy a range of data from column B, then paste to the next blank column.

In this example, I'd like to copy B2:B4 and paste to C2:C4, then again copy B2:B4 and paste to D2:D4, and so on. There will be existing data in other columns further right. Any advice is appreciated.

ABCDEF
1CompanyStock Price9:009:3010:00Action
2Apple$130Buy
3Microsoft$235Buy
4Tesla$125Sell
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I assumed that there will be further blanks after column F and you want to copy range(B2:B4) there too.
VBA Code:
Sub pope()
        Dim k As Integer
        Dim lr, lc As Long
        lc = Range("C1", Range("C1").End(xlToRight)).Columns.Count
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        
        For k = 1 To lc
                If Range("B2").Offset(0, k) = "" Then
                    Range("B2:B" & lr).Copy Range("B2").Offset(0, k)
                        End If
        Next k
End Sub
 
Upvote 0
I assumed that there will be further blanks after column F and you want to copy range(B2:B4) there too.
VBA Code:
Sub pope()
        Dim k As Integer
        Dim lr, lc As Long
        lc = Range("C1", Range("C1").End(xlToRight)).Columns.Count
        lr = Cells(Rows.Count, 1).End(xlUp).Row
       
        For k = 1 To lc
                If Range("B2").Offset(0, k) = "" Then
                    Range("B2:B" & lr).Copy Range("B2").Offset(0, k)
                        End If
        Next k
End Sub
Thanks for helping! This almost works. This macro is pasting my formula in column B repeatedly to all blank columns to the right. I'd like to paste values only, and only to the first available blank column to the right each time I run the macro.
 
Upvote 0
Welcome to the MrExcel board!

Does this do what you want?

VBA Code:
Sub Copy_Right()
  Range("B2", Range("B" & Rows.Count).End(xlUp)).Copy
  Range("A2").End(xlToRight).Offset(, 1).PasteSpecial xlPasteValues
  Application.CutCopyMode = False
End Sub
 
Upvote 0
Solution
It might also be worth considering using xlPasteValuesAndNumberFormats instead of xlPasteValues.
 
Upvote 0
Welcome to the MrExcel board!

Does this do what you want?

VBA Code:
Sub Copy_Right()
  Range("B2", Range("B" & Rows.Count).End(xlUp)).Copy
  Range("A2").End(xlToRight).Offset(, 1).PasteSpecial xlPasteValues
  Application.CutCopyMode = False
End Sub
Perfect! Thank you!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

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