在打开表单时自动执行的macros

是否有可能我的macros(update())每次打开excel文件时自动执行。 下面的代码不能正常工作。 谢谢

Private Sub Workbook_Open() Run "update" End Sub 

 Option Explicit Sub update() Dim rng As Range Dim Sh As String, Cl As String Dim ws As Worksheet Dim i As Integer, ncol As Integer Dim Row1 As String ncol = Range("B1:O1").Columns.Count For i = 1 To ncol Set ws = ThisWorkbook.Sheets("sheet1") With ws Row1 = .Cells(1, i).Value If Len(Row1) > 0 Then Sh = Split(Row1, "'!")(0) Cl = Split(Row1, "'!")(1) Set rng = ThisWorkbook.Sheets(Sh).Range(Cl) 'Here you were always refering to cell A2 not moving through the values which was the main problem. rng.Value = .Cells(2, i).Value End If End With Next i End Sub 

正如评论中提到的那样。 移动以下内容:

 Private Sub Workbook_Open() Run "update" End Sub 

到这里:

在这里输入图像说明

正如Siddharth所提到的,还有另外一种方式让macros运行在文件打开事件上,那就是简单地给它下面的签名:

 Sub Auto_Open 

另外,个人而言,我可能不会调用子程序“更新”,因为它非常接近大量的保留字 – 我会去“updateSomething”之类的东西。 这只是个人select。