下标超出范围9

时间:2016-04-29 03:15:28

标签: excel vba

我坚持使用这段代码:

With wb2
    .SaveAs Filename:=wb2.Path & "\sample2.xlsx"
End With
wb2.Activate
Set wb3 = Workbooks(wb2.Path & "\sample2.xlsx") 
wb2.Close
wb3.Activate

当它到达此行Set wb3 = Workbooks(wb2.Path & "\sample2.xlsx")时,它会显示:

  

下标超出范围

我正在尝试获取当前打开的工作簿的路径。我尝试了很多方法,但没有工作。有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

如果您只想要当前工作簿的路径,请尝试:

ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
'or
ActiveWorkbook.FullName

或在你的情况下

wb2.Path & "\" & wb2.Name
'or
wb2.FullName

答案 1 :(得分:0)

尝试使用以下

Sub test()
    Dim wb2 As Workbook
    Set wb2 = ActiveWorkbook
    With wb2
        .SaveAs Filename:=wb2.Path & "\sample2.xlsx", FileFormat:=51
    End With
    wb2.Activate
    Set wb3 = Workbooks(wb2.Path & "\sample2.xlsx")
    wb2.Close
    wb3.Activate
End Sub