VBA在OneDrive文件中使用VLOOKUP

huangapple go评论57阅读模式
英文:

VBA VLOOKUP with OneDrive file

问题

我正在寻找一种方法,可以从Sharepoint/OneDrive中的另一个文件中调用数据,然后在VBA中将其用于VLOOKUP公式,但我找不到方法来实现这一点,我尝试使用Workbook.OpenLinks,但它也不起作用。

一个影响因素是我实际上不知道要使用什么链接,以下是我尝试过的链接示例:

文件浏览器:C:\Users\Me\Organization\Different person - His folder\Subfolder\subsubforlder\Required file.xlsx

Chrome:https://organizationindustrie-my.sharepoint.com/:x:/r/personal/different_person_organization_com/_layouts/15/Doc.aspx?sourcedoc=%xxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx%xx&file=Required%20file.xlsx&action=default&mobileredirect=true

Excel公式:'https://organizationindustrie-my.sharepoint.com/personal/Different_person_organization_com/Documents/his Folder/subfolder/subsubfolder/[Required file.xlsx]Sheet name'!$A:$C

(我也尝试过不包括范围$A:$C和Sheet名称的版本)

已打开所需工作簿的Excel公式:'[Required file.xlsx]Sheet name'!$A:$C

目前我的代码可以工作,但在运行宏之前需要打开所需的文件。

以下是当前可以使用所需工作簿打开的代码:

With ActiveSheet
    .Cells(i, lastcol + 1).Value = Application.VLookup(Cells(i, "B"), Workbooks("Required file.xlsx").Worksheets("Sheet name").Range("A:C"), 3, False)
End With

我不需要打开工作簿,我只需要调用数据。

英文:

I'm looking for a way to call the data from another file that is in Sharepoint/OneDrive to use it in a VLOOKUP formula in VBA but I can't find a way to do it, I've tried using Workbook.OpenLinks but it also doesn't work

A contribuiting factor is that I don't actually know what link to use, here's an example of the links I've tried:

File explorer:C:\Users\Me\Organization\Different person - His folder\Subfolder\subsubforlder\Required file.xlsx

Chrome:https://organizationindustrie-my.sharepoint.com/:x:/r/personal/different_person_organization_com/_layouts/15/Doc.aspx?sourcedoc=%xxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx%xx&file=Required%20file.xlsx&action=default&mobileredirect=true

formulas in excel: 'https://organizationindustrie-my.sharepoint.com/personal/Different_person_organization_com/Documents/his Folder/subfolder/subsubfolder/[Required file.xlsx]Sheet name'!$A:$C

(I've tried this one also without the range $A:$C and Sheet name)

formulas in excel with the required workbook opened: '[Required file.xlsx]Sheet name'!$A:$C

Currently my code works but I need to open the required file before running the macro,

Here's the code that currently works with the required workbook opened

With ActiveSheet

    .Cells(i, lastcol + 1).Value = Application.VLookup(Cells(i, "B"), Workbooks("Required file.xlsx").Worksheets("Sheet name").Range("A:C"), 3, False)

End With

I don't need the workbook to open, I just need to call the data

答案1

得分: 1

要打开工作簿,您需要使用 Workbooks.Open("path"),其中路径是完整的 OneDrive 文件的 https 链接。要找到文件路径,最简单的方法是将文件手动加载到 Excel 中,然后在调试窗口中键入以下命令:

Print Workbooks("Required file.xlsx").FullName

这将显示完整的路径,以 https://..... 开头,以 .../Required file.xlsx 结尾。

一旦文件被打开,您可以像平常一样引用文件中的工作表。当您获取所需的数据后,当然要关闭它。

英文:

To open the workbook you will need to use Workbooks.Open("path") where path is the full https link to the OneDrive file. To find the file path the easiest way is to load the file manually into Excel, then in the Debug Window type in this command:

Print Workbooks("Required file.xlsx").FullName

This should then show you the full path starting https://..... and ending with .../Required file.xlsx.

Once the file has been opened you can refer to the sheets in the file as normal. You should of course close it after getting the data you need.

huangapple
  • 本文由 发表于 2023年8月4日 02:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定