重新初始化" ThisWorkbook.Path& quot; [英] Re-Initializing "ThisWorkbook.Path"

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

问题描述

首先,感谢你们中给我有关使用"ThisWorkbook.Path"的建议的人.它像一种魅力一样工作.

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

但是,我的代码遍历了七(7)个工作簿,并且在使用"ThisWorkbook.Path"时无法重新初始化"This.Workbook".让我详细说明.

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

这是宏所在的工作簿:

Workbooks("Financial_Aggregator_v3.xls").Activate

这是第一个工作簿,其中的代码添加了选项卡并进行了小计.基本上,ThisWorkbook.Path在这里工作:

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

在完成机械式"所需的操作后,我得到了以下代码片段,这些片段永远不会变成TRUE:

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

仅供参考,我试图将所有不同的工作簿分解为不同的Sub(),但这没有用.我还仔细检查了工作簿的名称.

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.

谢谢.

推荐答案

Excel.VBA中的"ThisWorkbook"有点像"Me",因为它仅适用于实际保存VBA代码的工作簿(.XLS)正在执行"ThisWorkbook".您需要做的是使用工作簿对象来抽象您要测试或操作的特定工作簿.

"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.

尝试这样的事情:

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& quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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