用vlookup引用工作表

新来的,只做了24小时左右。 其中的12个小时一直在研究这个问题。 我发现这么多页面上的例子看起来应该是他们应该工作,但是没有。 我一定是明显失踪了。

我的代码:

  1. 打开用户select(键入)现有工作表的msgbox。 目前只有一个工作表,C1。
  2. macros然后使用vlookup来查找单元格中的值,该值存储在variables中供以后使用。 我试图find的单元包含2016.1。 它位于C1表单元格C25中。

问题是vlookup。 我得到“运行时错误1004:无法获取WorksheetFunction类的VLookup属性”。

我知道我的variablesProdcode包含正确的工作表名称(C1),并且ForecastYear包含正确的年份(2016.1)。 我想我的问题是,我不是以某种方式引用工作表名称,但我试图从这么多网站的例子,并没有一个工作。


Function WorksheetExists(WSName As String) As Boolean On Error Resume Next WorksheetExists = Worksheets(WSName).Name = WSName On Error GoTo 0 End Function Private Sub UserForm_Initialize() Dim ProdCode As String Do Until WorksheetExists(ProdCode) ProdCode = InputBox("Enter Product Code: ", "Enter Product Code:", "ie C1") If Not WorksheetExists(ProdCode) Then MsgBox ProdCode & _ " doesn't exist!", vbExclamation Loop Sheets(ProdCode).Select Me.Title.Caption = "Forecast data for " & ProdCode Me.Label2012.Caption = Format(Now(), "yyyy") Me.Label1sta.Caption = "1st Qtr" Me.Label2nda.Caption = "2nd Qtr" Me.Label3rda.Caption = "3rd Qtr" Me.Label4tha.Caption = "4th Qtr" Me.LabelFc1.Caption = "Forecast" Me.Labelwfc1.Caption = "Weighted Forecast" Me.LabelwD1.Caption = "Weighted Demand" '----------------------------------------------------------------------------- '1st quarter current year predictions Dim ForecastYear As Double ForecastYear = Year(Now) + .1 'the .1 is to break the year into quarters MsgBox (ForecastYear) 'for debugging only. checks the correct year is selected MsgBox (ProdCode) 'for debugging only. checks the correct worksheet is selected Dim Forecast As Double Forecast = Application.WorksheetFunction.VLookup(ForecastYear, _ Sheets(ProdCode).Range("A9:J5000"), 10, False) Forecast = Round(Forecast, 2) '----------------------------------------------------------------------------- With ListBox1 .AddItem ForecastYear .AddItem Forecast .AddItem "" End With End Sub 

对不起,我知道这可能以前曾经问过。 我可能甚至盯着另一页上的答案,没有意识到这是答案。

我想你必须改变:

 Dim Forecast As Double Forecast = Application.WorksheetFunction.VLookup(ForecastYear, Sheets("ProdCode").Range("A9:J5000"), 10, False) Forecast = Round(Forecast, 2) '----------------------------------------------------------------------------- With ListBox1 .AddItem ForecastYear .AddItem Forecast .AddItem "" End With 

至:

 Dim Forecast As Variant Forecast = Application.VLookup(ForecastYear, Sheets(ProdCode).Range("A9:J5000"), 10, False) If IsError(Forecast) Then MsgBox "couldn't find '" & ForecastYear & "' in Sheets '" & ProdCode & "'" Exit Sub End If Forecast = Round(Forecast, 2) '----------------------------------------------------------------------------- With ListBox1 .AddItem ForecastYear .AddItem Forecast .AddItem "" End With 

此外,我会重构最初的ProdCode循环:

 ProdCode = Application.InputBox("Enter Product Code: ", "Enter Product Code:", "ie C1", , , , , 2) Do While Not WorksheetExists(ProdCode) MsgBox ProdCode & " doesn't exist!", vbExclamation ProdCode = Application.InputBox("Enter Product Code: ", "Enter Product Code:", "ie C1", , , , , 2) Loop