Rút Gọn Code Tạo Name.

Liên hệ QC

emgaingayngo

Thành viên hoạt động
Tham gia
9/2/07
Bài viết
141
Được thích
5
GPE cho em hỏi có cách nào rút gọn code sau:
PHP:
Sub TaoName()MotxMot
Dim the As String
S02.Activate
the = Range("The").Value
Range("L3").Select
If the = "TTMotxMot" Then '  MotxMot ->MotxChin
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[12])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[12])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[23])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[23])>=1,RC[22],""""))"
ElseIf the = "TTMotxHai" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[13])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[13])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[24])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[24])>=1,RC[22],""""))"
ElseIf the = "TTMotxBa" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[14])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[14])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[25])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[25])>=1,RC[22],""""))"
ElseIf the = "TTMotxBon" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[15])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[15])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[26])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[26])>=1,RC[22],""""))"
ElseIf the = "TTMotxNam" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[16])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[16])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[27])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[27])>=1,RC[22],""""))"
ElseIf the = "TTMotxSau" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[17])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[17])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[28])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[28])>=1,RC[22],""""))"
ElseIf the = "TTMotxBay" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[18])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[18])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[29])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[29])>=1,RC[22],""""))"
ElseIf the = "TTMotxTam" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[19])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[19])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[30])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[30])>=1,RC[22],""""))"
ElseIf the = "TTMotxChin" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[20])+COUNTIF(Table2So,R[1]C[11])+COUNTIF(Table2So,R[1]C[20])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[31])+COUNTIF(Data2So,R[1]C[22])+COUNTIF(Data2So,R[1]C[31])>=1,RC[22],""""))"
 
ElseIf the = "TTHaixMot" Then 'HaixMot
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[12])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[12])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[23])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[23])>=1,RC[22],""""))"
ElseIf the = "TTHaixHai" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[13])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[13])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[24])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[24])>=1,RC[22],""""))"
ElseIf the = "TTHaixBa" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[14])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[14])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[25])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[25])>=1,RC[22],""""))"
ElseIf the = "TTHaixBon" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[15])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[15])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[26])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[26])>=1,RC[22],""""))"
ElseIf the = "TTHaixNam" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[16])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[16])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[27])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[27])>=1,RC[22],""""))"
ElseIf the = "TTHaixSau" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[17])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[17])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[28])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[28])>=1,RC[22],""""))"
ElseIf the = "TTHaixBay" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[18])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[18])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[29])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[29])>=1,RC[22],""""))"
ElseIf the = "TTHaixTam" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[19])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[19])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[30])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[30])>=1,RC[22],""""))"
ElseIf the = "TTHaixChin" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[20])+COUNTIF(Table2So,R[2]C[11])+COUNTIF(Table2So,R[2]C[20])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[31])+COUNTIF(Data2So,R[2]C[22])+COUNTIF(Data2So,R[2]C[31])>=1,RC[22],""""))"
 
