【问题标题】:Run Macro on CSV file that is stored in another file在存储在另一个文件中的 CSV 文件上运行宏
【发布时间】:2018-10-02 11:24:40
【问题描述】:

我每天收到一个 CSV 文件,我使用存储在我的个人宏文件中的宏对其进行格式化。我正在尝试创建一个 vbs 文件,该文件将通过计划任务在 CSV 文件上运行宏,但我没有运气。这是vbs文件:

Option Explicit

ExcelMacroExample

Sub ExcelMacroExample() 
  Dim xlApp 
  Dim xlBook 

  Set xlApp = CreateObject("Excel.Application") 
  Set xlBook = xlApp.Workbooks.Open("c:\users\dmcgettigan\desktop\test.csv", 0, True) 
  xlApp.Run "c:\users\dmcgettigan\desktop\Personal.xlsb!newsales"
  xlApp.Quit 

  Set xlBook = Nothing 
  Set xlApp = Nothing 
End Sub 

这是我得到的错误:

C:\Users\dmcgettigan\Desktop\test.vbs(13, 3) Microsoft Excel:无法运行宏 'c:\users\dmcgettigan\desktop\Personal.xlsb!new sales'。该工作簿中的宏可能不可用,或者所有宏都可能被禁用。

关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: excel vba vbscript


    【解决方案1】:

    对我来说就是这样

      Dim xlApp 
    
      Set xlApp = CreateObject("Excel.Application") 
      xlapp.workbooks.open("<Path to your PERSONAL.XLSB>")
      xlApp.Run "PERSONAL.XLSB!Test"  
      xlApp.Quit 
    

    注释也对,你要运行的子名称不能有空格。

    更新如果无法对其进行测试,您的代码应该如下所示

    Option Explicit
    
    ExcelMacroExample
    
    Sub ExcelMacroExample() 
      Dim xlApp 
      Dim xlBook 
    
      Set xlApp = CreateObject("Excel.Application") 
      Set xlBook = xlApp.Workbooks.Open("c:\users\dmcgettigan\desktop\test.csv", 0, True) 
      xlapp.workbooks.open("c:\users\dmcgettigan\desktop\Personal.xlsb")
      xlApp.Run "Personal.xlsb!newsales"
      xlApp.Quit 
    
      Set xlBook = Nothing 
      Set xlApp = Nothing 
    End Sub
    

    【讨论】:

    • 这可以打开我的个人宏,但我只需要该文件中的宏在 test.csv 文件上运行。
    • 如果你想从personal.xlsb 运行一个宏,你首先必须打开它。或者我应该如何理解你的评论?
    • 有道理,但是我应该把你的代码放在哪里,在现有代码之前还是之后?
    • 你必须重写现有的代码,至少一点点。
    • 我不知道宏 newsales 做了什么,但错误消息是说宏不存在。 IMO 在您推测文件是否以只读方式打开之前,您必须先检查这一点。
    猜你喜欢
    • 2019-07-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多