Hi, niklas!
First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.
As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).
Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.
Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.
And about questions in general...
If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.
And about this question in particular...
Give a look at this file:
https://dl.dropboxusercontent.com/u/60558749/Use%20VBA%20to%20copy%20a%20column%20into%20the%20next%20empty%20column%20%28for%20niklas%20at%20chandoo.org%29.xlsm
It uses 2 dynamic named ranges:
SourceTable: =DESREF(Hoja1!$A$1;;;MIN(10;CONTARA(Hoja1!$A:$A));MIN(1;CONTARA(Hoja1!$1:$1))) -----> in english: =OFFSET(Hoja1!$A$1,,,MIN(10,COUNTA(Hoja1!$A:$A)),MIN(1,COUNTA(Hoja1!$1:$1)))
TargetTable: =DESREF(Hoja2!$A$1;;;CONTARA(Hoja2!$A:$A);CONTARA(Hoja2!$1:$1)+1) -----> in english: =OFFSET(Hoja2!$A$1,,,COUNTA(Hoja2!$A:$A),COUNTA(Hoja2!$1:$1)+1)
If you now that source will be always of 10x1 cells, you could prevent from using MIN functions. Adjust worksheets names properly. There are two versions of the code depending on if you're going to do something else with the data (use version 1) or just copy it (use version 2).
This is the code:
-----
Option Explicit
Sub CopyAtRightmost()
'(*) lines just used for testing purposes, remove them later
' constants
Const ksWSSource = "Hoja1"
Const ksSource = "SourceTable"
Const ksWSTarget = "Hoja2"
Const ksTarget = "TargetTable"
' declarations
Dim rngS As Range, rngT As Range
Static J As Integer '(*)
Dim I As Integer
' start
Set rngS = Worksheets(ksWSSource).Range(ksSource)
Set rngT = Worksheets(ksWSTarget).Range(ksTarget)
J = J + 1 '(*)
Worksheets(3).Cells(1, 1).Value = J '(*)
' process
rngS.Copy rngT.Columns(rngT.Columns.Count)
' end
Set rngT = Nothing
Set rngS = Nothing
Beep
End Sub
Sub CopyAtRightmostShortened()
'(*) lines just used for testing purposes, remove them later
' constants
' declarations
Static J As Integer '(*)
' start
J = J + 1 '(*)
Worksheets(3).Cells(1, 1).Value = J '(*)
' process
Worksheets("Hoja1"
.Range("SourceTable"
.Copy _
Worksheets("Hoja2"
.Range("TargetTable"
.Columns(Worksheets("Hoja2"
.Range("TargetTable"
.Columns.Count)
' end
Beep
End Sub
Sub CopyAtRightmostValues()
'(*) lines just used for testing purposes, remove them later
' constants
Const ksWSSource = "Hoja1"
Const ksSource = "SourceTable"
Const ksWSTarget = "Hoja2"
Const ksTarget = "TargetTable"
' declarations
Dim rngS As Range, rngT As Range
Static J As Integer '(*)
Dim I As Integer
' start
Set rngS = Worksheets(ksWSSource).Range(ksSource)
Set rngT = Worksheets(ksWSTarget).Range(ksTarget)
J = J + 1 '(*)
Worksheets(3).Cells(1, 1).Value = J '(*)
' process
rngS.Copy
Worksheets(ksWSTarget).Select
rngT.Cells(1, rngT.Columns.Count).Select
Selection.PasteSpecial xlPasteValues
Worksheets(ksWSSource).Select
' end
Set rngT = Nothing
Set rngS = Nothing
Beep
End Sub
Sub CopyAtRightmostShortenedValues()
'(*) lines just used for testing purposes, remove them later
' constants
' declarations
Static J As Integer '(*)
' start
J = J + 1 '(*)
Worksheets(3).Cells(1, 1).Value = J '(*)
' process
Worksheets("Hoja1"
.Range("SourceTable"
.Copy
With Worksheets("Hoja2"
.Select
.Range("TargetTable"
.Cells(1, .Range("TargetTable"
.Columns.Count).Select
Selection.PasteSpecial xlPasteValues
End With
Worksheets("Hoja1"
.Select
' end
Beep
End Sub
-----
Just advise if any issue.
Regards!
EDITED