使用Workbook Active中的变量的Excel VBA无效(动态获取活动工作簿名称)

时间:2017-09-17 04:43:07

标签: excel vba excel-vba

你好我正在创建vba项目,我需要从一个工作簿切换到另一个需要动态活动工作簿名称的工作簿

所以我正在尝试使用此代码,这里我收到错误“脚本超出范围”

Dim wb
wb = ActiveWorkbook.FullName
wb = Split(wb, "\")(UBound(Split(wb, "\")))
Workbooks("wb").Activate

但是当我将活动工作簿的动态名称更改为静态时...此代码有效...

Dim wb
Workbooks("project1.xlsm").Activate

请帮助大家..因为我需要活动工作簿的动态名称...

1 个答案:

答案 0 :(得分:1)

您现有代码中的错误是由于您尝试使用<SplitView PanePlacement="Right" PaneBackground="Transparent"> <SplitView.Pane> <Border x:Name="RootBorder" Padding="12,0,0,0"> <Grid> <controls:DropShadowPanel BlurRadius="12" Color="Black" Opacity="0.3" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"> <Rectangle Fill="White" /> </controls:DropShadowPanel> <!-- SystemControlPageBackgroundChromeLowBrush is the default PaneBackground brush, feel free to change it to whatever you like! --> <Grid x:Name="PaneContentGrid" Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}"> <!-- place your Panel content here. --> </Grid> </Grid> </Border> </SplitView.Pane> </SplitView> 文字 centerText = new Label { HorizontalTextAlignment = TextAlignment.Center, Text = "This is a disabled editor", HorizontalOptions = LayoutOptions.Fill }; 而不是变量String而导致的。所以以下内容应该有效:

"wb"

但是,使用wb然后剥离路径组件与使用Dim wb As String wb = ActiveWorkbook.FullName wb = Split(wb, "\")(UBound(Split(wb, "\"))) Workbooks(wb).Activate 属性没有区别:

FullName

但最好只创建一个Name对象并直接使用它:

Dim wb As String
wb = ActiveWorkbook.Name
Workbooks(wb).Activate

注意:极少数情况下需要使用WorkbookDim wb As Workbook Set wb = ActiveWorkbook wb.Activate 。如果没有看到以下代码,很难用很好的代码来帮助你,但可能就是这样:

Activate

查看this question的答案可能是个主意。