[英]How to extract specific range within excel worksheets and consolidate
I am trying to automate a report task in excel, I need to consolidate all workbooks in a folder into one worksheet, I followed the guide provided by danwagner here : https://danwagner.co/how-to-combine-multiple-excel-workbooks-into-one-worksheet-with-vba/我正在尝试在 excel 中自动执行报告任务,我需要将文件夹中的所有工作簿合并到一个工作表中,我遵循了 danwagner 提供的指南: https ://danwagner.co/how-to-combine-multiple-excel -workbooks-into-one-worksheet-with-vba/
It works very well however due to the unstructured nature of the data I am not getting the desired results.但是由于数据的非结构化性质,它工作得很好,我没有得到想要的结果。
Basically I would like the excel macro to only pick up a specific range of values, I am defining the upper row and number of columns but don't know how to have excel strictly copy out/extract a table within each worksheet(each workbook has the same starting row and number of columns for the table however the bottom row of the table vary).基本上,我希望 excel 宏只选取特定范围的值,我正在定义上一行和列数,但不知道如何让 excel 严格复制/提取每个工作表中的表格(每个工作簿都有表的起始行和列数相同,但表的底行不同)。
The table I need to grab from each workbook looks something like this, there is only one worksheet in each workbook to consolidate together:我需要从每个工作簿中抓取的表格看起来像这样,每个工作簿中只有一个工作表可以合并在一起:
Any tips would be greatly appreciated, btw I believe the edits need to be done on these lines of the macro from the guide but I keep running into errors:任何提示将不胜感激,顺便说一句,我相信需要在指南中的宏的这些行上进行编辑,但我一直遇到错误:
lngSrcLastCol = LastOccupiedColNum(wksSrc)
With wksSrc
Set rngSrc = .Range(.Cells(1, 1), .Cells(lngSrcLastRow, _
lngSrcLastCol))
End With
I know this isn't the answer you asked for, but perhaps a PowerQuery would work better here?我知道这不是您要的答案,但也许 PowerQuery 在这里效果更好?
If you'd like, you could try the following:如果您愿意,可以尝试以下操作:
Open a New Workbook打开新工作簿
Go to the Data Ribbon转到数据功能区
Select 'Get Data' -> 'From File' -> 'From Folder'选择“获取数据”->“来自文件”->“来自文件夹”
Navigate to the Folder housing your files导航到存放文件的文件夹
When Prompted select 'Transform Data'出现提示时选择“转换数据”
If necessary, set filters to only grab the files you want如有必要,将过滤器设置为仅抓取您想要的文件
In the far left [Content] column , click the icon with two down arrows在最左侧的[Content] 列中,单击带有两个向下箭头的图标
Select your table/tab and click 'OK' ( If more than one table or tab exists within your Workbook, you will be prompted with an option of navigating to the table/tab of your choice, I like to click the checkbox 'Skip files with errors' but that's up to you )选择您的表/选项卡并单击“确定” (如果您的工作簿中存在多个表或选项卡,系统将提示您导航到您选择的表/选项卡的选项,我喜欢单击复选框“跳过文件”有错误”,但这取决于您)
You will now apply a series of steps to get to the data you'd like, in your case and given your example, I would do something along the following lines:您现在将应用一系列步骤来获取您想要的数据,在您的情况下并给出您的示例,我将按照以下方式做一些事情:
'Remove Rows' -> 'Remove Top Rows' -> Number of Rows = 4 '删除行' -> '删除顶行' -> 行数 = 4
'Use First Row as Headers' ( Make sure you removed the top rows first ) '使用第一行作为标题' (确保你先删除了顶行)
'Filter' on [Column2] to remove nulls ( Assuming Column 2 never has text in rows below your target details, and it always has data matching the last desired row for your data pull ) [Column2] 上的“过滤器”以删除空值(假设第 2 列在目标详细信息下方的行中永远没有文本,并且它始终具有与数据拉取的最后所需行匹配的数据)
Now you should have your data, make any other edits you might want with regards to any formatting needs or additional filters you may want performed现在您应该拥有您的数据,根据您可能想要执行的任何格式需求或其他过滤器,进行您可能想要的任何其他编辑
Select 'Close & Load' ( All files will apply the same logic steps and build the final results into one table )选择“关闭并加载” (所有文件将应用相同的逻辑步骤并将最终结果构建到一个表中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.