ElseIf the = "TTBaxMot" Then 'BaxMot
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[12])+COUNTIF(Table2So,R[3]C[11])+COUNTIF(Table2So,R[3]C[12])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[23])+COUNTIF(Data2So,R[3]C[22])+COUNTIF(Data2So,R[3]C[23])>=1,RC[22],""""))"
ElseIf the = "TTBaxHai" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[13])+COUNTIF(Table2So,R[3]C[11])+COUNTIF(Table2So,R[3]C[13])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[24])+COUNTIF(Data2So,R[3]C[22])+COUNTIF(Data2So,R[3]C[24])>=1,RC[22],""""))"
ElseIf the = "TTBaxBa" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[14])+COUNTIF(Table2So,R[3]C[11])+COUNTIF(Table2So,R[3]C[14])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[25])+COUNTIF(Data2So,R[3]C[22])+COUNTIF(Data2So,R[3]C[25])>=1,RC[22],""""))"
ElseIf the = "TTBaxBon" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[15])+COUNTIF(Table2So,R[3]C[11])+COUNTIF(Table2So,R[3]C[15])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[26])+COUNTIF(Data2So,R[3]C[22])+COUNTIF(Data2So,R[3]C[26])>=1,RC[22],""""))"
ElseIf the = "TTBaxNam" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[16])+COUNTIF(Table2So,R[3]C[11])+COUNTIF(Table2So,R[3]C[16])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[27])+COUNTIF(Data2So,R[3]C[22])+COUNTIF(Data2So,R[3]C[27])>=1,RC[22],""""))"
ElseIf the = "TTBaxSau" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[17])+COUNTIF(Table2So,R[3]C[11])+COUNTIF(Table2So,R[3]C[17])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[28])+COUNTIF(Data2So,R[3]C[22])+COUNTIF(Data2So,R[3]C[28])>=1,RC[22],""""))"
ElseIf the = "TTBaxBay" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[18])+COUNTIF(Table2So,R[3]C[11])+COUNTIF(Table2So,R[3]C[18])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[29])+COUNTIF(Data2So,R[3]C[22])+COUNTIF(Data2So,R[3]C[29])>=1,RC[22],""""))"
ElseIf the = "TTBaxTam" Then
    ActiveWorkbook.Names.Add _
        Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[19])+COUNTIF(Table2So,R[3]C[11])+COUNTIF(Table2So,R[3]C[19])>=1,"""",RC[11]))"
    ActiveWorkbook.Names.Add _
        Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[30])+COUNTIF(Data2So,R[3]C[22])+COUNTIF(Data2So,R[3]C[30])>=1,RC[22],""""))"
ElseIf the = "TTBaxChin" Then
.........
End If
End Sub
 
Mình nghĩ bạn nên đưa file lên đi. Sẽ có cách giải khác cho tình huống này không nhất thiết phải dùng name này đâu.
Thân.
 
Upvote 0
Bước đầu dễ nhận thấy sau "refersto:=" sẽ là chuỗi, Ta có thể:

gpe cho em hỏi có cách nào rút gọn code sau:
PHP:
sub taoname()motxmot
dim the as string :                     S02.activate
1 Dim RC11 as string , RC22 as string   '<=|  GPE.COM'
2 rc11="=if(rc[11]="""","""",if("   :           Rc22= "=if(rc[22]="""","""",if("    '<=|'

the = range("the").value
range("l3").select
if the = "ttmotxmot" then '  motxmot ->motxchin'
3    activeworkbook.names.add _
        name:="chuaxhtt", refersto:= RC11 & "countif(table2so,rc[11])+ _
countif(table2so,rc[12])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[12])>=1,"""",rc[11]))"
4    activeworkbook.names.add _
        name:="xhindaytt", refersto:=RC22  & "countif(data2so,rc[22])+countif(data2so,rc[23])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[23])>=1,rc[22],""""))"
elseif the = "ttmotxhai" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[13])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[13])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[24])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[24])>=1,rc[22],""""))"
elseif the = "ttmotxba" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[14])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[14])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[25])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[25])>=1,rc[22],""""))"
elseif the = "ttmotxbon" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[15])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[15])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[26])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[26])>=1,rc[22],""""))"
elseif the = "ttmotxnam" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[16])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[16])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[27])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[27])>=1,rc[22],""""))"
elseif the = "ttmotxsau" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[17])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[17])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[28])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[28])>=1,rc[22],""""))"
elseif the = "ttmotxbay" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[18])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[18])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[29])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[29])>=1,rc[22],""""))"
elseif the = "ttmotxtam" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[19])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[19])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[30])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[30])>=1,rc[22],""""))"
elseif the = "ttmotxchin" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[20])+countif(table2so,r[1]c[11])+countif(table2so,r[1]c[20])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[31])+countif(data2so,r[1]c[22])+countif(data2so,r[1]c[31])>=1,rc[22],""""))"
 
elseif the = "tthaixmot" then 'haixmot
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[12])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[12])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[23])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[23])>=1,rc[22],""""))"
elseif the = "tthaixhai" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[13])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[13])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[24])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[24])>=1,rc[22],""""))"
elseif the = "tthaixba" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[14])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[14])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[25])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[25])>=1,rc[22],""""))"
elseif the = "tthaixbon" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[15])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[15])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[26])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[26])>=1,rc[22],""""))"
elseif the = "tthaixnam" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[16])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[16])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[27])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[27])>=1,rc[22],""""))"
elseif the = "tthaixsau" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[17])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[17])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[28])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[28])>=1,rc[22],""""))"
elseif the = "tthaixbay" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[18])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[18])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[29])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[29])>=1,rc[22],""""))"
elseif the = "tthaixtam" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[19])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[19])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[30])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[30])>=1,rc[22],""""))"
elseif the = "tthaixchin" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[20])+countif(table2so,r[2]c[11])+countif(table2so,r[2]c[20])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[31])+countif(data2so,r[2]c[22])+countif(data2so,r[2]c[31])>=1,rc[22],""""))"
 
