2017-09-19 47 views
0

我在下面使用此代码在另一个使用GetOpenFilename选择的文件中使用VLOOKUP。我想shtName是您选择的文件中的工作表的名称,但是每当我单步执行时,它始终是我工作的工作表的名称并将VLOOKUP放入。如何在VLOOKUP中使用GetOpenFilename获取图纸名称

我有shtName在我的VLOOKUP中,当我逐步浏览它时,它不会显示任何内容。 X显示文件名和路径,但shtName后显示什么。但是我的VLOOKUP无论如何都会结束工作,并将表单放入公式中。

这是为什么?我希望能够自己做到这一点,所以我知道我从你选择的文件中得到表单名称。

Dim iRet As Integer 
Dim strPrompt As String 
Dim strTitle As String 

' Promt 
strPrompt = "Please select the last Kronos Full File before the dates of this HCM Report." & vbCrLf & _ 
    "This will be used to find the Old Position, Org Unit, and Old Cost Center." & vbCrLf & _ 
    "For example, if the date of this report is 7-28-17 thru 8-25-17, the closest Kronos Full File you would want to use is 7-27-17." 

' Dialog's Title 
strTitle = "Last Kronos Full File for Old Positions" 

'Display MessageBox 
iRet = MsgBox(strPrompt, vbOK, strTitle) 

Dim LR As Long 
Dim X As String 
Dim lNewBracketLocation As Long 

X = Application.GetOpenFilename(_ 
    FileFilter:="Excel Files (*.xls*),*.xls*", _ 
    Title:="Choose the Kronos Full File.", MultiSelect:=False) 

MsgBox "You selected " & X 
'Find the last instance in the string of the path separator "\" 
lNewBracketLocation = InStrRev(X, Application.PathSeparator) 
'Edit the string to suit the VLOOKUP formula - insert "[" 
X = Left$(X, lNewBracketLocation) & "[" & Right$(X, Len(X) - lNewBracketLocation) 

shtName = ActiveWorkbook.Worksheets(1).name 

LR = Range("E" & Rows.Count).End(xlUp).Row 



Range("T2").Formula = "=VLOOKUP($E2,'" & X & "]shtName'!$B$1:$AP$99999,15,0)" 
Stop 
Range("T2").AutoFill Destination:=Range("T2:T" & Range("E" & Rows.Count).End(xlUp).Row) 
Stop 
Range("T2:T" & Range("E" & Rows.Count).End(xlUp).Row).Select 
Stop 
Range("U2").Formula = "=VLOOKUP($E2,'" & X & "]shtName'!$B$1:$AP$99999,41,0)" 
Range("U2").AutoFill Destination:=Range("U2:U" & Range("E" & Rows.Count).End(xlUp).Row) 
Range("U2:U" & Range("E" & Rows.Count).End(xlUp).Row).Select 
Range("V2").Formula = "=VLOOKUP($E2,'" & X & "]shtName'!$B$1:$AP$99999,18,0)" 
Range("V2").AutoFill Destination:=Range("V2:V" & Range("E" & Rows.Count).End(xlUp).Row) 
Range("V2:V" & Range("E" & Rows.Count).End(xlUp).Row).Select 
Cells.Select 
Cells.EntireColumn.AutoFit 
+0

您设置了'shtName = ActiveWorkbook.Worksheets(1).name'这意味着'shtName'是当前打开和活动工作簿中第一个工作表的名称。所以,如果你想让'shtName'成为别的东西,你需要设置它。 –

+0

啊,我认为GetOpenFilename是一旦我做到了,就会成为主动的。好的。你会推荐什么来获取表单名称?我可以从GetOpenFilename或其他东西激活文件吗? – Robillard

+0

我推荐:使用[Workbooks.Open Method](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbooks-open-method-excel)打开文件获取工作表名称和关闭它。以我的答案为例。 –

回答

2

类似下面的应该给你的工作表名称出文件

Dim wbk As Workbook 
Set wbk = Workbooks.Open(Filename:="YOUR_FILE_PATH", ReadOnly:=True) 

Dim shtName As String 
shtName = wbk.Worksheets(1).Name 
wbk.Close 

注:我们可以打开在只读模式下工作簿,如果我们不打算改变任何东西。


此外,我建议(以下为良好做法的一个很好的代码):

  • 始终指定一个工作表。
    例如,对于每Range("")Worksheets("YourSheetName").Range("")
    或者使用With声明:

    With Worksheets("YourSheetName") 
        .Range("A1").Value = 5 'recognize the starting full stop referring to the with statement 
    End With 
    
  • 同为RowsColumnsCells

  • 避免使用.Select,在.ActivateSelection.所有。
    (网上有很多教程如何避免它们)。
  • 使用Option Explicit并声明全部您的变量在使用前。
    (避免许多问题,特别是打字错误)。
+0

我明白了。真棒,我会检查出来。首先真正快速地研究另一个问题:) – Robillard

+0

是的,我通常通过录制一个宏来开始我的代码,并且不断地使用选择,我只是不想通过并且改变它。我对此非常感兴趣,并试图为我的工作做很多工作。我试图尽可能快地走,因为在我走的时候需要一段时间才能弄清楚。我是唯一一个会查看代码的人,但我一直在努力改进它。感谢您的指点。 – Robillard

+0

你能告诉我为什么我的VLOOKUP可以使用或不使用shtName,它在我的原始文章中?我可以把它拿出来,它根本不会影响它。我甚至可以在那里输入随机字母,它根本没有影响。 – Robillard

相关问题