从具有variables名称的封闭工作簿获取信息

我想知道是否有办法不必打开工作簿,从中获取信息。 问题是我有用户select文件,因为名称更改。 所以我使用Application.GetOpenFilename。 一旦他们select它,因为它实际上并没有打开,我试图从那里抓住一些细胞,并复制它们。 我有一些其他单元格使用vlookups引用工作簿以相同的方式,但这似乎不同或无法正常工作。 这里是代码:

Dim Window3 As String Dim x As String Dim lNewBracketLocation As Long Dim shtName As String ' Prompt strPrompt = "Please select the last 'HC Report' located in" & vbCrLf & _ "'C:\file\file\'" & vbCrLf & _ "before the dates of this Report." & vbCrLf & _ "This will be used to find the Interns that are currently working." & vbCrLf & _ "For example, if the date of this report is 9-8-17, you would want to use the 'August 2017.xlsx.' report." ' Dialog's Title strTitle = "Latest Report" 'Display MessageBox iRet = MsgBox(strPrompt, vbOK, strTitle) Window3 = Application.GetOpenFilename( _ FileFilter:="Excel Files (*.xls*),*.xls*", _ Title:="Choose previous quarter's file", MultiSelect:=False) MsgBox "You selected " & Window3 'below is some extra code from where I used this same startegy for VLOOKUP. 'Not sure if this "x" variable will be needed. lNewBracketLocation = InStrRev(Window2, Application.PathSeparator) 'Edit the string to suit the VLOOKUP formula - insert "[" x = Left$(Window2, lNewBracketLocation) & "[" & Right$(Window2, Len(Window2) - lNewBracketLocation) Dim wb3 As Workbook 'I want to do all of this WITHOUT opening this next file. Is that possible? ' If I open this file it works. but I am trying to do it without opening. 'Because it takes a minute 'Set wb3 = Workbooks.Open(Window3) shtName = wb3.Worksheets("Team Members").name '*******RIGHT here IS WHERE IT ERRORS****************** 'Run-time error '91': 'Object variable or With block variable not set Stop wb3.Sheets(shtName).Select ActiveSheet.Range("$A$1:$P$2769").autofilter Field:=1, Criteria1:="Interns" Range("A2768").Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select Selection.COPY 

这是我有一些其他的代码,而不需要实际打开其他文件。 我可以做同样的事情吗? 我无法得到它的工作。

 Dim Window2 As String Dim x As String Dim lNewBracketLocation As Long Window2 = Application.GetOpenFilename( _ FileFilter:="Excel Files (*.xls*),*.xls*", _ Title:="Choose previous quarter's file", MultiSelect:=False) MsgBox "You selected " & Window2 'Find the last instance in the string of the path separator "\" lNewBracketLocation = InStrRev(Window2, Application.PathSeparator) 'Edit the string to suit the VLOOKUP formula - insert "[" x = Left$(Window2, lNewBracketLocation) & "[" & Right$(Window2, Len(Window2) - lNewBracketLocation) shtName = ActiveWorkbook.Worksheets(1).name Stop MainWindow.Activate Columns("B:B").Select Selection.Delete Shift:=xlToLeft Range("AI2").FormulaR1C1 = "=VLOOKUP(RC2,'" & x & "]shtName'!R3C2:R9694C49, 23, FALSE)" Range("AJ2").FormulaR1C1 = "=VLOOKUP(RC2,'" & x & "]shtName'!R3C2:R9694C49, 19, FALSE)" Range("AK2").FormulaR1C1 = "=VLOOKUP(RC2,'" & x & "]shtName'!R3C2:R9694C49, 20, FALSE)" Range("AL2").FormulaR1C1 = "=VLOOKUP(RC36,'" & x & "]shtName'!R3C2:R9694C49, 23, FALSE)" 

从封闭的工作簿复制单元是不可能的 。 当Excelclosures外部工作簿时,Excel将caching结果的一个副本,从而使vlookup与众不同。

就像你想要做的一样,即你需要打开一次外部文件来获取数据。 使用vlookup时,将公式键入/粘贴到表单中。 此时,必须打开外部工作簿,或者从“ Update Values:Book1.xlsm文件select”对话框中select文件后,Excel会在后台打开它。 用你的代码,就是当你想要获取数据的时候。 您必须打开它才能自行caching数据。

但是,你可以通过使用这个解决时间问题:

 Application.Calculation = xlCalculationManual Set wb3 = Workbooks.Open(Window3) 

然后closures工作簿后,这:

 Application.Calculation = xlCalculationAutomatic