【问题标题】:Calling a personal macro using VB script failed with - Method 'Cells' of Object'_Global' failed VBA使用 VB 脚本调用个人宏失败 - Object'_Global' 的方法 'Cells' 失败 VBA
【发布时间】:2016-06-17 17:45:14
【问题描述】:
I am getting error while running the below vb script to run the personal macro stored on an macro enabled workbook. `"Method 'Sheets' of object '_Global' failed"`

    'Open an instance of Excel
    Set objExcel=CreateObject("Excel.Application")
    objExcel.Visible=True

设置 objWorkbook=objExcel.Workbooks.Open("C:\Microsoft\Excel\XLSTART\PERSONAL.XLSB") '调用宏 objExcel.Run "PERSONAL.XLSB!PLAN"

' Macro which is being called by the above script

     Option Explicit
    Public NewCols As Long          ' EGPO

    Dim i As Long
    Dim colx As Long

    Sub PLAN()
        Application.ScreenUpdating = False

        Call PLAN
         Application.ScreenUpdating = True
    End Sub

    Private Sub LatestCRDTwo()
    On Error Resume Next
    Dim wb As Workbook
    Dim P As String, Q As String
        P = "Pass\Fail"
        Q = "Test Cases"
    Application.ScreenUpdating = False

' 将使用任何启用宏的工作簿
设置 wb = ActiveWorkbook

    With Sheets("EGPO")
        Sheets("EGPO").Select
        NewCols = .UsedRange.Columns.Count
        .AutoFilterMode = False
' Add columns starting at column 4 and insert columns at every 2nd column
        For colx = 4 To 40 Step 2 
            NewCols = .UsedRange.Columns.Count
            Columns(colx).Insert Shift:=xlToRight
            NewCols = NewCols + 1
        Next colx
    End With
    On Error Resume Next
'Below "With statements" is to copy the values in the inserted columns
    With Sheets("EGPO")
        Sheets("EGPO").Select
        NewCols = .UsedRange.Columns.Count

' 循环粘贴值 对于 i = 4 到 NewCols 步骤 2 如果 wb.Worksheets("EGPO").Cells(1, 1).Value = "" 那么 wb.Worksheets("EGPO").Cells(2, i) = P wb.Worksheets("EGPO").Cells(2, i).Interior.ColorIndex = 15 wb.Worksheets("EGPO").Cells(1, i) = Q 万一 新列 = 新列 + 1 接下来我

    End With
    On Error Resume Next
    Application.ScreenUpdating = True
    End Sub

【问题讨论】:

  • 错误可能在您正在调用的宏中。你能把它的内容贴在这里吗?
  • @Isaac - 我在帖子中的 vb 脚本下方添加了宏代码,谢谢

标签: excel vbscript vba


【解决方案1】:

我认为这只是一个模板

C:\Microsoft\Excel\XLSTART\PERSONAL.XLSB

使用 Workbooks("Personal.xlsb").FullName 查找实际路径

C:\Users\best buy\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB

显然 PERSONAL.XLSB!PLAN 正在运行,因为这就是引发错误的原因。我认为问题是没有活动的工作簿。尝试激活或选择隐藏工作表上的工作表可能会引发此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多