You are on page 1of 7

Sub LOC_noiluc_DAM()

Call sohieu

Call ghicongthuc

Call fillcongthuc

Application.ScreenUpdating = False

' lam noi tieu de

Range("S2:W3").Select

With Selection.Font

.Color = -16776961

End With

Selection.Font.Bold = True

With Selection

.HorizontalAlignment = xlCenter

End With

' tra cuu lai gia tri phan tu

Columns("S:T").Select

Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Columns("S:T").Select

Selection.NumberFormat = "General"

With Selection

.HorizontalAlignment = xlCenter

End With

Range("S4").Formula = "=INDEX(A:A,MATCH(R4,C:C,0))"
Range("T4").Formula = "=INDEX(B:B,MATCH(R4,C:C,0))"

' dien x tai vi tri cuoi cung

Dim iLastR0ows2 As Integer

iLastRows2 = Range("R" & Rows.Count).End(xlUp).Row

Range("S" & iLastRows2).Value = "x"

Range("T" & iLastRows2).Value = "x"

' Fill cong thuc

Range("S4:T4").Select

Range(Selection, Selection.End(xlDown)).Select

Selection.FillDown

' Copy lai cong thuc va Paste value de bao ve du lieu

Columns("S:T").Select

Selection.Copy

Range("S1").Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

:=False, Transpose:=False

Application.CutCopyMode = False

Range("A1").Select
Application.ScreenUpdating = True

End Sub

Sub sohieu()

Application.ScreenUpdating = False

' Cap nhat so hieu phan tu

Columns("R:R").Select

Selection.Clear

Range("C4").Select

Range(Selection, Selection.End(xlDown)).Select

Selection.Copy

Range("R4").Select

ActiveSheet.Paste

Application.CutCopyMode = False

ActiveSheet.Range("$R$4:$R$1000000").RemoveDuplicates Columns:=1, Header:=xlNo

End Sub

' ham chuyen text thanh cong thuc

Function TTF(Ref As String)

Application.Volatile

TTF = Evaluate(Ref)

End Function

Sub ghicongthuc()

Application.ScreenUpdating = False
' Ghi cong thuc

Range("Y1").Formula = "=Y2&""_MAX FORMULA AS TEXT"""

Range("AA1").Formula = "=AA2&""_MAX FORMULA AS TEXT"""

Range("AC1").Formula = "=AC2&""_MAX FORMULA AS TEXT (1-1) (2-2) (3-3)"""

Range("S2").Formula = "Step"

Range("T2").Formula = "Begin"

Range("U2").Formula = "Begin plus"

Range("V2").Formula = "Mid"

Range("W2").Formula = "Mid plus"

Range("X2").Formula = "End"

Range("Y2").Formula = "V2"

Range("Z2").Formula = "V2"

Range("AA2").Formula = "T"

Range("AB2").Formula = "T"

Range("AC2").Formula = "M3"

Range("AD2").Formula = "M3"

Range("AE2").Formula = "M3"

Range("AF2").Formula = "M1-1"

Range("AG2").Formula = "M2-2"

Range("AH2").Formula = "M3-3"

Range("Y3").Formula = "=MATCH(Y2,2:2,0)"

Range("Z3").Formula = "Value"

Range("AA3").Formula = "=MATCH(AA2,2:2,0)"

Range("AB3").Formula = "Value"

Range("AC3").Formula = "=MATCH(AC2,2:2,0)"

Range("AD3").Formula = "=MATCH(AD2,2:2,0)"

Range("AE3").Formula = "=MATCH(AE2,2:2,0)"

Range("AF3").Formula = "Value"

Range("AG3").Formula = "Value"
Range("AH3").Formula = "Value"

Range("S4").Formula = "=COUNTIF(C:C,R4)"

Range("T4").Formula = "4"

Range("U4").Formula = "=ROUNDDOWN((V4-T4)/2,0)+T4"

Range("V4").Formula = "=T4+S4/2-1"

Range("W4").Formula = "=ROUNDDOWN((X4-V4)/2,0)+V4"

Range("X4").Formula = "=T4+S4-1"

Range("Y4").Formula =
"=CONCATENATE(""MAX(MAX("",ADDRESS(T4,$Y$3,4),"":"",ADDRESS(X4,$Y$3,4),""),ABS(MIN("",ADDRE
SS(T4,$Y$3,4),"":"",ADDRESS(X4,$Y$3,4),"")))"")"

Range("Z4").Formula = "=TTF(Y4)"

Range("AA4").Formula =
"=CONCATENATE(""MAX(MAX("",ADDRESS(T4,$AA$3,4),"":"",ADDRESS(X4,$AA$3,4),""),ABS(MIN("",AD
DRESS(T4,$AA$3,4),"":"",ADDRESS(X4,$AA$3,4),"")))"")"

Range("AB4").Formula = "=TTF(AA4)"

Range("AC4").Formula =
"=CONCATENATE(""MIN(MIN("",ADDRESS(T4,$AC$3,4),"":"",ADDRESS(U4,$AC$3,4),""),MIN("",ADDRESS
(V4,$AC$3,4),"":"",ADDRESS(W4,$AC$3,4),""))"")"

Range("AD4").Formula =
"=CONCATENATE(""MAX("",ADDRESS(T4,$AD$3,4),"":"",ADDRESS(X4,$AD$3,4),"")"")"

Range("AE4").Formula =
"=CONCATENATE(""MIN(MIN("",ADDRESS(U4,$AC$3,4),"":"",ADDRESS(V4,$AC$3,4),""),MIN("",ADDRESS
(W4,$AC$3,4),"":"",ADDRESS(X4,$AC$3,4),""))"")"

Range("AF4").Formula = "=TTF(AC4)"

Range("AG4").Formula = "=TTF(AD4)"

Range("AH4").Formula = "=TTF(AE4)"

Range("T5").Formula = "=T4+S4"

Application.ScreenUpdating = True

End Sub
Sub fillcongthuc()

Application.ScreenUpdating = False

' Tim dong cuoi va dien cong thuc

Dim iLastRows As Integer

iLastRows = Range("R" & Rows.Count).End(xlUp).Row

Range("S" & iLastRows).Value = "x"

Range("T" & iLastRows).Value = "x"

Range("U" & iLastRows).Value = "x"

Range("AH" & iLastRows).Value = "x"

' Fill cong thuc

Range("S4").Select

Range(Selection, Selection.End(xlDown)).Select

Selection.FillDown

Range("T5").Select

Range(Selection, Selection.End(xlDown)).Select

Selection.FillDown

Range("U4:AH4").Select

Range(Selection, Selection.End(xlDown)).Select

Selection.FillDown
' Copy lai cong thuc va Paste value de bao ve du lieu

Columns("R:AH").Select

Selection.Copy

Range("R1").Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

:=False, Transpose:=False

' Xoa dong khong can thiet

Columns("S:Y").Select

Application.CutCopyMode = False

Selection.Delete Shift:=xlToLeft

Columns("T:T").Select

Selection.Delete Shift:=xlToLeft

Columns("U:W").Select

Selection.Delete Shift:=xlToLeft

Application.ScreenUpdating = True

End Sub

You might also like