开发者

Getting the selected cell's range from a different worksheet in Excel

开发者 https://www.devze.com 2023-01-06 17:16 出处:网络
I\'m trying to set up Excel so that the cell\'s value that is selected in the first worksheet is set to the value of a cell that\'s double clicked in a different worksheet. So far my code looks like t

I'm trying to set up Excel so that the cell's value that is selected in the first worksheet is set to the value of a cell that's double clicked in a different worksheet. So far my code looks like this:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)



Dim c As Range

For Each c In Sheet1.Range("M11:M24")
    If IsEmpty(c) Then
        c.Value = Target.Value
   开发者_C百科     Exit For
    End If
Next c
End Sub

What this does is sets the first empty cell in the range m11:m24 to the contents of the double clicked cell in the other worksheet. What I want though is not a static "M11:M24" range, but instead have the user select a cell in the first worksheet by clicking on it, move to the other worksheet, double click a cell in that worksheet and have the value appear in the selected cell on the first worksheet. I think I could have it so that there is a variable set up to save which cell is selected in the first worksheet and then just access that from the other worksheet. But I'd prefer if there was away built in to Excel to just choose the selected cell.

Is there a way to get the selected cell/range in Excel?


I solved this easily. The code is:

Sheet1.Activate
ActiveCell.Value = Target.Value

If you want to do a whole selection, try

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Sheet1.Activate

    Dim r As Range
    Set r = Selection

    r.Value = Target.Value

End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号