簡體   English   中英

VBScript打開Excel,然后添加VBA宏

[英]VBScript to open Excel then add a VBA macro

我需要一個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

跟着這些步驟:

  1. 在Excel中打開VBA編輯器,然后添加一個新模塊。
  2. 將您的宏代碼粘貼到其中。
  3. 右鍵單擊模塊,然后選擇Export...
  4. 給它一個文件名並將其保存在某個地方。
  5. 在您的VBScript中,添加以下代碼行:

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

    請注意,在Excel 2007+中,您不能將宏保存在xlsx文件中。 您將需要使用SaveAs代替,並為文件指定xslm擴展名。 或者,您可以使用舊的xls格式(這就是您的示例所使用的格式)。

這不是直截了當的,但是我要做的是使用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

你可以在這里這里閱讀更多

但是另一種方法是使用鼠標和鍵盤宏記錄器(可以編程為模仿動作的獨立應用程序)。 我個人已經使用KeyText十多年了。

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM