Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 宏将所选内容格式化为具有动态大小原始数据转储的表_Vba_Excel - Fatal编程技术网

Vba 宏将所选内容格式化为具有动态大小原始数据转储的表

Vba 宏将所选内容格式化为具有动态大小原始数据转储的表,vba,excel,Vba,Excel,我已经试了几个小时来做一个选择原始数据转储的宏。我是这样做的: Range("A1").Select 'Start at A1 Range(Selection, Selection.End(xlDown)).Select 'Go all the way down Range(Selection, Selection.End(xlToRight)).Select 'Go all the way to the rgt 由于xlDown和xlToRight,它适用于不同大小的数据转储。不过,我想这远

我已经试了几个小时来做一个选择原始数据转储的宏。我是这样做的:

Range("A1").Select 'Start at A1
Range(Selection, Selection.End(xlDown)).Select 'Go all the way down
Range(Selection, Selection.End(xlToRight)).Select 'Go all the way to the rgt
由于xlDown和xlToRight,它适用于不同大小的数据转储。不过,我想这远远不是最佳的

现在,我需要将该选择格式化为一个表,以生成一个透视表

如何将选择存储到变量或变量中,然后在其他代码中使用它来格式化is as表格

就好像我有了VAR0和VAR1,然后:

ActiveSheet.ListObjects.Add(xlSrcRange, Range(VAR0:VAR1), , xlYes).Name 
= _"Table1"
我只是在学习VBA和使用宏记录器,并使用一些数据来播放。我真的很感谢你的指点

我正在使用Excel 2016


谢谢

您可以将变量设置为最后一行和最后一列,然后使用这些值定义范围。有多种方法可以做到这一点,我相信人们对各种方法都有利弊,但如果您的数据没有空行、空列或空白单元格,那么我喜欢:

last_row = Worksheets("<worksheet name>").UsedRange.Rows.Count
last_col = Worksheets("<worksheet name>").UsedRange.Columns.Count

这可能对您有用,但我也应该注意到,在代码中使用选择存在风险。

基本上,这就是我所寻找的,也许将来有人会偶然发现并使用它

'----------+ Format Raw Data As Table +----------
Dim table0 As ListObject
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select

Set table0 = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
tableb0.TableStyle = "TableStyleMedium15"
'----------+ Done Formatting as Table +-----------
'Start by declaring a variable as a ListObject, 
'Goto cell A1
'Select All the way from top to bottom till last cell with data
'Select All the way from left to right till last cell with data
'set table0 ListObject to the selection we just made
'finally format table0 by accessing table style "property"
'This may not work if data has blank cells.
'Specially, if A rows / columns have rows or data is not uniform.

*感谢@rryanp对我的帮助,从对另一个问题的回答来看,这段代码实际上属于@Dmitry Pavlip

嗨,谢谢!我在考虑制作一个命名范围。但这看起来更好。我将变量声明为Long,并将它们分配给last_row=worksheetsheets1.UsedRange.Rows.Count,但我得到运行时错误“9”:下标超出范围。我想我没有正确地参考表格。。。我忘了提到的是工作表右边还有其他内容。例如操作说明和运行宏的按钮。我猜超出范围是工作表名称的问题。是不是一定是工作表1而不是工作表1或稍有不同?把东西放在右边会让你很难得到你的列号。但是,如果您的原始代码选择了正确的范围,可能您可以执行以下操作:将最后一行设置为长,将最后一列设置为长,将rng设置为范围,最后一行=ActiveCell.EndxlDown.row,最后一列=ActiveCell.EndxlToRight.Column,设置rng=RangeActiveCell,cellslat\u row,最后选择,然后在您的代码中使用rng,其中包含RangeVAR0:VAR1I,我会尝试一下,非常感谢!我将把选择分配给LC和LR以及do:ActiveSheet.ListObjects.AddxlSrcRange、RangeActiveCell、CellsLR、LC、xlYes.Name=\uTable0
'----------+ Format Raw Data As Table +----------
Dim table0 As ListObject
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select

Set table0 = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
tableb0.TableStyle = "TableStyleMedium15"
'----------+ Done Formatting as Table +-----------
'Start by declaring a variable as a ListObject, 
'Goto cell A1
'Select All the way from top to bottom till last cell with data
'Select All the way from left to right till last cell with data
'set table0 ListObject to the selection we just made
'finally format table0 by accessing table style "property"
'This may not work if data has blank cells.
'Specially, if A rows / columns have rows or data is not uniform.