[英]Excel, VBA, can't get cell from row and column ID
I am working on the function Worksheet_Change and I have an issue. 我正在使用Worksheet_Change函数,但是有一个问题。 I cannot get a cell using the row and the column ID.
我无法使用行和列ID获得单元格。
I have tried a lot of solutions but nothing. 我尝试了很多解决方案,但是什么也没有。
Could you help me please? 请问你能帮帮我吗?
This is my script: 这是我的脚本:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim myCell As Range
Set KeyCells = Range("G4:N19")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
selectedRow = Target.Row
myColumn = Range("F" & 1).Column
With Sheets(1)
' selectedRow = 16, myColumn = 6
myCell = .Cells(selectedRow, myColumn)
' myCell is empty
MsgBox myCell
End With
End If
End Sub
Thank you for your help. 谢谢您的帮助。
You need to use Set when assigning a range (object) variable (as you did earlier in the code). 分配范围(对象)变量时,需要使用Set (就像您在代码前面所做的那样)。
Set myCell = .Cells(selectedRow, myColumn)
You should always declare all your variables too (and note Rory's point). 您还应该始终声明所有变量(并注意Rory的观点)。 The myColumn line looks somewhat redundant too.
myColumn行看起来也有些多余。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim myCell As Range
Dim selectedRow As Long
Dim myColumn As Long
Set KeyCells = Range("G4:N19")
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
selectedRow = Target.Row
myColumn = Range("F1").Column
With Sheets(1)
' selectedRow = 16, myColumn = 6
Set myCell = .Cells(selectedRow, myColumn)
' myCell is empty
MsgBox myCell.Value
End With
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.