如何在Excel中获取上一个单元格的范围?我有一个ComboBox,通常我可以使用ActiveCell.Value = box.Value
将其值填充到活动单元格(在ComboBox下)。当我选择了我的ComboBox的值并单击任何其他单元格时,我希望将ComboBox的值写入前一个单元格,但是此代码将其写入我单击的单元格:
Private Sub box_LostFocus()
ActiveCell.Value = box.Value
End Sub
有什么想法吗?
答案 0 :(得分:3)
如果您在工作表代码中包含此内容,则PreviousCell将始终是您选择的上一个范围。
Dim PreviousCell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Your code that uses PreviousCell should go in the if statement that makes sure PreviousCell has a value
If Not PreviousCell Is Nothing Then
Debug.Print PreviousCell.Address
End If
Set PreviousCell = Target ' This needs to be the last line of code.
End Sub