Excel Worksheet_Change事件不工作 [英] Excel Worksheet_Change Event not working

查看:981
本文介绍了Excel Worksheet_Change事件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写一个宏,更改任何列
应自动保存工作表。我的Excel表展开至 G25



我尝试过这个但不起作用:

 code> Private Sub Worksheet_Change(ByVal Target As Range)
如果不相交(Target,Target.Worksheet.Range(G25))没有,然后
ActiveWorkbook.Save
结束Sub

我已将其保存在 ThisWorkBook 下。



任何帮助都不胜感激。

解决方案

ThisWorkbook 该处理程序称为 Workbook_SheetChange ,它接受两个参数, Sh (类型为 Object )和目标(一个范围)。所以,你的代码不会在那里工作。

如果你把你的代码放在你想要的工作表中(例如, Sheet1 ),而不是在 ThisWorkbook ,将 End If 并将范围更改为A1:G25(表示从第1列到第25行的平方第25栏),它应该工作。
这样做:

  Private Sub Worksheet_Change(ByVal Target As Range)
如果不相交(目标, Target.Worksheet.Range(A1:G25))是Nothing Then
MsgBoxchanged
End If
End Sub

为了完整,在 ThisWorkbook 下,这将工作:

  Private Sub Workbook_SheetChange(ByVal Sh As Object,ByVal Target As Range)
如果不相交(Target,Target.Worksheet.Range(A1:G25))没有,然后
MsgBoxchanged
End If
End Sub


I am trying to write a macro where changing any column should automatically save worksheet.

My Excel sheet expands till G25.

I tried this but its not working:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("G25")) Is Nothing Then
        ActiveWorkbook.Save
End Sub

I have save it under ThisWorkBook.

Any help is appreciated.

解决方案

Under ThisWorkbook that handler is called Workbook_SheetChange and it accepts two arguments, Sh (of type Object) and Target (a Range). So, your code won't work there.
If you put your code in the sheet you want (Sheet1, for instance) instead of in the ThisWorkbook, put the End If and change the range to "A1:G25" (representing a square from row 1 column A to row 25 column 25), it should work. This did:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
        MsgBox "changed"
    End If
End Sub

For completeness, under ThisWorkbook, this will work:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
        MsgBox "changed"
    End If
End Sub

这篇关于Excel Worksheet_Change事件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