我的宏 (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
To here:
正如 Siddharth 所提到的,还有另一种方法可以让宏在文件打开事件上运行,那就是简单地为其提供以下签名:
Sub Auto_Open
另外,就我个人而言,我可能不会将子例程称为“更新”,因为它非常接近许多保留字 - 我会选择“updateSomething”之类的东西。这只是个人选择。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)