Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:调用LostFocus()后获取上一个单元格的范围_Vba_Excel - Fatal编程技术网

Excel VBA:调用LostFocus()后获取上一个单元格的范围

Excel VBA:调用LostFocus()后获取上一个单元格的范围,vba,excel,Vba,Excel,如何获取Excel中上一个单元格的范围?我有一个组合框,通常我可以用ActiveCell.value=box.value将其值填入活动单元格(在组合框下)。当我选择ComboBox的值并在任何其他单元格中单击时,我希望将ComboBox的值写入上一个单元格,但此代码将其写入我单击的单元格: Private Sub box_LostFocus() ActiveCell.Value = box.Value End Sub 有什么想法吗?如果您在工作表代码中包含此内容,则PreviousCell

如何获取Excel中上一个单元格的范围?我有一个组合框,通常我可以用
ActiveCell.value=box.value
将其值填入活动单元格(在组合框下)。当我选择ComboBox的值并在任何其他单元格中单击时,我希望将ComboBox的值写入上一个单元格,但此代码将其写入我单击的单元格:

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

有什么想法吗?

如果您在工作表代码中包含此内容,则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

你说的前一个单元格是什么意思?