gpt4 book ai didi

vba - 打开工作表时自动执行的宏

转载 作者:行者123 更新时间:2023-12-03 02:49:59 62 4
gpt4 key购买 nike

每次打开 Excel 文件时,我的宏 (update () ) 是否有可能自动执行。下面的代码不能很好地工作。谢谢

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

到这里:

enter image description here

正如 Siddharth 所提到的,还有另一种方法可以让宏在文件打开事件上运行,那就是简单地为其提供以下签名:

Sub Auto_Open

另外,就我个人而言,我可能不会将子例程称为“update”,因为它非常接近许多保留字 - 我会选择“updateSomething”之类的名称。这只是个人选择。

关于vba - 打开工作表时自动执行的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21578605/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com