excel - VBA/宏问题创建正确高度/宽度的表格并在没有电子表格最后一行的情况下创建该表格
问题描述
我已经写了(在 StackOverflow 的帮助下,谢谢!)一个宏
- 抓取 excel 文件(本例中实际上只有 7 个)
- 将他们的工作表迁移到主文件中(每个源 excel 文件中只有一个工作表)
- 删除查看窗格
- 更改列宽
我在“查找范围的右下角,向上移动一行并选择回到“家”并命名表名”时遇到问题。
高层次的想法是我不希望每个文件底部的摘要行。
我不确定我是否为此部分选择了“最佳”解决方案(我实际上更喜欢:
- 找到最下面一行
- 删除它
- 转到“新”
xlLastCell
- 从范围创建表
a1:xlLastCell
此外,创建的表“始终”为 8 行高 x 19 列(S 列)宽。
我正在就两个相关项目寻求帮助:
- 创建没有底行的表格
- 根据数据集的变量性质创建表(我相信解决方案可能是在处理每个文件时设置一个变量。
'Find the bottom right of the range, move up one row and select back to "home" and make a table name
Range("A1").Select
Range(Selection, Selection.SpecialCells(xlLastCell)). _
Resize(Range(Selection, Selection.SpecialCells(xlLastCell)).Columns.Count, _
Range(Selection, Selection.SpecialCells(xlLastCell)).Rows.Count - 1).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = "xARPatient"
Sub aaaa()
Dim wb_source As Workbook
Dim wb_target As Workbook
Application.DisplayAlerts = False
Set wb_target = Workbooks.Open("c:\Test\MEBIllingOffice.xlsm")
'assuming the sheet name is "xAccountARAgingPatient.xlsx" note - sheet names must be unique within target
'Import File #1''''''''''''''''''''''''''''''''''''''''''
'import file as a worksheet from other worbooks in the same directory
Set wb_source = Workbooks.Open("C:\Test\xAccountARAgingPatient.xlsx")
For Each ws In Worksheets
For Each LO In ws.ListObjects
LO.Unlist
Next
Next
wb_source.Sheets("xAccountARAgingPatient.xlsx").Copy after:=wb_target.Sheets(wb_target.Sheets.Count)
'remove extraneous formatting
Cells.Select
Cells.EntireColumn.AutoFit
Selection.ClearFormats
ActiveWindow.FreezePanes = False
'Find the bottom right of the range, move up one row and select back to "home" and make a table name
Range("A1").Select
Range(Selection, Selection.SpecialCells(xlLastCell)). _
Resize(Range(Selection, Selection.SpecialCells(xlLastCell)).Columns.Count, _
Range(Selection, Selection.SpecialCells(xlLastCell)).Rows.Count - 1).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = "xARPatient"
wb_source.Close
'Import File #2''''''''''''''''''''''''''''''''''''''''''
'import file as a worksheet from other worbooks in the same directory
Set wb_source = Workbooks.Open("C:\Test\xAccountARAgingPayer.xlsx")
For Each ws In Worksheets
For Each LO In ws.ListObjects
LO.Unlist
Next
Next
wb_source.Sheets("xAccountARAgingPayer.xlsx").Copy after:=wb_target.Sheets(wb_target.Sheets.Count)
'remove extraneous formatting
Cells.Select
Cells.EntireColumn.AutoFit
Selection.ClearFormats
ActiveWindow.FreezePanes = False
'Find the bottom right of the range, move up one row and select back to "home" and make a table name
Range("A1").Select
Range(Selection, Selection.SpecialCells(xlLastCell)). _
Resize(Range(Selection, Selection.SpecialCells(xlLastCell)).Columns.Count, _
Range(Selection, Selection.SpecialCells(xlLastCell)).Rows.Count - 1).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = "xARPayer"
wb_source.Close
''''''''''''''''''''''''''''''''''''''''''
'wb_target.Save
'wb_target.Close
Application.DisplayAlerts = True
End Sub
解决方案
你可以尝试这样的事情:
Dim rng As range, ws as worksheet
set ws = Activesheet
set rng = ws.Range("a1").currentregion 'select whole table
set rng = rng.resize(rng.rows.count - 1, rng.columns.count) 'make one row shorter...
ws.ListObjects.Add(xlSrcRange, rng, , xlYes).Name = "xARPayer"
推荐阅读
- python - 具有多个日期时间的 Pandas 数据框
- google-apps-script - 清理并尝试合并编辑代码
- r - 如何选择具有跨函数 dplyr 的连续列
- sql - 将报告从 COGNOS 迁移到 SQL Server
- php - PHP:使用静态变量作为方法参数
- stripe-payments - 在之前删除的 Stripe 中创建客户时出现问题
- spreadsheet - 使用 importXML 从 URL 中批量查找售罄的产品页面
- javascript - Mapbox GL JS - 如何添加点云层
- entity-framework - Azure 函数启动中的 EF Core 迁移
- sql - 如何在“Case When Then”SQL 语句中用特殊字符替换字符?