Nhờ sửa code VBA (1 người xem)

Liên hệ QC

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

heyhey1994

Thành viên chính thức
Tham gia
16/3/17
Bài viết
78
Được thích
17
Em viết đoạn code thế này mà chạy nó thông báo: next without for. Anh chị nào biết sửa giúp em với ạ

Public Sub chaymm()
Dim i, j, k As Integer
k = WorksheetFunction.counta(Range("A:A")) + 1
For i = 4 To k

If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value = Cells(i, 2).Value) And (Cells(i + 1, 11).Value > Cells(i, 11).Value) Then
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i + 1, 11).Value)

If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value = Cells(i, 2).Value) And (Cells(i + 1, 11).Value < Cells(i, 11).Value) Then
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)

If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value <> Cells(i, 2).Value) Then
j = j + 1
Sheets("Sheet1").Cells(j, 1).Value = (Cells(i, 1).Value)
Sheets("Sheet1").Cells(j, 2).Value = (Cells(i, 2).Value)
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)

If (Cells(i + 1, 1).Value <> Cells(i, 1).Value) And (Cells(i + 1, 2).Value <> Cells(i, 2).Value) Then
j = j + 1
Sheets("Sheet1").Cells(j, 1).Value = (Cells(i, 1).Value)
Sheets("Sheet1").Cells(j, 2).Value = (Cells(i, 2).Value)
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)
End If
Next i
End Sub
 
Em viết đoạn code thế này mà chạy nó thông báo: next without for. Anh chị nào biết sửa giúp em với ạ

Public Sub chaymm()
Dim i, j, k As Integer
k = WorksheetFunction.counta(Range("A:A")) + 1
For i = 4 To k

If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value = Cells(i, 2).Value) And (Cells(i + 1, 11).Value > Cells(i, 11).Value) Then
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i + 1, 11).Value)

If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value = Cells(i, 2).Value) And (Cells(i + 1, 11).Value < Cells(i, 11).Value) Then
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)

If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value <> Cells(i, 2).Value) Then
j = j + 1
Sheets("Sheet1").Cells(j, 1).Value = (Cells(i, 1).Value)
Sheets("Sheet1").Cells(j, 2).Value = (Cells(i, 2).Value)
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)

If (Cells(i + 1, 1).Value <> Cells(i, 1).Value) And (Cells(i + 1, 2).Value <> Cells(i, 2).Value) Then
j = j + 1
Sheets("Sheet1").Cells(j, 1).Value = (Cells(i, 1).Value)
Sheets("Sheet1").Cells(j, 2).Value = (Cells(i, 2).Value)
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)
End If
Next i
End Sub
code thiếu nhiều: End if
 
Upvote 0
Mã:
Public Sub chaymm()
Dim i, j, k As Integer
k = WorksheetFunction.counta(Range("A:A")) + 1
For i = 4 To k

If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value = Cells(i, 2).Value) And (Cells(i + 1, 11).Value > Cells(i, 11).Value) [B]Then
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i + 1, 11).Value)
[/B]
If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value = Cells(i, 2).Value) And (Cells(i + 1, 11).Value < Cells(i, 11).Value) [B]Then
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)
[/B]
If (Cells(i + 1, 1).Value = Cells(i, 1).Value) And (Cells(i + 1, 2).Value <> Cells(i, 2).Value) [B]Then
j = j + 1
Sheets("Sheet1").Cells(j, 1).Value = (Cells(i, 1).Value)
Sheets("Sheet1").Cells(j, 2).Value = (Cells(i, 2).Value)
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)[/B]

If (Cells(i + 1, 1).Value <> Cells(i, 1).Value) And (Cells(i + 1, 2).Value <> Cells(i, 2).Value) Then
j = j + 1
Sheets("Sheet1").Cells(j, 1).Value = (Cells(i, 1).Value)
Sheets("Sheet1").Cells(j, 2).Value = (Cells(i, 2).Value)
Sheets("Sheet1").Cells(j, 3).Value = (Cells(i, 11).Value)
End If
Next i
End Sub
Thiếu kết thúc các hàm IF
Bạn chọn 1 trong 2 cách :
- Bổ sung end if theo cú pháp của bạn
- Đưa "then" "sheet..." phần tô đậm về cùng 1 dòng
 
Upvote 0
code thiếu nhiều: End if
Thiếu kết thúc các hàm IF
Bạn chọn 1 trong 2 cách :
- Bổ sung end if theo cú pháp của bạn
- Đưa "then" "sheet..." phần tô đậm về cùng 1 dòng
dạ em cảm ơn ạ. Bây giờ em có 1 vấn đề muốn hỏi nữa là có 4 loại số Bx0y00, Bx00y00, Bx00y0, Bx0y0 như thế này thì viết code thế nào để nó tách thành 2 cột, 1 cột x..., 1 cột y... ví dụ B50600 thì thành 1 cột 50, 1 cột 600 còn b70020 thì thành 700,20. Em nghĩ mãi mà vẫn chưa có ý tưởng ạ.
 
Upvote 0
dạ em cảm ơn ạ. Bây giờ em có 1 vấn đề muốn hỏi nữa là có 4 loại số Bx0y00, Bx00y00, Bx00y0, Bx0y0 như thế này thì viết code thế nào để nó tách thành 2 cột, 1 cột x..., 1 cột y... ví dụ B50600 thì thành 1 cột 50, 1 cột 600 còn b70020 thì thành 700,20. Em nghĩ mãi mà vẫn chưa có ý tưởng ạ.
Điểm phân chia của chuỗi là vị trí số <>0 lùi 1 đơn vị đó bạn
 
Upvote 0

File đính kèm

Lần chỉnh sửa cuối:
Upvote 0
Web KT

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

Back
Top Bottom