我想使用vba在excel中将数据转换为表格格式 必须通过调用源文件在不同的工作表中执行代码 而且范围必须是动态的
这是我尝试过的代码
Sub converttbl()
Dim OpenWb As Workbook
Set OpenWb = Workbooks.Open("C:\Users\user\Desktop\data.xlsb")
Dim wsData As Worksheet
Set wsData = OpenWb.Worksheets("Data")
Set wsData = ActiveSheet
ActiveSheet.Range("A1").Select
Application.CutCopyMode = False
ActiveSheet.ListObjects.Add(xlSrcRange, _
Range("A1", Range("A1").End(xlToRight).End(xlDown)), , xlYes).Name _
= "Table1"
MsgBox "Execution completed"
End Sub
答案 0 :(得分:0)
创建表时,您需要将整个数据范围作为输入,而不只是左上角的单元格(A1)。试试这个:
ActiveSheet.ListObjects.Add(xlSrcRange, _
Range("A1", Range("A1").End(xlToRight).End(xlDown)), , xlYes).Name _
= "Table1"
答案 1 :(得分:0)
感谢您的支持... 以下代码可将动态范围的数据转换为表
Sub converttbl()
Dim OpenWb As Workbook
Set OpenWb = Workbooks.Open("C:\Users\user\Desktop\data.xlsb")
Dim wsData As Worksheet
Set wsData = OpenWb.Worksheets("Data")
OpenWb.Worksheets("Data").Range("A1").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Selection.CurrentRegion, , xlYes).Name = _
"MyTable"
MsgBox "Execution completed"
End Sub