Option Explicit
Dim Rng As Range
Dim Clls As Range
Sub ChonCacOConLai()
Dim ContRng As Range, SelectRng As Range
Dim NoCont As Boolean
Dim TRow As Long, BRow As Long
Dim LCol As Integer, RCol As Integer
' Kiem & Loai Cac O Ngoai Khung'
Set Rng = Selection
TRow = Rng.Cells(1, 1).Row: BRow = TRow + Rng.Rows.Count - 1
LCol = Rng.Cells(1, 1).Column: RCol = LCol + Rng.Columns.Count - 1
Set Rng = Nothing
Set SelectRng = Application.InputBox(prompt:="Select a DeSelectCells", Type:=8)
For Each Clls In SelectRng
If Clls.Row >= TRow And Clls.Row <= BRow And Clls.Column >= LCol And _
Clls.Column <= RCol Then
If Rng Is Nothing Then
Set Rng = Clls
Else
Set Rng = Union(Rng, Clls)
End If: End If
Next Clls
If Rng.Cells.Count < ResizeRange(Rng).Cells.Count Then
Set ContRng = ResizeRange(Rng, False)
Set Rng = ResizeRange(Rng): NoCont = True
End If
Dim DgTr As Long, DgD As Long
Dim CotTr As Integer, CotF As Integer
DgTr = Rng.Cells(1, 1).Row: DgD = DgTr + Rng.Rows.Count - 1
CotTr = Rng.Cells(1, 1).Column: CotF = CotTr + Rng.Columns.Count - 1
Set Rng = Nothing
Dim RightR As Range, TopR As Range, LeftR As Range, BottomR As Range
If TRow = DgTr Then
If DgD < BRow Then
Set BottomR = Range(Cells(DgD + 1, LCol), Cells(BRow, RCol))
1 If CotTr = LCol And CotF < RCol Then
Set RightR = Range(Cells(TRow, CotF + 1), Cells(DgD, RCol))
Set SelectRng = Union(RightR, BottomR)
2 ElseIf CotTr > LCol And CotF < RCol Then
Set LeftR = Range(Cells(TRow, LCol), Cells(DgD, CotTr - 1))
Set RightR = Range(Cells(TRow, CotF + 1), Cells(DgD, RCol))
Set SelectRng = Union(LeftR, RightR, BottomR)
3 ElseIf CotTr > LCol And CotF = RCol Then
Set LeftR = Range(Cells(TRow, LCol), Cells(DgD, CotTr - 1))
Set SelectRng = Union(LeftR, BottomR)
4 ElseIf CotTr = LCol And CotF = RCol Then
Set SelectRng = BottomR
End If
ElseIf DgD = BRow Then
5 If CotTr = LCol And CotF < RCol Then
Set RightR = Range(Cells(TRow, CotF + 1), Cells(BRow, RCol))
Set SelectRng = RightR
6 ElseIf CotTr > LCol And CotF < RCol Then
Set LeftR = Range(Cells(TRow, LCol), Cells(BRow, CotTr - 1))
Set RightR = Range(Cells(TRow, CotF + 1), Cells(BRow, RCol))
Set SelectRng = Union(LeftR, RightR)
7 ElseIf CotTr > LCol And CotF = RCol Then
Set LeftR = Range(Cells(TRow, LCol), Cells(BRow, CotTr - 1))
Set SelectRng = LeftR
8 ElseIf CotTr = LCol And CotF = RCol Then
If NoCont Then
ContRng.Select
Else
msgbox "No Range Select."
End If
Exit Sub
End If
End If
ElseIf TRow < DgTr Then
If DgD < BRow Then
Set TopR = Range(Cells(TRow, LCol), Cells(DgTr - 1, RCol))
Set BottomR = Range(Cells(DgD + 1, LCol), Cells(BRow, RCol))
9 If LCol = CotTr And RCol > CotF Then
Set RightR = Range(Cells(DgTr, CotF + 1), Cells(DgD, RCol))
Set SelectRng = Union(TopR, RightR, BottomR)
10 ElseIf LCol < CotTr And RCol > CotF Then
Set RightR = Range(Cells(DgTr, CotF + 1), Cells(DgD, RCol))
Set LeftR = Range(Cells(DgTr, LCol), Cells(DgD, CotTr - 1))
Set SelectRng = Union(TopR, LeftR, RightR, BottomR)
11 ElseIf LCol < CotTr And RCol = CotF Then
Set LeftR = Range(Cells(DgTr, LCol), Cells(DgD, CotTr - 1))
Set SelectRng = Union(TopR, LeftR, BottomR)
12 ElseIf LCol = CotTr And RCol = CotF Then
Set SelectRng = Union(TopR, BottomR)
End If
ElseIf DgD = BRow Then
Set TopR = Range(Cells(TRow, LCol), Cells(DgTr - 1, RCol))
13 If LCol = CotTr And RCol > CotF Then
Set RightR = Range(Cells(DgTr, CotF + 1), Cells(BRow, RCol))
Set SelectRng = Union(TopR, RightR)
14 ElseIf LCol < CotTr And RCol > CotF Then
Set RightR = Range(Cells(DgTr, CotF + 1), Cells(BRow, RCol))
Set LeftR = Range(Cells(DgTr, LCol), Cells(BRow, CotTr - 1))
Set SelectRng = Union(TopR, LeftR, RightR)
15 ElseIf LCol < CotTr And RCol = CotF Then
Set LeftR = Range(Cells(DgTr, LCol), Cells(BRow, CotTr - 1))
Set SelectRng = Union(TopR, LeftR)
16 ElseIf LCol = CotTr And RCol = CotF Then
Set SelectRng = TopR
End If
End If
End If
If NoCont Then
Union(SelectRng, ContRng).Select
Else
SelectRng.Select
End If
End Sub