Macro to Copy and Paste to next blank Column

SaxMan

New Member
Joined
Aug 14, 2013
Messages
1
I am very new to the whole concept of VBA and am having trouble figuring how to copy the contents of three cells and having these contents pasted into three cells in a column. I want a macro that copies the same three cells (B5, B10, B16) and pastes them in E1:E3 the first time it runs, F1:F3 the second time, G1:G3 the third time and so on. I suspect the VBA code will have to include something where it specifies that the values of the three cells need to be pasted in the next available column. I do not have any code so far.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try:
Code:
Sub Test()
For C = 5 To 100 '100 just to test, if your next column is infinite
If Worksheets("Sheet2").Cells(1, C) <> "" Then
C = C + 1
End If
Worksheets("Sheet1").Range("B5").Copy Destination:=Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, C).End(xlUp).Offset(0, 0)
Worksheets("Sheet1").Range("B10").Copy Destination:=Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, C).End(xlUp).Offset(1, 0)
Worksheets("Sheet1").Range("B16").Copy Destination:=Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, C).End(xlUp).Offset(1, 0)
Next C
End Sub
 
Last edited:
Upvote 0
you could try this code.
Rich (BB code):
Sub Saxman()
'for http://www.mrexcel.com/forum/excel-questions/720328-macro-copy-paste-next-blank-column.html
Dim lc As Long
lc = Cells(1, Columns.Count).End(xlToLeft).Column
If lc = 7 Then
    MsgBox ("Macro has been run 3 times already")
    Exit Sub
    Else
    If lc < 5 Then
        lc = 4
        Else:
    End If
End If
lc = lc + 1
Cells(1, lc).Value = Cells(5, 2).Value
Cells(2, lc).Value = Cells(10, 2).Value
Cells(3, lc).Value = Cells(16, 2).Value
End Sub
 
Upvote 0
This might work for you:

Code:
Sub CopyToColumn2()
Dim lLastCol As Long
lLastCol = WorksheetFunction.Max(4, Cells(1, Columns.Count).End(xlToLeft).Offset(, 1).Column)
Cells(1, lLastCol).Resize(3, 1).Value = Application.Transpose(Array(Range("B5"), Range("B10"), Range("B16")))
End Sub
 
Last edited:
Upvote 0
Hi
I was wondering if you can help me.

I have below VBA code that goes through multiple files and copy rang A1-C60 from all the sheets and paste them into a master workbook with one sheet. The macro is ran from that master workbook.

I need to run this macro every month, at the moment The data are pasted in the next available row. So my data are always in columns A to C with data running down.

I want the macro modified so the data are pasted in the next empty column. So the first time the macro is run, the data are pasted in columns A to C, the second time in D to F and so on........

could you help me?

Code:
Option Explicit


' >>>>> Put the initial path where the files to be processed are stored here. _
  End with backslash
Const sInitialPath = "C:\MyPath\"


Sub GetData()
    Dim wbIn As Workbook, wbOut As Workbook
    Dim rIn As Range, rOut As Range
    Dim wsIn As Worksheet, wsOut As Worksheet
    Dim diaFolder As FileDialog
    Dim lCount As Long


    Set wbOut = ThisWorkbook
    ' Assuming masterWB has only one sheet
    Set wsOut = wbOut.Sheets(1)
    
        '   get file name for file to process
    MsgBox "Select all the files you want to process by using the Ctrl key and the mouse. "

    ' Open the file dialog to get the  files
    Set diaFolder = Application.FileDialog(msoFileDialogFilePicker)
    With diaFolder
        .AllowMultiSelect = True
        .InitialView = msoFileDialogViewList
        .InitialFileName = sInitialPath
        lCount = .Show
    End With
    If lCount = -1 Then
    ' for each selected file
    For lCount = 1 To diaFolder.SelectedItems.Count
        
        Set wbIn = Workbooks.Open(diaFolder.SelectedItems(lCount))

        'loop through all the sheets in the opened book
        For Each wsIn In wbIn.Sheets
            'set output range on the Mastersheet to last row
            Set rOut = wsOut.Cells(wsOut.Rows.Count, 1).End(xlUp).Offset(1, 0)
            'now copy the values accross to the Mastersheet
            With wsIn.Range("A1:C60")
                rOut.Resize(.Rows.Count, .Columns.Count).Value = .Value
            End With
        Next wsIn
        'close WB
        wbIn.Close savechanges:=False
    Next lCount
    End If
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True

    'Cleanup
    Set wbIn = Nothing
    Set wbOut = Nothing
    Set rIn = Nothing
    Set rOut = Nothing
    Set wsIn = Nothing
    Set wsOut = Nothing
    Set diaFolder = Nothing
End Sub
 
Upvote 0
Hi
I was wondering if you can help me.

I have below VBA code that goes through multiple files and copy rang A1-C60 from all the sheets and paste them into a master workbook with one sheet. The macro is ran from that master workbook.

I need to run this macro every month, at the moment The data are pasted in the next available row. So my data are always in columns A to C with data running down.

I want the macro modified so the data are pasted in the next empty column. So the first time the macro is run, the data are pasted in columns A to C, the second time in D to F and so on........

could you help me?

Duplicates:

http://www.mrexcel.com/forum/excel-...paste-next-empty-cell-column.html#post4177977
http://www.mrexcel.com/forum/excel-...-paste-into-next-open-column.html#post4177976
http://www.mrexcel.com/forum/excel-...copy-paste-next-blank-column.html#post4177975
http://www.mrexcel.com/forum/excel-...-next-empty-cell-other-sheet.html#post4177972
http://www.mrexcel.com/forum/excel-questions/584509-pasting-next-blank-row-visual-basic-applications.html#post4177970


Forum Rules
21.) Avoid multiple questions of a similar nature. Duplicate posts by the same user will be locked and/or deleted when found.
 
Upvote 0

Forum statistics

Threads
1,223,534
Messages
6,172,891
Members
452,487
Latest member
ISOmark26

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