0

I have the following VBA script:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.ScreenUpdating = False

    If Not Application.Intersect(Target, Range("calendar")) Is Nothing Then
       [selectedCell1] = ActiveCell.Value

Application.ScreenUpdating = True

    End If
End Sub

Currently, It recognizes only one cell is highlighted and returns it into the specific cell named selectedCell1.

This is my example: enter image description here

If I select the cell N25 which contains the date "03/08/2017" it returns "03/08/2017" into another sheet cell named "selectedCell1".

But what I would like it to do, is realize I've selected the entire week, and then return that entire week range in cell "selectedCell1". See: enter image description here

And then return 01/08/2017 - 05/08/2017 (that entire range) in cell "selecetedCell1".

Not sure how to adjust this VBA script. Help would be appreciated. Thanks.

1
  • [selectedCell1].Resize(Target.Rows.Count, Target.Columns.Count) = Target.Value Don't use ActiveCell :) Commented Jul 31, 2017 at 18:21

1 Answer 1

1
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.ScreenUpdating = False

    If Not Application.Intersect(Target, Range("calendar")) Is Nothing Then
        If Target.Cells.Count = 1 Then
            [selectedCell1] = Target.Value
        Else
            [selectedCell1] = Format(Application.WorksheetFunction.Min(Target), "dd/mm/yyyy") & " - " & Format(Application.WorksheetFunction.Max(Target), "dd/mm/yyyy")
        End If

Application.ScreenUpdating = True

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.