我必须制作一份主报告,汇编来自两个有点大的工作簿的信息。这些工作簿每周都会更改,但每个工作簿中的信息结构将始终相同,从每列的列数到每个列名(每个列的记录数可能会有多少,当然,从20000到25000等)
从工作簿1中,我必须从范围中的特定列中提取信息。通过调查,我设法找到一个很好的代码,帮助了我很多。这些信息来自工作表中的A:W,但我只需要来自c,f,g,i和w列的信息。
Private Sub BtnImportDPr_Click()
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim rngSourceRange As Range
Dim rngDestination As Range
Set wkbCrntWorkBook = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel 2007-16", "*.xlsx; *.xlsm; *.xlsa; *.xlsb"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
Workbooks.Open .SelectedItems(1)
Set wkbSourceBook = ActiveWorkbook
Worksheets("SysSummary").Activate
ActiveSheet.Range("A:W").AutoFilter Field:=3, Criteria1:="<=700" _
, Operator:=xlAnd
Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="SysSummary!C:C,SysSummary!F:F,SysSummary!G:G,SysSummary!I:I,SysSummary!W:W", Type:=8)
wkbCrntWorkBook.Activate
Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="B:F", Type:=8)
rngSourceRange.Copy rngDestination
rngDestination.CurrentRegion.EntireColumn.AutoFit
wkbSourceBook.Close False
End If
End With
End Sub
此代码允许用户选择他/她想要使用的工作簿,我知道它看起来有很多潜在错误的风险,但我确信我从中提取信息的范围结构仍将是同样,一旦它被改变,它可能意味着这个工作实用程序将不存在。 |||
现在,使用第二个工作簿,我需要使用一个表(在用户将选择的所有工作簿中,它的名称将保持不变)。表格很大,从A列到AQ,但我只需要来自a,e,u,p,q,v和aa列的信息。
所以,既然最后一个代码工作了,我再次尝试使用它,但是一些错误弹出了。就像,一张桌子不同。
Private Sub BtnImportTTS_Click()
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim rngSourceRange As Range
Dim rngDestination As Range
Set wkbCrntWorkBook = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel 2007-16", "*.xlsx; *.xlsm; *.xlsa; *.xlsb"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
Workbooks.Open .SelectedItems(1)
Set wkbSourceBook = ActiveWorkbook
Worksheets("Sys_DETAIL").Activate
ConvertTableToRange
ActiveSheet.Range("A3:AT").AutoFilter Field:=27, Criteria1:="02"
Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="Sys_DETAIL!A:A,Sys_DETAIL!E:E,Sys_DETAIL!U:U,Sys_DETAIL!P:P,Sys_DETAIL!Q:Q,Sys_DETAIL!V:V,Sys_DETAIL!AA:AA", Type:=8)
wkbCrntWorkBook.Activate
Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="B:H", Type:=8)
rngSourceRange.Copy rngDestination
rngDestination.CurrentRegion.EntireColumn.AutoFit
wkbSourceBook.Close savechanges:=False
End If
End With
End Sub
Sub ConvertTableToRange()
Dim rList As Range
With Worksheets("TTS_DETAIL").ListObjects("Table_TRACK_TTS.accdb")
Set rList = .Range
.Unlist ' convert the table back to a range
End With
End Sub
经过调查后,我决定采用惰性路径,只需通过vba代码将表转换为正常范围即可。但由于某种原因,我无法提取所有信息。过滤到Criteria1:02(这是非常重要的)后,我留下了8500条记录(以前是200000条记录),但Excel只提取了5000条记录。
我不确定它是否与以前作为表格的范围有关。所以,如果有人可以帮我修改我的代码或给我一个与表一起使用的新代码,我将非常感激。