[英]VBA Run-time error '1004': Method 'Range' of object '_Worksheet'
I know this question is similar to a lot of others posted on Stack, but none of their solutions worked for me.我知道这个问题与 Stack 上发布的许多其他问题相似,但他们的解决方案都不适合我。 I'm new to VBA so please try to make your solution easy to understand.
我是 VBA 的新手,所以请尽量让您的解决方案易于理解。 The code below essentially conditionally locks and unlock cells (not done yet; still trying to get the framework down):
下面的代码基本上有条件地锁定和解锁单元格(尚未完成;仍在尝试关闭框架):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Sheet1.Protect UserInterFaceOnly:=True
' Volatility
If Not IsEmpty(Range("B11").Value) Then
Range("B12").Value = ""
Range("B13").Value = ""
Range("B22").Value = Range("B11").Value
Range("B12:B13").Locked = True
Else
Range("B12:B13").Locked = False
End If
If IsEmpty(Range("B12").Value) And IsEmpty(Range("B13").Value) Then
Range("B11").Locked = False
Else
Select Case Range("B12").Value
Case Is = "Daily"
Range("B22").Value = Range("B13").Value * Sqr(252)
Case Is = "Weekly"
Range("B22").Value = Range("B13").Value * Sqr(52)
Case Is = "Monthly"
Range("B22").Value = Range("B13").Value * Sqr(12)
Case Is = "Annual"
Range("B22").Value = Range("B13").Value
End Select
Range("B11").Locked = True
End If
' Time
If Not IsEmpty(Range("B14").Value) Then
Range("B15").Value = ""
Range("B15").Locked = True
Range("B23").Value = Range("B14").Value / Range("B7").Value
Else
Range("B15").Locked = False
End If
If Not IsEmpty(Range("B15").Value) Then
Range("B14").Locked = True
Range("B23").Value = Range("B15").Value
Else
Range("B14").Locked = False
End If
' Dividends
If Not IsEmpty(Range("B16").Value) Then
Range("B17").Value = ""
Range("B17").Locked = True
Else
Range("B17").Locked = False
End If
If Not IsEmpty(Range("B17").Value) Then
Range("B16").Locked = True
Else
Range("B16").Locked = False
End If
Select Case Range("B6").Value
Case Is = "Cox Rox Rubinstein (1979)"
' If requirements satisfied, populate outputs
' Else make output values blank
Range("B24").Value = ""
Case Is = "Forward Tree"
Range("B24").Value = ""
Case Is = "Lognormal Tree"
Range("B24").Value = ""
Case Is = "Custom"
Range("B24").Value = ""
End Select
End Sub
The problem is, anytime I change any cell value on the sheet Excel prompts "Getting run-time 1004: Method 'Range' of object '_Worksheet' failed" without specifying which line of code, and then force quits the program.问题是,每当我更改工作表 Excel 上的任何单元格值时都会提示“获取运行时 1004:object '_Worksheet' 的方法'范围'失败”,而没有指定哪一行代码,然后强制退出程序。 See sheet here .
请参阅此处的表格。 Any help is greatly appreciated!
任何帮助是极大的赞赏!
I figured out that I was fundamentally wrong in my usage of我发现我在使用
Private Sub Worksheet_Change(ByVal Target As Range)
Without using intersect() I'm essentially addressing a range that is nonexistent.在不使用 intersect() 的情况下,我本质上是在处理一个不存在的范围。 Easy fix is the following:
简单的修复如下:
If Not Intersect(Target, Range("B2")) Is Nothing Then
After which, turning events on and off becomes relevant as to prevent infinite loops when changing the values of selected cells.之后,打开和关闭事件变得相关,以防止在更改选定单元格的值时出现无限循环。 Thanks for the tips!
感谢您的提示!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.