Nhờ các anh chị sửa giúp code VBA bị lỗi else without if

Liên hệ QC

vantuqvna

Thành viên mới
Tham gia
2/9/13
Bài viết
6
Được thích
0
Mã:
Sub q1()
    Sheets("DANHMUC").Select
    n = Range("B5:G504").Value
    Sheets("NHAP").Select
    For i = 3 To 72
        If Application.WorksheetFunction.VLookup(Cells(i, 2), n, 3, 0) = False Then Cells(i, 3) = 0
        Else
            Cells(i, 3) = Application.WorksheetFunction.VLookup(Cells(i, 2), n, 3, 0)
        End If
        If Application.WorksheetFunction.VLookup(Cells(i, 2), n, 5, 0) = False Then Cells(i, 6) = 0
            Else
            Cells(i, 6) = Application.WorksheetFunction.VLookup(Cells(i, 2), n, 5, 0)
        End If
        If Application.WorksheetFunction.VLookup(Cells(i, 2), n, 6, 0) = False Then Cells(i, 7) = 0
            Else
            Cells(i, 7) = Application.WorksheetFunction.VLookup(Cells(i, 2), n, 6, 0)
        End If
    Next
End Sub

chào các anh chị trên GPE,
mình bị lỗi else without if,
Nhờ sửa giùm với ạ.
 
Lần chỉnh sửa cuối:
Mã:
Sub q1()
    Sheets("DANHMUC").Select
    n = Range("B5:G504").Value
    Sheets("NHAP").Select
    For i = 3 To 72
        If Application.WorksheetFunction.VLookup(Cells(i, 2), n, 3, 0) = False Then Cells(i, 3) = 0
        Else
            Cells(i, 3) = Application.WorksheetFunction.VLookup(Cells(i, 2), n, 3, 0)
        End If
        If Application.WorksheetFunction.VLookup(Cells(i, 2), n, 5, 0) = False Then Cells(i, 6) = 0
            Else
            Cells(i, 6) = Application.WorksheetFunction.VLookup(Cells(i, 2), n, 5, 0)
        End If
        If Application.WorksheetFunction.VLookup(Cells(i, 2), n, 6, 0) = False Then Cells(i, 7) = 0
            Else
            Cells(i, 7) = Application.WorksheetFunction.VLookup(Cells(i, 2), n, 6, 0)
        End If
    Next
End Sub

chào các anh chị trên GPE,
mình bị lỗi else without if,
Nhờ sửa giùm với ạ.
Thấy bạn này chịu khó học ghê, 1-2 giờ sáng vẫn vào đọc...
------------
Khi If dieu_kien Then ket_qua viết trên cùng một dòng thì không cần End If, tức là đã kết thúc cấu trúc If Then rồi.

Vậy:
If Application.WorksheetFunction.VLookup(Cells(i, 2), n, 3, 0) = False Then Cells(i, 3) = 0
đã kết thúc cấu trúc If - Then rồi;
Dòng kế tiếp mà viết Else thì phía trước không có cái If nào được bắt đầu cả.
==> Báo lỗi else without if.

Sửa với bài trên: Ngắt dòng lệnh ngay sau Then, tức là:
PHP:
If Application.WorksheetFunction.VLookup(Cells(i, 2), n, 3, 0) = False Then
         Cells(i, 3) = 0
Else
         Cells(i, 3) = Application.WorksheetFunction.VLookup(Cells(i, 2), n, 3, 0)
End If
------------
Khi cần lấy giá trị từ bảng tính thì ta viết chính tắc, chỉ đích danh địa chỉ cần lấy từ workbook/worksheet/range.
Không dùng phương thức Sheet.Select làm code chậm, màn hình nháy nháy... trừ khi cần điều khiển sheet với mục đích khác(...).
Chỉ chỉnh lại code trên về cách viết:
PHP:
Sub q1()
    Dim n
    n = Sheets("DANHMUC").Range("B5:G504").Value
    With Sheets("NHAP")
    For i = 3 To 72
        If Application.WorksheetFunction.VLookup(.Cells(i, 2), n, 3, 0) = False Then
            .Cells(i, 3) = 0
        Else
            .Cells(i, 3) = Application.WorksheetFunction.VLookup(.Cells(i, 2), n, 3, 0)
        End If
        If Application.WorksheetFunction.VLookup(.Cells(i, 2), n, 5, 0) = False Then
            .Cells(i, 6) = 0
        Else
            .Cells(i, 6) = Application.WorksheetFunction.VLookup(.Cells(i, 2), n, 5, 0)
        End If
        If Application.WorksheetFunction.VLookup(.Cells(i, 2), n, 6, 0) = False Then
            .Cells(i, 7) = 0
        Else
            .Cells(i, 7) = Application.WorksheetFunction.VLookup(.Cells(i, 2), n, 6, 0)
        End If
    Next i
    End With
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
mình làm được rồi, Thank bạn befaint nhiều nhá.
mình mới bắt đầu tìm hiểu nên nhiều cái không biết lắm, có gì mọi người giúp đỡ e với...
 
Upvote 0
Web KT
Back
Top Bottom