简体   繁体   English

Excel VBA脚本VLOOKUP具有未知工作表名称的其他文件

[英]excel VBA script VLOOKUP other file with unknown worksheet name

Im trying to create a VBA script with a VLOOKUP to another workbook. 我试图用VLOOKUP创建另一个工作簿的VBA脚本。 The name of the 2nd workbook is always Stock.xls. 第二个工作簿的名称始终为Stock.xls。 The data in Stock.xls is always in the first worksheet , but the name of this sheet is different every day. Stock.xls中的数据始终在第一个工作表中 ,但是此工作表的名称每天都不同。 It can be ARTLIST1, ARTLIST2, ARTLIST(3), ARTLIST-4 etc... but always starts with ARTLIST. 可以是ARTLIST1,ARTLIST2,ARTLIST(3),ARTLIST-4等...,但始终以ARTLIST开头。 How can I tell the VBA script just to use the first sheet, or the sheet always contains the word ARTLIST (eg ARTLIST*) 我该如何告诉VBA脚本仅使用第一张图纸,还是该图纸始终包含单词ARTLIST(例如ARTLIST *)

 ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(C[-36],'[Stock.xls]ARTLIST1'!R1:R65536,13,0)"
Selection.Copy

Help is highly appreciated. 非常感谢您的帮助。 Thanks in advance! 提前致谢!

Richard 理查德

You can reference it by index and the get the name. 您可以通过索引引用它并获取名称。 Then you the name variable in the building of the formula. 然后,您将在公式构建中使用name变量。

Dim wsName as string
Dim ws As Excel.Worksheet

Set ws = Application.Worksheets(1)
wsName = ws.Name

ActiveCell.FormulaR1C1 = "=VLOOKUP(C[-36],'[Stock.xls]" & wsName & "'!R1:R65536,13,0)"
Selection.Copy

Use the worksheet with index of 1 to get the first sheet: 使用索引为1的工作表获取第一张工作表:

For each ws in worksheetsFor Each ws In ActiveWorkbook.Worksheets
  If ws.Index = 1 Then
      'Do code, this is the first worksheet
  End If
Next ws

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM