2017-07-31 73 views
0

我有以下VBA腳本:VBA識別多個單元格被突出顯示

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 

目前,它只能識別一個單元被突出顯示,並返回到名爲selectedCell1特定小區。

這是我的例子: enter image description here

如果我選擇包含日期「2017年3月8日」返回「2017年3月8日」到一個名爲「selectedCell1」另一片小區的小區N25。

但是我想做的事情是意識到我選擇了整整一週,然後在單元格「selectedCell1」中返回整個星期的範圍。參見: enter image description here

然後在單元格「selecetedCell1」中返回01/08/2017 - 05/08/2017(表示整個範圍)。

不知道如何調整這個VBA腳本。幫助將不勝感激。謝謝。

+0

'[selectedCell1] .Resize(Target.Rows.Count,Target.Columns.Count)= Target.Value'不要使用ActiveCell :) –

回答

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 
相關問題