elseif the = "ttbaxmot" then 'baxmot
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[12])+countif(table2so,r[3]c[11])+countif(table2so,r[3]c[12])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[23])+countif(data2so,r[3]c[22])+countif(data2so,r[3]c[23])>=1,rc[22],""""))"
elseif the = "ttbaxhai" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[13])+countif(table2so,r[3]c[11])+countif(table2so,r[3]c[13])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[24])+countif(data2so,r[3]c[22])+countif(data2so,r[3]c[24])>=1,rc[22],""""))"
elseif the = "ttbaxba" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[14])+countif(table2so,r[3]c[11])+countif(table2so,r[3]c[14])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[25])+countif(data2so,r[3]c[22])+countif(data2so,r[3]c[25])>=1,rc[22],""""))"
elseif the = "ttbaxbon" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[15])+countif(table2so,r[3]c[11])+countif(table2so,r[3]c[15])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[26])+countif(data2so,r[3]c[22])+countif(data2so,r[3]c[26])>=1,rc[22],""""))"
elseif the = "ttbaxnam" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[16])+countif(table2so,r[3]c[11])+countif(table2so,r[3]c[16])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[27])+countif(data2so,r[3]c[22])+countif(data2so,r[3]c[27])>=1,rc[22],""""))"
elseif the = "ttbaxsau" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[17])+countif(table2so,r[3]c[11])+countif(table2so,r[3]c[17])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[28])+countif(data2so,r[3]c[22])+countif(data2so,r[3]c[28])>=1,rc[22],""""))"
elseif the = "ttbaxbay" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[18])+countif(table2so,r[3]c[11])+countif(table2so,r[3]c[18])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[29])+countif(data2so,r[3]c[22])+countif(data2so,r[3]c[29])>=1,rc[22],""""))"
elseif the = "ttbaxtam" then
    activeworkbook.names.add _
        name:="chuaxhtt", refersto:="=if(rc[11]="""","""",if(countif(table2so,rc[11])+countif(table2so,rc[19])+countif(table2so,r[3]c[11])+countif(table2so,r[3]c[19])>=1,"""",rc[11]))"
    activeworkbook.names.add _
        name:="xhindaytt", refersto:="=if(rc[22]="""","""",if(countif(data2so,rc[22])+countif(data2so,rc[30])+countif(data2so,r[3]c[22])+countif(data2so,r[3]c[30])>=1,rc[22],""""))"
elseif the = "ttbaxchin" then
.........
End if
end sub


:-= Bạn xem các dòng lệnh có đánh số í! :-=
 
Upvote 0
Bạn có thể rút gọn lại như thế này.
PHP:
Private Function ChuSo(Chu As String) As Byte
Chu = LCase(Chu)
ChuSo = -((Chu = "mot") + (Chu = "hai") * 2 + (Chu = "ba") * 3 + (Chu = "bon") * 4 + (Chu = "nam") * 5 + (Chu = "sau") * 6 + (Chu = "bay") * 7 + (Chu = "tam") * 8 + (Chu = "chin") * 9)
End Function
PHP:
Sub TaoName()
Dim The As String
The = Range("The").Value
ActiveWorkbook.Names.Add Name:="ChuaXhTT", RefersTo:="=IF(RC[11]="""","""",IF(COUNTIF(Table2So,RC[11])+COUNTIF(Table2So,RC[" & 11 + ChuSo(Mid(The, InStr(The, "x") + 1, 5)) & "])+COUNTIF(Table2So,R[" & ChuSo(Mid(The, 3, InStr(The, "x") - 3)) & "]C[11])+COUNTIF(Table2So,R[" & ChuSo(Mid(The, 3, InStr(The, "x") - 3)) & "]C[" & 11 + ChuSo(Mid(The, InStr(The, "x") + 1, 5)) & "])>=1,"""",RC[11]))"
ActiveWorkbook.Names.Add Name:="XhInDayTT", RefersTo:="=IF(RC[22]="""","""",IF(COUNTIF(Data2So,RC[22])+COUNTIF(Data2So,RC[" & 22 + ChuSo(Mid(The, 3, InStr(The, "x") - 3)) & "])+COUNTIF(Data2So,R[" & ChuSo(Mid(The, InStr(The, "x") + 1, 5)) & "]C[22])+COUNTIF(Data2So,R[" & ChuSo(Mid(The, InStr(The, "x") + 1, 5)) & "]C[" & 22 + ChuSo(Mid(The, 3, InStr(The, "x") - 3)) & "])>=1,RC[22],""""))"
End Sub
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom