ブックブックから情報を取得するためにブックを開く必要がない方法があるかと思います。問題は、名前が変更されたため、まずファイルを選択させることです。だから私は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
これは他のファイルを実際に開くことなくvlookupを実行するコードです。私は同じことをしてもらえますか?私はそれを働かせることはできません。
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を使用しますが、これを使用することによって、時間の問題を解決することができますしかし
私のファイルの別の部分にあるそのファイルから情報を取得するために、それは開かないが、情報を取得する。だから私はこれでこれをやり遂げることができたと思った。 私はそのコードをもう一度使ってみる必要があると思います。私はあまりにも多くのものを変えました。 変数をファイルパスに設定して、それを参照することはできますか? メインポストでvlookupに使用するコードを投稿します – Robillard
@Robillard実際には閉じたファイルからデータを取得するわけではありません。 Excelは、ファイルが最後に開かれたときに結果のコピーをキャッシュします。外部ファイルが変更されている場合は、開いていなければ更新が表示されません。 – robinCTS
私は情報を一度だけ取る必要があります。私はそこにvlookupを望んでいないので、値を貼り付けてコピーします。私は一度それをして、報告書を送ってください。次回に別のファイルを使って再度実行します。では、application.getfilenameは実際にファイルを開きますか?私はちょうどそれが働くので混乱しているが、私は働くことができない。私はvlookupでやっているように、ちょうどそれを参照することができるはずです。 – Robillard