0
我想知道是否有办法不必打开工作簿以从中获取信息。问题是我让用户首先选择文件,因为名称更改。所以我使用Application.GetOpenFilename。一旦他们选择了它,因为它实际上并没有打开,我试图从那里抓取一些单元格并复制它们。我有一些其他单元格使用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)"
我使用Application.GetFilename代码,然后用VLOOKUP从我的文件的另一部分获取来自该文件的信息,并且不会打开,但它会获取信息。所以我想我可以用这个做。 我想我需要再次尝试使用该代码并重新开始。我改变了太多东西。 我可以不只是设置一个变量的文件路径,然后引用或什么? 我将发布我在主帖子中使用的代码 – Robillard
@Robillard它实际上并没有从关闭的文件中获取数据。上次打开文件时,Excel缓存结果的副本。如果外部文件已更改,除非打开它,否则不会看到更新。 – robinCTS
我只需要一次信息。然后我复制粘贴特殊的值,因为我不想在那里查找。我做了一次然后发送报告。然后下次再用另一个文件再做一次。那么application.getfilename实际上是否打开文件?我只是困惑,因为那个工程,但我不能让我的工作。我觉得我应该像在vlookup中那样引用它。 – Robillard