我在用户表单上有一个VBA Vlookup,我希望数据来自保存在Intranet上的excel文件。
当我尝试获取内部网上数据的VLookup
时,它的工作方式如下:
=VLOOKUP(I6,'https://intranet.private.private.uk/folder1/folder2/folder3/Documents/[Contract database.xlsm]AvailableContracts'!$H$9:$L$108,2,FALSE)
我想将该链接合并到下面的VBA中,而不会收到错误消息:
Private Sub ContractsList_AfterUpdate()
If WorksheetFunction.CountIf(Sheet2.Range("A:A"), Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.TextBox1 = Application.WorksheetFunction.VLookup(Me.ContractsList, Sheet2.Range("A5:E72"), 2, 0)
End With
End Sub
引发错误的一行是:
.TextBox1 = Application.WorksheetFunction.VLookup(Me.ContractsList, Sheet2.Range("A5:E72"), 2, 0)
当我使用excel Sheet2.Range("A5:E72")
公式中的内部网络链接更改VLookup
时。
答案 0 :(得分:2)
下面将打开工作簿,并允许您像处理任何其他工作簿一样使用它。我更新了您的代码,以包含新工作簿以及您在上面VLOOKUP
中提供的范围。我不认为VBA会像你希望的那样让你进行查找。
Private Sub ContractsList_AfterUpdate()
Dim wb as Workbook
Set wb = Workbooks.Open("https://intranet.private.private.uk/folder1/folder2/folder3/Documents/Contract database.xlsm")
If WorksheetFunction.CountIf(Sheet2.Range("A:A"), Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.TextBox1 = Application.WorksheetFunction.VLookup(Me.ContractsList, wb.Sheets("AvailableContractsRange").Range("H9:L108"), 2, 0)
End With
End Sub
答案 1 :(得分:2)
您可以使用Application.Match
节省大量时间和代码行,请查看以下代码:
Private Sub ContractsList_AfterUpdate()
Dim WB As Workbook
Dim Sht As Worksheet
' set workbook to workbook location at internet (according to your formula)
Set WB = Workbooks.Open("https://intranet.private.private.uk/folder1/folder2/folder3/Documents/Contract database.xlsm")
Set Sht = WB.Worksheets("AvailableContracts")
With Me.ContractsList
If Not IsError(Application.Match(.Value, Sht.Range("A:A"), 0)) Then '<-- value found in Column A
'Lookup values based on first control
Me.TextBox1 = Sht.Range("C" & Application.Match(.Value, Sheet2.Range("A:A"), 0)).Value
Else '<-- value not found in Column A
MsgBox "This contract is not on the list"
.Value = ""
Exit Sub
End If
End With
End Sub
答案 2 :(得分:0)
这个怎么样?您也应该确定工作簿。
Private Sub ContractsList_AfterUpdate()
If WorksheetFunction.CountIf(Workbooks("Contract database").sheet2.Range("A:A"), Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.TextBox1 = Application.WorksheetFunction.VLookup(Me.ContractsList, Workbooks("Contract database").sheet2.Range("A5:E72"), 2, 0)
End With
End Sub
答案 3 :(得分:0)
看起来你已经注释掉了
With Me
行,但这可能只是你问题中的格式。
了解您收到的错误消息会很有帮助。