重新初始化“ThisWorkbook.Path” [英] Re-Initializing "ThisWorkbook.Path"

查看:1063
本文介绍了重新初始化“ThisWorkbook.Path”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,感谢那些给我的使用ThisWorkbook.Path的建议。它的作用就像一个魅力。



但是,我的代码遍历七(7)个工作簿,当使用ThisWorkbook.Path时,我无法重新初始化This。工作簿。请详细说明。



这是宏所在的工作簿:

 工作簿(Financial_Aggregator_v3.xls)。激活

这是代码添加的第一个工作簿一个选项卡,并且总计。基本上,ThisWorkbook.Path在这里工作:

 工作簿(Chapter_7-10_Mechanical.xls)激活

在做了我所需要的机械之后,我有以下代码片段,从来没有出现TRUE:

 工作簿(Financial_Aggregator_v3.xls)。激活
如果FileThere(ThisWorkbook.Path& Application.PathSeparator&Chapter_7- 90_ECS_1_LLC.xls)然后

适用于机械表单的功能代码为:

 函数FileThere(FileName As String)As Boolean 
FileThere =(Dir(FileName)>)
结束功能

FYI,我试图将所有不同的工作簿分解成不同的Sub ),但是没有起作用。我还三次检查了工作簿的名称。



提前感谢。

解决方案<在Excel.VBA中的div>

ThisWorkbook类似于Me,因为它仅适用于正在执行ThisWorkbook的VBA代码的工作簿(.XLS)。您需要做的是使用Workbook对象来抽象想要测试或操作的特定工作簿。



尝试这样:

  Public Sub TestWB()
Dim CurrWB As Workbook

'将工作簿转换为我们的对象变量:'
Set CurrWB = Workbooks(Chapter_7-10_Mechanical.xls)

'更改.Path:'
CurrWB.SaveAs NewFileName,AddToMru:= True

End Sub


First, thanks to those of you who gave me the suggestion on using "ThisWorkbook.Path". It worked like a charm.

However, my code walks through seven (7) workbooks and when using "ThisWorkbook.Path" I can not re-initialize the "This.Workbook". Let me elaborate.

This is the workbook where the Macro resides:

Workbooks("Financial_Aggregator_v3.xls").Activate

This is the first workbook where the code adds a tab and does sub-totals. Basically, ThisWorkbook.Path works here:

Workbooks("Chapter_7-10_Mechanical.xls").Activate

After doing what I need done with "Mechanical" I have the following code snippet, which never turns out TRUE:

Workbooks("Financial_Aggregator_v3.xls").Activate
If FileThere(ThisWorkbook.Path & Application.PathSeparator & "Chapter_7-90_ECS_1_LLC.xls") Then

The code for the function, which works for the "Mechanical" sheet is:

Function FileThere(FileName As String) As Boolean
    FileThere = (Dir(FileName) > "")
End Function

FYI, I tried to break all of the different Workbooks into different Sub(), but that didn't work. I also triple-checked the name of the workbooks.

Thanks in advance.

解决方案

"ThisWorkbook" in Excel.VBA is sort of like "Me", in that it only applies to the workbook (.XLS) that actually holds the VBA code that is executing the "ThisWorkbook". What you need to do is to use Workbook objects to abstract the particular workbook that you want to test or manipulate.

Try something like this:

Public Sub TestWB()
    Dim CurrWB As Workbook

    'To get a workbook into our object variable:'
    Set CurrWB = Workbooks("Chapter_7-10_Mechanical.xls")

    'To Change the .Path:'
    CurrWB.SaveAs NewFileName, AddToMru:=True

End Sub

这篇关于重新初始化“ThisWorkbook.Path”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