Xin mọi người sửa giúp e lỗi code khi e dùng application.match với arr() (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

khanhhero

Thành viên hoạt động
Tham gia
28/7/11
Bài viết
144
Được thích
36
Như tiêu đề, e muốn lọc trong mảng những phần tử nào chưa xuất hiện trong Sheet Data để update phần tử đó qua sheet data, nhưng khi làm thì báo lỗi subscript range, em dùng vòng lặp cho cells thì công thức chạy, nhưng dùng cho arr() thì báo lỗi. Mọi người sửa giúp em với ạ. Cám ơn mọi người nhiều!

Sub update_key()
Dim array_source()

array_source = ThisWorkbook.Sheets("Detail").Range("A4:A1100").Value

For i = 1 To 1100
If IsError(Application.Match(array_source(i, 0), ThisWorkbook.Sheets("Data").Range(Range("a4"), Range("a4").End(xlDown).Value, 0))) Then
Sheet4.Range("A4").End(xlDown).Offset(1, 0).Value = array_source(i, 0)
End If
Next i
End Sub
 
Lần chỉnh sửa cuối:
Như tiêu đề, e muốn lọc trong mảng những phần tử nào chưa xuất hiện trong Sheet Data để update phần tử đó qua sheet data, nhưng khi làm thì báo lỗi subscript range, em dùng vòng lặp cho cells thì công thức chạy, nhưng dùng cho arr() thì báo lỗi. Mọi người sửa giúp em với ạ. Cám ơn mọi người nhiều!
bạn sửa lại thành vầy nha
Mã:
application.WorksheetFunction.Match(......)
 
Upvote 0
Cám ơn bạn, nhưng mà nó báo lỗi ở array_source(i, 0), chứ không phải lỗi ở application bạn ah, hic hic
Bạn sửa theo bài #2 và đổi
Mã:
[COLOR=#000000]array_source(i, 0)
[/COLOR]

Thành
Mã:
[COLOR=#000000]array_source(i, 1)
[/COLOR]

Xem sao. Nếu không được thì up file lên.
 
Upvote 0
Em tìm được lỗi rồi, array_source(i, 1) chứ ko phải array_source(i, 0) :D
 
Upvote 0
Như tiêu đề, e muốn lọc trong mảng những phần tử nào chưa xuất hiện trong Sheet Data để update phần tử đó qua sheet data, nhưng khi làm thì báo lỗi subscript range, em dùng vòng lặp cho cells thì công thức chạy, nhưng dùng cho arr() thì báo lỗi. Mọi người sửa giúp em với ạ. Cám ơn mọi người nhiều!
Mã:
Sub update_key()
  Dim array_source
  Dim last_row
  array_source = ThisWorkbook.Sheets("Detail").Range("A4:A1100").Value
  For[COLOR=#ff0000] i = 1 To 1100[/COLOR]
    If IsError(Application.Match(array_source(i, [COLOR=#ff0000]0[/COLOR]), [COLOR=#ff0000]ThisWorkbook.Sheets("Data").Range(Range("a4"), Range("a4").End(xlDown).Value[/COLOR], 0))) Then
      Sheet4.Range("A4").End(xlDown).Offset(1, 0).Value = array_source(i, [COLOR=#ff0000]0[/COLOR])
    End If
  Next i
End Sub
Sai quá trời luôn (những chỗ màu đỏ)
- Đầu tiên là:
Mã:
[COLOR=#ff0000]ThisWorkbook.Sheets("Data").Range(Range("a4"), Range("a4").End(xlDown).Value[/COLOR]
Viết thế này mới đúng:
Mã:
ThisWorkbook.Sheets("Data").Range([COLOR=#ff0000]"a4"[/COLOR], [COLOR=#ff0000]ThisWorkbook.Sheets("Data").[/COLOR]Range("a4").End(xlDown)[COLOR=#ff0000])[/COLOR].Value
Hoặc có thể đặt thêm 1 biến data cho gọn:
Mã:
Sub update_key()
  Dim [COLOR=#ff0000]data[/COLOR]
  ..................
  [COLOR=#ff0000]data = ThisWorkbook.Sheets("Data").Range("a4", ThisWorkbook.Sheets("Data").Range("a4").End(xlDown)).Value[/COLOR]
  ......................
    If IsError(Application.Match(array_source(i, 1), [COLOR=#ff0000]data[/COLOR], 0)) Then
     ........................
    End If
  Next i
End Sub
- Tiếp theo: For i = 1 To 1100 là sai. Con số 1100 sẽ dẫn đến lỗi Out of range (vì từ A4 đến A1100 chưa đủ 1100 dòng). Chính xác phải là:
Mã:
For i = LBound(array_source, 1) To UBound(array_source, 1)
- Thứ 3: Truy xuất mảng 2 chiều như vầy: array_source(i, 0) là sai. Số 0 sửa thành số 1 mới đúng
- Thứ 4: Sai mấy dấu ngoặc (chỗ thừa chỗ thiếu)
-------------------------
Sửa lại toàn bộ
Mã:
Sub update_key()
  Dim array_source, [COLOR=#ff0000]data[/COLOR]
  Dim last_row,[COLOR=#ff0000] i As Long[/COLOR]
  array_source = ThisWorkbook.Sheets("Detail").Range("A4:A1100").Value
  [COLOR=#ff0000]data = ThisWorkbook.Sheets("Data").Range("a4", ThisWorkbook.Sheets("Data").Range("a4").End(xlDown)).Value[/COLOR]
  [COLOR=#ff0000]For i = LBound(array_source, 1) To UBound(array_source, 1)[/COLOR]
    If IsError(Application.Match(array_source(i, 1), [COLOR=#ff0000]data[/COLOR], 0)) Then
      Sheet4.Range("A4").End(xlDown).Offset(1, 0).Value = array_source(i, [COLOR=#ff0000]1[/COLOR])
    End If
  Next i
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Mã:
Sub update_key()
  Dim array_source
  Dim last_row
  array_source = ThisWorkbook.Sheets("Detail").Range("A4:A1100").Value
  For[COLOR=#ff0000] i = 1 To 1100[/COLOR]
    If IsError(Application.Match(array_source(i, [COLOR=#ff0000]0[/COLOR]), [COLOR=#ff0000]ThisWorkbook.Sheets("Data").Range(Range("a4"), Range("a4").End(xlDown).Value[/COLOR], 0))) Then
      Sheet4.Range("A4").End(xlDown).Offset(1, 0).Value = array_source(i, [COLOR=#ff0000]0[/COLOR])
    End If
  Next i
End Sub
Sai quá trời luôn (những chỗ màu đỏ)
- Đầu tiên là:
Mã:
[COLOR=#ff0000]ThisWorkbook.Sheets("Data").Range(Range("a4"), Range("a4").End(xlDown).Value[/COLOR]
Viết thế này mới đúng:
Mã:
ThisWorkbook.Sheets("Data").Range([COLOR=#ff0000]"a4"[/COLOR], [COLOR=#ff0000]ThisWorkbook.Sheets("Data").[/COLOR]Range("a4").End(xlDown)[COLOR=#ff0000])[/COLOR].Value
Hoặc có thể đặt thêm 1 biến data cho gọn:
Mã:
Sub update_key()
  Dim [COLOR=#ff0000]data[/COLOR]
  ..................
  [COLOR=#ff0000]data = ThisWorkbook.Sheets("Data").Range("a4", ThisWorkbook.Sheets("Data").Range("a4").End(xlDown)).Value[/COLOR]
  ......................
    If IsError(Application.Match(array_source(i, 1), [COLOR=#ff0000]data[/COLOR], 0)) Then
     ........................
    End If
  Next i
End Sub
- Tiếp theo: For i = 1 To 1100 là sai. Con số 1100 sẽ dẫn đến lỗi Out of range (vì từ A4 đến A1100 chưa đủ 1100 dòng). Chính xác phải là:
Mã:
For i = LBound(array_source, 1) To UBound(array_source, 1)
- Thứ 3: Truy xuất mảng 2 chiều như vầy: array_source(i, 0) là sai. Số 0 sửa thành số 1 mới đúng
- Thứ 4: Sai mấy dấu ngoặc (chỗ thừa chỗ thiếu)
-------------------------
Sửa lại toàn bộ
Mã:
Sub update_key()
  Dim array_source, [COLOR=#ff0000]data[/COLOR]
  Dim last_row,[COLOR=#ff0000] i As Long[/COLOR]
  array_source = ThisWorkbook.Sheets("Detail").Range("A4:A1100").Value
  [COLOR=#ff0000]data = ThisWorkbook.Sheets("Data").Range("a4", ThisWorkbook.Sheets("Data").Range("a4").End(xlDown)).Value[/COLOR]
  [COLOR=#ff0000]For i = LBound(array_source, 1) To UBound(array_source, 1)[/COLOR]
    If IsError(Application.Match(array_source(i, 1), [COLOR=#ff0000]data[/COLOR], 0)) Then
      Sheet4.Range("A4").End(xlDown).Offset(1, 0).Value = array_source(i, [COLOR=#ff0000]1[/COLOR])
    End If
  Next i
End Sub

Em dân ngoại đạo, mày mò cóp chỗ này, chôm chỗ kia xíu nên nó lủng củng thế đấy bác ạ. Cám ơn bác đã sửa code giúp em ạ. :)
 
Upvote 0
Web KT

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

Back
Top Bottom