Excel VBA-运行时错误1004打开工作簿 [英] Excel VBA- Runtime Error 1004 opening a workbook

查看:64
本文介绍了Excel VBA-运行时错误1004打开工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vba相当陌生,但是我写了一段代码来分解一列中的行,然后进行复制,然后将单元格粘贴到另一个宏工作簿中.当我第一次运行它时,代码运行良好.但是,我今天尝试使用它,但我感到震惊,我犯了一个错误.此错误表明它找不到目标文件名.这很奇怪,因为目标文件名没有更改或移动.这是代码示例.

Pretty new to vba, but I wrote a code that unmerges rows within a column, copies thus cells then pastes within a different macro workbook. The code ran well when I first ran it. However I attempted to use it today and I was shocked I had an error. This error stated that it could not find the destination file name. which was weird because the destination file name did not change or move. here is a sample of the code.

错误显示在行:

设置book2 = Workbooks.Open('17财年报告的跨栏燃料Usage.xlsm)

Set book2 = Workbooks.Open("Fiscal '17 Reported Straddle Fuel Usage.xlsm")

代码如下:

Public Sub Button6_Click()
       'Change name of button

       Worksheets("7500").Buttons("Button 6").Text = "Send"
       With Sheets("7500")

       'Unmerge cells
       .Range("AI3:AI92").Select
       With Selection
        For Each rCell In Selection
         With rCell
         If .MergeCells Then
          .MergeArea.UnMerge
         End If
        End With
       Next rCell
       End With
      End With

      'open destination workbook

       Dim book1 As Workbook
       Dim sheet1 As Worksheet
       Dim book2 As Workbook
       Dim sheet2 As Worksheet
       Application.ScreenUpdating = False
       Set book1 = ThisWorkbook
       Set book2 = Workbooks.Open("Fiscal '17 Reported Straddle Fuel Usage.xlsm")
       Set sheet1 = book1.Sheets("7500")
       Set sheet2 = book2.Sheets("Nov ‘17")

推荐答案

目标文件名未更改或移动

您未指定路径.指定路径.

You're not specifying a path. Specify a path.

Set book2 = Workbooks.Open("Fiscal '17 Reported Straddle Fuel Usage.xlsm")

Const path As String = "C:\Foo\Bar\Fiscal '17 Reported Straddle Fuel Usage.xlsm"
Set book2 = Workbooks.Open(path)

Workbooks.Open 起作用,无论 CurDir 路径如何,只要向用户显示打开", CurDir 的值都会更改.文件"对话框,他们开始浏览:没有完整路径的文件名不可靠.

Workbooks.Open works off whatever the CurDir path is, and the value of CurDir changes whenever the user is presented with an "open file" dialog and they start browsing around: a file name without a full path is unreliable.

与VBA中的许多事情一样,最好对其进行明确说明.给它一个完整的路径.

As with many, MANY things in VBA, it's better to be explicit about it. Give it a full path.

这篇关于Excel VBA-运行时错误1004打开工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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