Xin mọi người giúp rút gọn CODE !!!!!!

Liên hệ QC

hoduytrieuvy

Thành viên mới
Tham gia
2/7/14
Bài viết
18
Được thích
1
Hiện tại mình có 1 code này, do mình không rành về VBA lắm. Nên đã sử dụng MACROS.
Giờ thấy nó dài quá nên cần mọi người rút gọn CODE lại.
Cám Ơn mọi người.


Sub LECHKHO_01()
'
' LECHKHO_01 Macro
Range("AU7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-44],'[1.xls]01'!R7C3:R900C21,19,0)"
Range("AU7").Select
Selection.AutoFill Destination:=Range("AU7:AU841")
Range("AU7:AU841").Select
Range("AW7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-46],'[1.xls]01'!R7C3:R900C22,20,0)"
Range("AW7").Select
Selection.AutoFill Destination:=Range("AW7:AW841")
Range("AW7:AW841").Select
Range("AY7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-48],'[1.xls]01'!R7C3:R900C23,21,0)"
Range("AY7").Select
Selection.AutoFill Destination:=Range("AY7:AY841")
Range("AY7:AY841").Select
Range("BA7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-50],'[1.xls]01'!R7C3:R900C24,22,0)"
Range("BA7").Select
Selection.AutoFill Destination:=Range("BA7:BA841")
Range("BA7:BA841").Select
Range("BC7").Select
Selection.ClearContents
Range("BC7").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-52],'[1.xls]01'!R7C3:R900C25,23,0)"
Range("BC7").Select
Selection.AutoFill Destination:=Range("BC7:BC841")
Range("BC7:BC841").Select
Range("BE7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-54],'[1.xls]01'!R7C3:R900C26,24,0)"
Range("BE7").Select
Selection.AutoFill Destination:=Range("BE7:BE841")
Range("BE7:BE841").Select
Range("BG7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-56],'[1.xls]01'!R7C3:R900C27,25,0)"
Range("BG7").Select
Selection.AutoFill Destination:=Range("BG7:BG841")
Range("BG7:BG841").Select
Range("BI7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-58],'[1.xls]01'!R7C3:R900C28,26,0)"
Range("BI7").Select
Selection.AutoFill Destination:=Range("BI7:BI841")
Range("BI7:BI841").Select
Range("BK7").Select
Selection.ClearContents
Range("BK7").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-60],'[1.xls]01'!R7C3:R900C29,27,0)"
Range("BK7").Select
Selection.AutoFill Destination:=Range("BK7:BK841")
Range("BK7:BK841").Select
Range("BM7").Select
Selection.ClearContents
Range("BM7").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-62],'[1.xls]01'!R7C3:R900C30,28,0)"
Range("BM7").Select
Selection.AutoFill Destination:=Range("BM7:BM841")
Range("BM7:BM841").Select
Range("BO7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-64],'[1.xls]01'!R7C3:R900C31,29,0)"
Range("BO7").Select
Selection.AutoFill Destination:=Range("BO7:BO841")
Range("BO7:BO841").Select
Range("BQ7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-66],'[1.xls]01'!R7C3:R900C32,30,0)"
Range("BQ7").Select
Selection.AutoFill Destination:=Range("BQ7:BQ841")
Range("BQ7:BQ841").Select
Range("BS7").Select
Selection.ClearContents
Range("BS7").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-68],'[1.xls]01'!R7C3:R900C33,31,0)"
Range("BS7").Select
Selection.AutoFill Destination:=Range("BS7:BS841")
Range("BS7:BS841").Select
Range("BU7").Select
Selection.ClearContents
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-70],'[1.xls]01'!R7C3:R900C34,32,0)"
Range("BU7").Select
Selection.AutoFill Destination:=Range("BU7:BU841")
Range("BU7:BU841").Select
Range("BW7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-72],'[1.xls]01'!R7C3:R900C35,33,0)"
Range("BW7").Select
Selection.AutoFill Destination:=Range("BW7:BW841")
Range("BW7:BW841").Select
Range("BY7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-74],'[1.xls]01'!R7C3:R900C36,34,0)"
Range("BY7").Select
Selection.AutoFill Destination:=Range("BY7:BY841")
Range("BY7:BY841").Select
Range("CA7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-76],'[1.xls]01'!R7C3:R900C38,36,0)"
Range("CA7").Select
Selection.AutoFill Destination:=Range("CA7:CA841")
Range("CA7:CA841").Select
Range("CA7").Select
End Sub
 
Hiện tại mình có 1 code này, do mình không rành về VBA lắm. Nên đã sử dụng MACROS.
Giờ thấy nó dài quá nên cần mọi người rút gọn CODE lại.
Cám Ơn mọi người.


Sub LECHKHO_01()
'
' LECHKHO_01 Macro
Range("AU7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-44],'[1.xls]01'!R7C3:R900C21,19,0)"
Range("AU7").Select
Selection.AutoFill Destination:=Range("AU7:AU841")
Range("AU7:AU841").Select
Range("AW7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-46],'[1.xls]01'!R7C3:R900C22,20,0)"
Range("AW7").Select
Selection.AutoFill Destination:=Range("AW7:AW841")
Range("AW7:AW841").Select
Range("AY7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-48],'[1.xls]01'!R7C3:R900C23,21,0)"
Range("AY7").Select
Selection.AutoFill Destination:=Range("AY7:AY841")
Range("AY7:AY841").Select
Range("BA7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-50],'[1.xls]01'!R7C3:R900C24,22,0)"
Range("BA7").Select
Selection.AutoFill Destination:=Range("BA7:BA841")
Range("BA7:BA841").Select
Range("BC7").Select
Selection.ClearContents
Range("BC7").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-52],'[1.xls]01'!R7C3:R900C25,23,0)"
Range("BC7").Select
Selection.AutoFill Destination:=Range("BC7:BC841")
Range("BC7:BC841").Select
Range("BE7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-54],'[1.xls]01'!R7C3:R900C26,24,0)"
Range("BE7").Select
Selection.AutoFill Destination:=Range("BE7:BE841")
Range("BE7:BE841").Select
Range("BG7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-56],'[1.xls]01'!R7C3:R900C27,25,0)"
Range("BG7").Select
Selection.AutoFill Destination:=Range("BG7:BG841")
Range("BG7:BG841").Select
Range("BI7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-58],'[1.xls]01'!R7C3:R900C28,26,0)"
Range("BI7").Select
Selection.AutoFill Destination:=Range("BI7:BI841")
Range("BI7:BI841").Select
Range("BK7").Select
Selection.ClearContents
Range("BK7").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-60],'[1.xls]01'!R7C3:R900C29,27,0)"
Range("BK7").Select
Selection.AutoFill Destination:=Range("BK7:BK841")
Range("BK7:BK841").Select
Range("BM7").Select
Selection.ClearContents
Range("BM7").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-62],'[1.xls]01'!R7C3:R900C30,28,0)"
Range("BM7").Select
Selection.AutoFill Destination:=Range("BM7:BM841")
Range("BM7:BM841").Select
Range("BO7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-64],'[1.xls]01'!R7C3:R900C31,29,0)"
Range("BO7").Select
Selection.AutoFill Destination:=Range("BO7:BO841")
Range("BO7:BO841").Select
Range("BQ7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-66],'[1.xls]01'!R7C3:R900C32,30,0)"
Range("BQ7").Select
Selection.AutoFill Destination:=Range("BQ7:BQ841")
Range("BQ7:BQ841").Select
Range("BS7").Select
Selection.ClearContents
Range("BS7").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-68],'[1.xls]01'!R7C3:R900C33,31,0)"
Range("BS7").Select
Selection.AutoFill Destination:=Range("BS7:BS841")
Range("BS7:BS841").Select
Range("BU7").Select
Selection.ClearContents
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-70],'[1.xls]01'!R7C3:R900C34,32,0)"
Range("BU7").Select
Selection.AutoFill Destination:=Range("BU7:BU841")
Range("BU7:BU841").Select
Range("BW7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-72],'[1.xls]01'!R7C3:R900C35,33,0)"
Range("BW7").Select
Selection.AutoFill Destination:=Range("BW7:BW841")
Range("BW7:BW841").Select
Range("BY7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-74],'[1.xls]01'!R7C3:R900C36,34,0)"
Range("BY7").Select
Selection.AutoFill Destination:=Range("BY7:BY841")
Range("BY7:BY841").Select
Range("CA7").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-76],'[1.xls]01'!R7C3:R900C38,36,0)"
Range("CA7").Select
Selection.AutoFill Destination:=Range("CA7:CA841")
Range("CA7:CA841").Select
Range("CA7").Select
End Sub
Tốt nhất bạn cho file lên và đưa ra kết quả cần đạt được viết code còn nhanh hơn.:D.
 
Upvote 0
Bạn thử code sau. Tôi khuyên bạn đã dùng code thì không cần dùng VLockup ghi vào bảng tính nữa
----------------------------------------------------
PHP:
Sub test()
  Dim i&, j&, k&, m&: k = 19: m = 44
  Dim AUCol&, CACol&: AUCol = Range("AU1").Column: CACol = Range("CA1").Column - 1
  For j = AUCol  To CACol
    For i = 7 To 841
      If j = CACol Then k = k + 1
      Cells(i, AUCol + k - 19).FormulaR1C1 = _
      "=VLOOKUP(RC[-" & m & "],'[1.xls]01'!R7C3:R900C" & k + 2 & "," & k & ",0)"
  Next : k = k + 1: m = m + 2: Next j
End Sub
 
Upvote 0
Tốt nhất bạn cho file lên và đưa ra kết quả cần đạt được viết code còn nhanh hơn.:D.
FILE của công ty, e ko dám đưa lên bạn ơi. hix
Bài đã được tự động gộp:

Bạn thử code sau. Tôi khuyên bạn đã dùng code thì không cần dùng VLockup ghi vào bảng tính nữa
----------------------------------------------------
PHP:
Sub test()
  Dim i&, j&, k&, m&: k = 19: m = 44
  Dim AUCol&, CACol&: AUCol = Range("AU1").Column: CACol = Range("CA1").Column - 1
  For j = AUCol  To CACol
    For i = 7 To 841
      If j = CACol Then k = k + 1
      Cells(i, AUCol + k - 19).FormulaR1C1 = _
      "=VLOOKUP(RC[-" & m & "],'[1.xls]01'!R7C3:R900C" & k + 2 & "," & k & ",0)"
  Next : k = k + 1: m = m + 2: Next j
End Sub
không chạy được bạn ơi, đứng hình luôn.
 
Lần chỉnh sửa cuối:
Upvote 0
Nhìn kĩ lại code của bạn. Tôi thấy các cột cách nhau một đơn vị. Cột cuối đột biến tăng 1 đơn vị
Bạn thử lại
PHP:
Sub test()
  Dim i&, j&, k&, m&: k = 19: m = 44
  Dim AUCol&, CACol&: AUCol = Range("AU1").Column: CACol = Range("CA1").Column
  For j = AUCol To CACol Step 2
    For i = 7 To 841
      If j = CACol Then k = k + 1
      If i = 7 Then
          Cells(i, AUCol + m - 44).Formula = _
                    "=VLOOKUP(RC[-" & m & "],'[1.xls]01'!R7C3:R900C" & k + 2 & "," & k & ",0)"
      Else
          Cells(i, AUCol + m - 44).Formula = Cells(7, AUCol + m - 44).FormulaR1C1
      End If
  Next i: k = k + 1: m = m + 2: Next j
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Nhìn kĩ lại code của bạn. Tôi thấy các cột cách nhau một đơn vị. Cột cuối đột biến tăng 1 đơn vị
Bạn thử lại
PHP:
Sub test()
  Dim i&, j&, k&, m&: k = 19: m = 44
  Dim AUCol&, CACol&: AUCol = Range("AU1").Column: CACol = Range("CA1").Column
  For j = AUCol To CACol Step 2
    For i = 7 To 841
      If j = CACol Then k = k + 1
      If i = 7 Then
          Cells(i, AUCol + k - 19).Formula = _
                    "=VLOOKUP(RC[-" & m & "],'[1.xls]01'!R7C3:R900C" & k + 2 & "," & k & ",0)"
      Else
          Cells(i, AUCol + k - 19).Formula = Cells(7, AUCol + k - 19).FormulaR1C1
      End If
  Next i: k = k + 1: m = m + 2: Next j
End Sub
Đúng rồi bạn. Do cột đó không cần dò lệch. Nên mình bỏ qua. bạn cứ việc chạy cách nhau 1 đơn vị cũng dc, mình sẽ thêm cột đó vào nữa.
 
Upvote 0
Hên xui:
PHP:
Sub RutGonLechKho()
 Const GPE As Integer = 841
 Dim Cot As Byte, Tmp As Integer, Col As Integer
 
 For Cot = 47 To 80 Step 2
    With Cells(7, Cot)
        .ClearContents
        Tmp = (Cot + 1) \ 2 - 5:                    Col = Cot - 3
        .FormulaR1C1 = "=VLOOKUP(RC[-" & Col & "],'[1.xls]01'!R7C3:R900C21," & Tmp & ",0)"            '19'
        .Select
        Selection.AutoFill Destination:=Cells(7, Cot).Resize(GPE)
    End With
 Next Cot
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Hên xui:
PHP:
Sub RutGonLechKho()
Const GPE As Integer = 841
Dim Cot As Byte, Tmp As Integer, Col As Integer

For Cot = 47 To 80 Step 2
    With Cells(7, Cot)
        .ClearContents
        Tmp = (Cot + 1) \ 2 - 5:                    Col = Cot - 3
        .FormulaR1C1 = "=VLOOKUP(RC[-" & Col & "],'[1.xls]01'!R7C3:R900C21," & Tmp & ",0)"            '19'
        .Select
        Selection.AutoFill Destination:=Cells(7, Cot).Resize(GPE)
    End With
Next Cot
End Sub
cột đầu tiên chạy đúng. VLOOKUP(C7,'[1.xls]01'!$C$7:$U$900,19,0)
cột thứ 2 sai: VLOOKUP(C7,'[1.xls]01'!$C$7:$U$900,20,0) ( sai chỗ chữ U )
bác tăng lên 1 cột dùm e.
nếu đúng là cột 2 như thế này: VLOOKUP(C7,'[1.xls]01'!$C$7:$V$900,20,0)
 
Upvote 0
cột đầu tiên chạy đúng. VLOOKUP(C7,'[1.xls]01'!$C$7:$U$900,19,0)
cột thứ 2 sai: VLOOKUP(C7,'[1.xls]01'!$C$7:$U$900,20,0) ( sai chỗ chữ U )
bác tăng lên 1 cột dùm e.
nếu đúng là cột 2 như thế này: VLOOKUP(C7,'[1.xls]01'!$C$7:$V$900,20,0)
Bạn xem code nhé.
Mã:
Sub lechhang()
  Application.ScreenUpdating = False
   Dim arr, arr1, lr As Long, i As Long, j As Integer, dic As Object, ten As String, wb As Workbook, b As Long, lr1 As Long
   Set dic = CreateObject("scripting.dictionary")
   ten = ThisWorkbook.Path & "\test2.xlsx"
   With Sheets("sheet1")
   lr = .Range("A" & Rows.Count).End(xlUp).Row
      If lr < 2 Then Exit Sub
   arr = .Range("A2:S" & lr).Value
   .Range("j2:s" & lr).ClearContents
   For i = 1 To UBound(arr, 1)
      If Not dic.exists(arr(i, 1)) Then
         dic.Add arr(i, 1), i
      End If
   Next i
   End With
   Set wb = Workbooks.Open(ten)
   lr1 = wb.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
   If lr1 < 2 Then Exit Sub: wb.Close False
      arr1 = wb.Sheets(1).Range("A2:I" & lr1).Value
      wb.Close False
      For i = 1 To UBound(arr1, 1)
          b = dic.Item(arr1(i, 1))
          If b Then
             arr(b, 10) = arr1(i, 5)
             arr(b, 11) = arr(b, 5) - arr(b, 10)
             arr(b, 12) = arr1(i, 6)
             arr(b, 13) = arr(b, 6) - arr(b, 12)
             arr(b, 14) = arr1(i, 7)
             arr(b, 15) = arr(b, 7) - arr(b, 14)
             arr(b, 16) = arr1(i, 8)
             arr(b, 17) = arr(b, 8) - arr(b, 16)
             arr(b, 18) = arr1(i, 9)
             arr(b, 19) = arr(b, 9) - arr(b, 18)
         End If
     Next i
   With Sheets("sheet1")
          .Range("A2:S" & lr).Value = arr
   End With
   Application.ScreenUpdating = True
End Sub
 

File đính kèm

Upvote 0
Tạo công thức tất cả các cột
Mã:
Sub AddVlookup()
Dim sRow As Long, eCol As Long, j As Long
eCol = Range("AAA1").End(xlToLeft).Column
sRow = Range("A1000000").End(xlUp).Row - 1
For j = 10 To eCol Step 2
  Cells(2, j).Resize(sRow).FormulaR1C1 = "=VLOOKUP(RC2,[test2.xlsx]Sheet1!R2C2:R6C9," & j / 2 - 1 & ",0)"
  Cells(2, j + 1).Resize(sRow).FormulaR1C1 = "=RC[-1]-RC[" & -1 - j / 2 & "]"
Next j
End Sub
 
Upvote 0
Tạo công thức tất cả các cột
Mã:
Sub AddVlookup()
Dim sRow As Long, eCol As Long, j As Long
eCol = Range("AAA1").End(xlToLeft).Column
sRow = Range("A1000000").End(xlUp).Row - 1
For j = 10 To eCol Step 2
  Cells(2, j).Resize(sRow).FormulaR1C1 = "=VLOOKUP(RC2,[test2.xlsx]Sheet1!R2C2:R6C9," & j / 2 - 1 & ",0)"
  Cells(2, j + 1).Resize(sRow).FormulaR1C1 = "=RC[-1]-RC[" & -1 - j / 2 & "]"
Next j
End Sub
check inbox giúp e ạ.
 
Upvote 0
cột đầu tiên chạy đúng. VLOOKUP(C7,'[1.xls]01'!$C$7:$U$900,19,0)
cột thứ 2 sai: VLOOKUP(C7,'[1.xls]01'!$C$7:$U$900,20,0) ( sai chỗ chữ U )
bác tăng lên 1 cột dùm e.
nếu đúng là cột 2 như thế này: VLOOKUP(C7,'[1.xls]01'!$C$7:$V$900,20,0)
PHP:
Sub RutGonLechKho()
 Const GPE As Integer = 841
 Dim Cot As Byte, Tmp As Integer, Col As Integer
 
 For Cot = 47 To 80 Step 2
    With Cells(7, Cot)
        .ClearContents
        Tmp = (Cot + 1) \ 2 - 5:                    Col = Cot - 3
        .FormulaR1C1 = "=VLOOKUP(RC[-" & Col & "],'[1.xls]01'!R7C3:R900C" & Tmp + 2 & "," & Tmp & ",0)"            '19=> 21'
        .Select
        Selection.AutoFill Destination:=Cells(7, Cot).Resize(GPE)
    End With
 Next Cot
End Sub
 
Upvote 0
PHP:
Sub RutGonLechKho()
Const GPE As Integer = 841
Dim Cot As Byte, Tmp As Integer, Col As Integer

For Cot = 47 To 80 Step 2
    With Cells(7, Cot)
        .ClearContents
        Tmp = (Cot + 1) \ 2 - 5:                    Col = Cot - 3
        .FormulaR1C1 = "=VLOOKUP(RC[-" & Col & "],'[1.xls]01'!R7C3:R900C" & Tmp + 2 & "," & Tmp & ",0)"            '19=> 21'
        .Select
        Selection.AutoFill Destination:=Cells(7, Cot).Resize(GPE)
    End With
Next Cot
End Sub
Ngon bác ơi. chạy ok rồi.
 
Upvote 0
'[1.xls]01'!R7C3:R900C38
Cái này là một range cố định. Người ta đặt vào một name rồi cứ từ ấy mà dùng.
(tôi chỉ nói về quy luật Vlookup trong Excel)
 
Upvote 0
Web KT

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

Back
Top Bottom