简体   繁体   English

创建表但首先找到最后一列

[英]create table but first finding the last column

I have a project where I pull excel data into my spreadsheet with copy and paste.我有一个项目,我通过复制和粘贴将 excel 数据拉到我的电子表格中。 I then have to create charts for each category which varies anywhere from two columns to 25 columns.然后,我必须为从两列到 25 列不等的每个类别创建图表。 In creating the charts I make a "Table" of the data.在创建图表时,我制作了数据的“表格”。 Because the number of columns can vastly vary I am trying to use end.(xltoleft) reading that address.因为列数可能有很大差异,所以我尝试使用 end.(xltoleft) 读取该地址。 For some reason, I can not add the string in the ListObjects.add(xlSrcRange. I get Method Range of object _Global failed.由于某种原因,我无法在 ListObjects.add(xlSrcRange 中添加字符串。我得到 object _Global 的方法范围失败。

 last_column = Worksheets("SelectFile").Cells(33, Columns.Count).End(xlToLeft).Column
    Dim LastCol As String
    LastCol = Range("BA33").End(xlToLeft).Address
    Range("A34").Select
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$33:&LastCol"), , xlYes).Name = "Table1"
    Range("Table1[#All]").Select
    ActiveWindow.SmallScroll Down:=-51

Your issue is how your Range reference is written.您的问题是您的 Range 参考是如何编写的。

Currently you have Range("$A$33:&LastCol") which is literally Range("$A$33:&LastCol") .目前你有Range("$A$33:&LastCol") ,它的字面意思是Range("$A$33:&LastCol")

Move the quotes like so: Range("$A$33:" & LastCol)像这样移动引号: Range("$A$33:" & LastCol)

The cell reference is enclosed in quotes as a string up until the colon and then concatenated with your LastCol variable.单元格引用作为字符串括在引号中,直到冒号,然后与您的LastCol变量连接。

If your variable LastCol represents say, Cell address $AA$33 , the above example would translate to Range(":$A$33:$AA$33") .如果您的变量LastCol表示单元格地址$AA$33 ,则上面的示例将转换为Range(":$A$33:$AA$33")

If you put a variable enclosed in quotes like so: "$A$33:&LastCol" , your not referencing the variable as it becomes part of a string.如果您将变量放在引号中,如下所示: "$A$33:&LastCol" ,您不会引用该变量,因为它会成为字符串的一部分。 To concatenate correctly (using & ) you need to ensure it's outside of the double quotes like so: "$A$33:" & LastCol .要正确连接(使用& ),您需要确保它在双引号之外,如下所示: "$A$33:" & LastCol That basically translates to joining a string "$A$33:" with your variable value "$AA$33" (per the example I've given above).这基本上转化为将字符串"$A$33:"与您的变量值"$AA$33" (根据我上面给出的示例)连接起来。

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

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