简体   繁体   English

Excel VBA可变列选择

[英]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. 我目前正在尝试完成一个宏,该宏会将所有人员的列表汇总到1个列表中,并显示所有列表中人员的所有不同信息。

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. 当宏到达更新后的代码的第一行时,出现运行时错误'1004':应用程序定义或对象定义的错误。

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"). 当我逐行浏览时,Columns(“ E”)选择的数据与以前是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. 不完全确定您要执行的操作,但是此代码直到选择E1->最后一列和最后一行已填充。

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. 我计算了E列中使用的行,如果那不是您想要的,请更改该行。
I also looked for the last column in row 1. change that if that is incorrect. 我还寻找了第1行的最后一列。如果那是不正确的,请进行更改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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