gpt4 book ai didi

vba - Excel VBA 代码返回错误

转载 作者:行者123 更新时间:2023-12-04 21:11:35 28 4
gpt4 key购买 nike

有这个 vba 的 excel 文件:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Target, Range("A1:A1000")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
End Sub

这是做什么的,当在这些单元格上插入一些东西时,它会将其转换为大写字母。一切正常,只是一个小问题......文件每天被几个人使用,所以插入的数据每天都会被删除几次。发生的情况是,如果我一次删除一个单元格,它运行平稳,如果我同时删除多个单元格,我会收到运行时错误“13”。

我该如何纠正这个?

最佳答案

当 Target 不止一个单元格时,遍历 Target 中的每个匹配单元格。

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A1000") Is Nothing Then
dim trgt as range
for each trgt in Intersect(Target, Range("A1:A1000")
With trgt
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
next trgt
End If
End Sub

关于vba - Excel VBA 代码返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49673608/

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