![](/img/trans.png)
[英]How can I iterate from a specific Excel cell to the latest row having a value in this column using VBA?
[英]Excel, VBA, can't get cell from row and column ID
我正在使用Worksheet_Change函數,但是有一個問題。 我無法使用行和列ID獲得單元格。
我嘗試了很多解決方案,但是什么也沒有。
請問你能幫幫我嗎?
這是我的腳本:
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
謝謝您的幫助。
分配范圍(對象)變量時,需要使用Set (就像您在代碼前面所做的那樣)。
Set myCell = .Cells(selectedRow, myColumn)
您還應該始終聲明所有變量(並注意Rory的觀點)。 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.