首页 文章

VBScript打开Excel然后添加一个VBA宏

提问于
浏览
1

我需要一个VBScript来打开某个Excel文档,然后在打开时它必须添加一个宏并保存 .

我可以打开Excel文档,但我不知道如何打开宏屏幕(Alt F11),然后添加代码并保存...

反正有没有这样做?

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\test.xls")
objExcel.Application.DisplayAlerts = False
objExcel.Application.Visible = True`

'Macro Script

Sub HideRows()
    Dim cell As Range   
    For Each cell In Range("H1:W200")
        If Not isEmpty(cell) Then
            If cell.Value <> "" And cell.Value = 0 Then 
                cell.EntireRow.Hidden = True
                Columns("H").EntireColumn.Hidden = True
                Columns("I").EntireColumn.Hidden = True
                Columns("J").EntireColumn.Hidden = True
                Columns("M").EntireColumn.Hidden = True
                Columns("N").EntireColumn.Hidden = True
                Columns("O").EntireColumn.Hidden = True
                Columns("P").EntireColumn.Hidden = True
                Columns("Q").EntireColumn.Hidden = True
                Columns("S").EntireColumn.Hidden = True
                Columns("T").EntireColumn.Hidden = True
                Columns("V").EntireColumn.Hidden = True
            End If
        End If
    Next
End Sub

3 回答

  • 1

    跟着这些步骤:

    • 在Excel中打开VBA编辑器并添加新模块 .

    • 将宏代码粘贴到其中 .

    • 右键单击Module并选择 Export... .

    • 给它一个文件名并将其保存在某个地方 .

    • 在VBScript中,添加以下代码行:

    objWorkbook.VBProject.VBComponents.Import "/path/to/your/module.bas"
    objWorkbook.Save
    

    请注意,在Excel 2007中,无法在 xlsx 文件中保存宏 . 您需要使用 SaveAs ,并为该文件提供 xslm 扩展名 . 或者,您可以使用旧的 xls 格式(这是您在示例中使用的格式) .

  • 0

    这不是直截了当的,但我要做的是使用 SendKeys 函数来模拟Alt F11 .

    Application.SendKeys "%{F11}", True
    

    然后使用相同的逻辑,使用击键导航到正确的窗口,添加模块,然后使用以下方法将宏代码粘贴到正确的位置:

    Application.SendKeys ""^V"
    Application.SendKeys ""^V", True   'Incase that one above does not work
    

    然后你可以保存使用:

    Application.SendKeys ""^S", True
    

    你可以阅读更多关于herehere的信息

    但另一种方法是使用鼠标和键盘宏录制器(可以编程为模仿动作的独立应用程序) . 我个人已经使用了KeyText超过10年来做这种事情 .

  • 0

    您可以使用 VBProject 对象的 VBComponents 对象以编程方式添加代码 . 所以在代码的最后一行后面添加:

    Set objModule = objworkbook.VBProject.VBComponents.Add(1)       ' 1 = vbext_ct_StdModule
    
    objExcel.Visible = True    ' not necessary if you close Excel anyway
    
    theSource = ""
    theSource = theSource & "Sub HideRows()" & vbCrLf
    theSource = theSource & "    Dim cell As Range   " & vbCrLf
    theSource = theSource & "    For Each cell In Range(""H1:W200"")" & vbCrLf
    theSource = theSource & "        If Not isEmpty(cell) Then" & vbCrLf
    theSource = theSource & "            If cell.Value <> """" And cell.Value = 0 Then " & vbCrLf
    theSource = theSource & "                cell.EntireRow.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""H"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""I"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""J"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""M"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""N"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""O"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""P"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""Q"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""S"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""T"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""V"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "            End If" & vbCrLf
    theSource = theSource & "        End If" & vbCrLf
    theSource = theSource & "    Next" & vbCrLf
    theSource = theSource & "End Sub" & vbCrLf
    
    objModule.CodeModule.AddFromString theSource
    
    'objExcel.Quit
    
    'Set objExcel = Nothing
    

相关问题