I’m new to VBA and I’m trying this for loop out, but I think my “To” counter is wrong. I want it to perform the specified actions until there are no more columns left to cut.
What is happening is, I have a horizontal set of data(Column “T” to “EV”) that I am cutting and pasting in order vertically on column S. It works, but I get this error still:
I get:
Run-time error ‘1004’:
This selection is not valid.
There are several possible reasons:
- Copy and paste areas cannot overlap unless they’re the same size and shape.
- If you are using the Create from Selection command, the row or column containing the proposed names won’t be included in the(cuts off here)
Code:
Dim x As Integer
For x = 0 To ActiveCell.CurrentRegion.Columns.Count
ActiveCell.Select
ActiveCell.Offset(0, x).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Range("S3000").Select
ActiveCell.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Range("T11").Select
Next x
Not tested, written directly here, but it should work…
dim c as range
for each c in range("t1").currentregion
range("s3000").end(xlup).offset(1,0)= c.value
c.clear
next c
There’s no point changing the active cell. It just slows down your code.
Answer:
You could just use the built-in TRANSPOSE worksheet function to achieve this – see here
If you want to achieve this using VBA then, as Tiago Cardoso pointed out, using cut and paste is generally not the best solution. Consider using the Cells
method of the Worksheet
object and keeping track of which column and which row you are working with.