[英]How to reference the far right worksheet in another workbook, for a VLOOKUP?
I am writing a vlookup function and I want to reference the last tab in a another workbook, rather than the tab name as it changes daily.我正在编写一个 vlookup 函数,我想引用另一个工作簿中的最后一个选项卡,而不是每天更改的选项卡名称。
To move to the last tab in the workbook I have要移动到工作簿中的最后一个选项卡,我有
Sheets(Sheets.Count).Select
Is it possible to reference this in the a VLOOKUP?是否可以在 VLOOKUP 中引用它? (left a blank where the worksheet should be)
(在工作表应该放置的地方留一个空白)
"=VLOOKUP(RC[133],'[Unavista UTI Lookup December2019.xlsm]" "'!C4:C7,4,0)"
This is just off the top of my head (untested), but maybe something like this这只是我的头顶(未经测试),但也许是这样的
Dim wbData as Workbook
Set wbData = Workbooks.Add("Unavista UTI Lookup December2019.xlsm")
ActiveWorkbook.Worksheets("Sheet1").Range("A1").Formula = "=VLOOKUP(RC[133],[" & wbData.Name & "]" & wbData.WorkSheets(wbData.WorkSheets.Count).Name & "!C4:C7,4,0)"
wbData.Close False
Alternatively, using the .Open
method:或者,使用
.Open
方法:
Dim wbData as Workbook
Set wbData = Workbooks.Open(Filename:="Unavista UTI Lookup December2019.xlsm", ReadOnly:=True)
ActiveWorkbook.Worksheets("Sheet1").Range("A1").Formula = "=VLOOKUP(RC[133],[" & wbData.Name & "]" & wbData.WorkSheets(wbData.WorkSheets.Count).Name & "!C4:C7,4,0)"
wbData.Close False
Adjust Sheet1 and A1 to your needs根据您的需要调整 Sheet1 和 A1
But still, you would need to trigger this code on an event of some sort, which isn't clear to me yet.但是,您仍然需要在某种事件上触发此代码,这对我来说尚不清楚。
The Function I am at is below;我所在的功能如下;
Currently i am changing the date of the worksheet which the vlookup is referencing.目前我正在更改 vlookup 引用的工作表的日期。 So the below shows "Dec 9" from yesterday.
所以下面显示了昨天的“12月9日”。 Once the new tab gets created today in the file "Unavista UTI Lookup December2019" - It will show "Dec 10"
今天在“Unavista UTI Lookup December2019”文件中创建新标签后 - 它将显示“Dec 10”
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[133],'[Unavista UTI Lookup December2019.xlsm]Dec 9'!C4:C7,4,0)"
What I am trying to do is find a way for the code to automatically change this to todays date.我想要做的是找到一种方法让代码自动将其更改为今天的日期。 Speaking with someone at work we tried the below
与工作中的某人交谈,我们尝试了以下方法
Dim DayValue As String
Dim FormulaString As String
DayValue = Format(Date, "mmm d")
Range("E2").Select
FormulaString = "=VLOOKUP(EH2,'[Unavista UTI Lookup December2019.xlsm]" & DayValue & "' !$D:$G,4,0)"
The formula isn't bringing back any error, but unfortunately the lookup isn't doing anything, and just bringing back empty cells.该公式没有带回任何错误,但不幸的是查找没有做任何事情,只是带回空单元格。
If anyone can resolve this, as i've tried numerous things and still not been able to get it to work.如果有人可以解决这个问题,因为我已经尝试了很多事情,但仍然无法让它工作。
I would assume that your were applying the formula with a line like this:我假设您正在使用这样的行应用公式:
Range("E2").Formula = FormulaString
however when I tried that line, it returned an error as your line has one blank space in excess, as the syntax for a link with another workbook should be:但是,当我尝试该行时,它返回一个错误,因为您的行有一个多余的空格,因为与另一个工作簿的链接的语法应该是:
'[WorkbookName]WorksheetName'!Range
and your line has:并且您的线路有:
'[WorkbookName]WorksheetName' !Range
Therefore instead of this:因此,而不是这样:
FormulaString = "=VLOOKUP(EH2,'[Unavista UTI Lookup December2019.xlsm]" & DayValue & "' !$D:$G,4,0)"
use this:用这个:
FormulaString = "=VLOOKUP(EH2,'[Unavista UTI Lookup December2019.xlsm]" & DayValue & "'!$D:$G,4,0)"
Range("E2").Formula = FormulaString
FormulaString = "=VLOOKUP(EH2,'[Unavista UTI Lookup December2019.xlsm]" & DayValue & "'!$D:$G,4,0)"
Range("E2").Formula = FormulaString
However, in order to ease the maintenance of the procedure I suggest to use a constant, try this procedure:但是,为了简化程序的维护,我建议使用常量,请尝试以下程序:
Sub Formula_Today()
Const kFml As String = "= VLOOKUP( RC[133]," & _
"'[Unavista UTI Lookup December2019.xlsm]#TODAY'!C4:C7, 4, 0 )"
Dim sFml As String
sFml = Replace(kFml, "#TODAY", Format(Date, "mmm d"))
With ThisWorkbook.Worksheets("DATA")
.Range("E2:E10").FormulaR1C1 = sFml
End With
End Sub
Before adding the new worksheet添加新工作表之前
After adding the new worksheet and applying the Formula_Today
procedure添加新工作表并应用
Formula_Today
过程后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.