VBA代码将工作表从Sharepoint上的关闭工作簿复制到当前工作簿。

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

VBA code to copy worksheet from a closed workbook on Sharepoint to current workbook

问题

我正在尝试创建一个宏,从另一个位于Sharepoint上的工作簿(wbReport)中复制一个工作表(wsReport)到宏所在的工作簿(wbMain),并将复制的工作表放在名为"Main"的工作表之后。在wbMain中。wsReport是wbReport中唯一的工作表。这应该在不可见地“打开”wbReport的情况下完成 - 我通过将ScreenUpdating设置为False来实现这一点。

我有以下相关代码:

Application.ScreenUpdating = False

Dim wbReport as Workbook
Dim wsReport as Worksheet
Dim wsMain as Worksheet
Dim ReportPath as String
Dim NewWindow as New Excel.Application

Set wsMain = ThisWorkbook.Worksheets("Main")
ReportPath = "https://company.sharepoint.com/FilePath"
Set wbReport = NewWindow.Workbooks.Open(ReportPath)
Set wsReport = wbReport.Worksheets(1)
wsReport.Copy After:=wsMain

一切都运行正常,直到最后一行。路径是正确的,如果我将ScreenUpdating设置为True,它会可视地打开正确的文件。如果我逐步执行代码,我可以执行Debug.Print wsReport.Range("A1"),它会返回wsReport单元格A1的值,所以我知道那部分是有效的。但一旦运行代码的最后一行(实际复制的那行),我会收到**“运行时错误'1004':不支持此接口”**。

