Excel VBA:调用LostFocus()后获取上一个单元格的范围 [英] Excel VBA: Get range of previous cell after calling LostFocus()

查看:55
本文介绍了Excel VBA:调用LostFocus()后获取上一个单元格的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取Excel中上一个单元格的范围?我有一个ComboBox,通常我可以使用 ActiveCell.Value = box.Value 将其值填充到活动单元格中(在ComboBox下).当我选择了ComboBox的值并单击任何其他单元格后,我希望将ComboBox的值写入到先前的单元格中,但是此代码将其写入到我单击的单元格中:

How can I get the range of a previous cell in Excel? I have a ComboBox and usually I can fill its value into the active cell (under the ComboBox) with ActiveCell.Value = box.Value. When I have selected a value of my ComboBox and click in any other cell, I want the value of the ComboBox to be written into the previous cell, but this code writes it to cell I clicked on:

Private Sub box_LostFocus()
  ActiveCell.Value = box.Value
End Sub

有什么想法吗?

推荐答案

如果将其包含在工作表代码中,PreviousCell将始终是您选择的先前范围.

If you include this in your worksheet code, PreviousCell will always be the previous range you had selected.

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

这篇关于Excel VBA:调用LostFocus()后获取上一个单元格的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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