简体   繁体   中英

Excel VBA varaible column selection

I am currently trying to finish a Macro that will pull together a list of all the people into 1 list and show all of the different info for people across all of the lists.

I have gotten to the point where I have all the lists together, and all I need to do now is select the proper data and compile it.

My current code:

Sheets("Sheet2").Select
Columns("E:AI").Select
Selection.Copy
Sheets("Sheet1").Select
Range("E1").Select
ActiveSheet.Paste

I am trying to replace the columns line so that it can select a variable length.

Columns("E").Select
Range(Selection, Selection.End(x1toRight)).Select
Range(Selection, Selection.End(x1Down)).Select
Selection.Copy

When the Macro gets to the first 2nd line in my updated code, I get a run time error '1004': Application-defined or object defined error.

When I go through it line by line, there seems to be an issue with the Columns("E") selecting the same data as it previously had when it was Columns ("E:AI").

Any help would be greatly appreciated.

Not exactly sure what you are trying to do but this code till select E1 -> last column and last row filled.

You need to adapt it to suit your needs. I assumed your table is square, if not change the code.

I counted the rows used in column E, if that is not what you want change that.
I also looked for the last column in row 1. change that if that is incorrect.

LastCol = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column ' here you need to change '1' to what row to look at
LastRow = Cells(Rows.Count, "E").End(xlUp).Row ' change "E" to fit your need
Range("E1:" & Split(Cells(1, LastCol).Address(True, False), "$")(0) & LastRow).Select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM