Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA For loop until column is empty problem

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
like image 378
Leon Avatar asked Dec 31 '25 20:12

Leon


1 Answers

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.

like image 75
iDevlop Avatar answered Jan 02 '26 14:01

iDevlop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!