我无法弄清楚为什么会出现此错误。我找不到有关此错误与复制数据相关的大量信息,但似乎其他遇到此问题的人是因为未正确引用其他文件中的工作表(例如,在应该引用工作表的地方使用“wsReport.Range(Range("A1").Offset...”而实际上应该是“wsReport.Range(wsReport.Range("A1").Offset...)。我认为通过明确定义所有相关的工作簿和工作表,我已经避免了这个问题(VBA认为它在哪个工作簿中不应该有关系,wsReport应该始终引用'https://company.sharepoint.com/FilePath'的Worksheets(1),wsMain应该始终引用包含宏的文件中的“Main”工作表)。

如果我尝试复制整个工作表而不仅仅是复制单元格值(wsReport.Cells.Copy... xlPasteValues)到wbMain中的新工作表,我可以让类似的东西起作用。但是,我需要从wsReport复制一切,包括格式、注释等,所以我不能只复制并粘贴值。当我尝试使用xlPasteAll时,它会执行一个奇怪的操作,并粘贴数据wsReport的透明.png,而不是实际数据 - 这显然不起作用。我认为解决这个问题的方法也可能有效,但只是按原样复制整个工作表似乎更容易处理。

一般来说,我可以找到的关于如何执行此操作的任何指导似乎只是说“正确使用Worksheets.Copy方法”,据我所知,我已经这样做了。也许我只是漏掉了一些极其明显的东西。这里显示的代码可能不完美(我进行了匿名处理,可能会遗漏一些内容),但这是我的宏中的前几行代码,因此在此之前没有任何可能会破坏某些东西的内容。

英文:

I am trying to make a macro that copies a worksheet (wsReport) from another workbook (wbReport), located on Sharepoint, to the workbook that the macro is in (wbMain), putting the copied worksheet after the worksheet named "Main" in wbMain.
wsReport is the only worksheet in wbReport.
This should be done without visibly 'opening' wbReport - I am doing this by setting ScreenUpdating to False.

The relevant code I have is below:

Application.ScreenUpdating = False

Dim wbReport as Workbook
Dim wsReport as Worksheet
Dim wsMain as Worksheet
Dim ReportPath as String
Dim NewWindow as New Excel.Application

Set wsMain = ThisWorkbook.Worksheets("Main")
ReportPath = "https://company.sharepoint.com/FilePath"
Set wbReport = NewWindow.Workbooks.Open(ReportPath)
Set wsReport = wbReport.Worksheets(1)
wsReport.Copy After:=wsMain

Everything works up until the last line. The path is correct, if I make ScreenUpdating = True it visually opens the correct file. If I step through the code, I can do

Debug.Print wsReport.Range("A1")

and it returns the value from cell A1 of wsReport, so I know that's working. But as soon as it runs the last line of code (the line to actually copy), I get **"Run-time error '1004': No such interface supported".
**

I can't figure out why this error is coming up. I can't find a lot of information on this error as it pertains to copying data, but it seems that other people who have had the issue had it by not properly referring to the worksheet in the other file everywhere they should have (eg. "wsReport.Range(Range("A1").Offset..." when it should have been "wsReport.Range(wsReport.Range("A1").Offset...). I think I have avoided this by clearly defining all of my relevant workbooks and worksheets (it shouldn't matter what workbook VBA thinks it's in, wsReport should always refer to 'https://company.sharepoint.com/FilePath'.Worksheets(1), and wsMain should always refer to the "Main" sheet in the file with the macro.)

I can get something similar to work if instead of copying the entire sheet, I copy the cell values (wsReport.Cells.Copy... xlPasteValues) to a new sheet in wbMain. However, I need everything from wsReport, including formatting, notes, etc., so I can't just copy and paste values. When I try to use xlPasteAll, it does a weird thing and pastes a transparent .png of the data in wsReport instead of the actual data - this obviously doesn't work. I figure a solution to this could also work, but just copying the whole sheet as-is seems like it would be easier to work with.

In general, any guidance I can find on how to do this seems to just say "use the Worksheets.Copy method properly," which as far as I can tell I am. Maybe I'm just missing something extremely obvious. It's possible that my code that I showed here isn't perfect (I anonymized it and might have missed something), but this is the first few lines of code in my macro so there's nothing happening before this that might be breaking something.

答案1

得分: 1

已弄清楚 - 我从另一个答案中获得了从Sharepoint打开文件的方法,并且在确认它有效后,我没有进行深入的批判性审查。感谢BigBen找到了一些显示你不能在不同实例之间进行.Copy操作的东西,还要感谢Tim Williams给了我一个提示,说明这是不必要的。没有必要打开新的Excel实例。以下代码实现了我想要的功能:

Application.ScreenUpdating = False

Dim wbReport as Workbook
Dim wsReport as Worksheet
Dim wsMain as Worksheet
Dim ReportPath as String

Set wsMain = ThisWorkbook.Worksheets("Main")
ReportPath = "https://company.sharepoint.com/FilePath"
Set wbReport = Workbooks.Open(ReportPath)
Set wsReport = wbReport.Worksheets(1)
wsReport.Copy After:=wsMain
wbReport.Close

您只需直接使用文件路径打开文件,只需要确保在复制后关闭文件,如果您不希望最终用户看到它打开(关闭屏幕更新后,它不会在视觉上打开一个窗口,除非您在打开和关闭之间调试并停止它)。

英文:

Figured it out - I got the method to open a file from Sharepoint from another answer, and didn't really critically look at it once I established it worked. Thanks to BigBen for finding something that showed you couldn't .Copy between instances, and Tim Williams for giving me the hint that it was not necessary. There was no need to open a new instance of Excel. The following code does what I wanted it to do:

Application.ScreenUpdating = False

Dim wbReport as Workbook
Dim wsReport as Worksheet
Dim wsMain as Worksheet
Dim ReportPath as String

Set wsMain = ThisWorkbook.Worksheets("Main")
ReportPath = "https://company.sharepoint.com/FilePath"
Set wbReport = Workbooks.Open(ReportPath)
Set wsReport = wbReport.Worksheets(1)
wsReport.Copy After:=wsMain
wbReport.Close

You can just directly open the file using the path, and just need to make sure you close it after you've copied if you don't want the end user to ever see it open (with screen updating off it never visually opens a window unless you debug and stop it between opening and closing.)

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

发表评论

匿名网友

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

确定