NguyenthiH
Thành viên mới đăng ký
- Tham gia
- 11/12/16
- Bài viết
- 940
- Được thích
- 172
- Giới tính
- Nữ
mình làm thủ công như vậy*** chi em thấy từ mục 1->218 nó cứ cách mục 219-> 221 một khoảng trắng, mặc dù em đã canh trái.
Mục 1->218 là trích xuất từ phần mềm Foxpro ra và em đã chuyển mã, không biết tại sao lại có khoảng trắng,mà cột SL tồn không biết có phải là số không nữa. Mong các anh chị giúp loại bỏ khoảng trắng này.
Sub botrong()
Dim lr As Long
lr = Sheet1.Range("B6500").End(3).Row
'MsgBox lr
With Sheet1
For i = lr To 4 Step -1
.Range("B" & i) = Trim(.Range("B" & i))
'.Range("D" & i) = CDbl(Trim(.Range("D" & i)))
Next i
End With
End Sub
Private Sub Luu1_Click()
Dim i As Integer, irow As Long
If ngay1 = "" Or spn1 = "" Then MsgBox ("Ban chua nhap ngay hoac so phieu nhap"), vbExclamation, "GPE": Exit Sub
If ListBox1.ListCount = 0 Then MsgBox ("ban chua cap nhat Noi dung vao Listbox"), vbExclamation, "GPE": Exit Sub
Application.ScreenUpdating = False 'giup code chay. nhanh hon
With Sheet5
irow = .Range("E65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu
For i = 0 To ListBox1.ListCount - 1
.Cells(irow + i, 1) = irow + i - 3
.Cells(irow + i, 2) = CDate(ngay1.Value)
.Cells(irow + i, 2).NumberFormat = "m/d/yyyy"
.Cells(irow + i, 3) = UCase(spn1.Value)
.Cells(irow + i, 4) = ListBox1.List(i, 1) 'ten nhan cung cap
.Cells(irow + i, 5) = ListBox1.List(i, 2) 'ten hang hoa
.Cells(irow + i, 6) = ListBox1.List(i, 3) 'don vi tinh
.Cells(irow + i, 7) = ListBox1.List(i, 4) 'so luong
.Cells(irow + i, 8) = ListBox1.List(i, 5) 'ten theo KT
Next
.Cells(irow, 7).Resize(i).NumberFormat = "#,##0.00"
.Cells(irow, 1).Resize(i, 8).Borders.LineStyle = 1
.Cells(irow, 1).Resize(i, 8).Borders.ThemeColor = 5
End With
Sheet5.Range("B4:H4").Resize(i).Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
ngay1 = "": spn1 = "": cb_NCC1 = "": cb_thh1 = "": dvt1 = "": sln1 = ""
ListBox1.Clear
Application.ScreenUpdating = True
ngay1.SetFocus
End Sub
Sheet5.Range("B4:H4").Resize(i).Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
Trong cả dòng, chỉ có i là một biến. Đặt code đưa nó ra xem là cái gì....chổ này sai chổ nào ạ:
Mã:Sheet5.Range("B4:H4").Resize(i).Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
Sai chổ nầyNhờ các anh chị giúp em xem code này sao nó không chạy sort ạ:
chổ này sai chổ nào ạ:Mã:Private Sub Luu1_Click() Dim i As Integer, irow As Long If ngay1 = "" Or spn1 = "" Then MsgBox ("Ban chua nhap ngay hoac so phieu nhap"), vbExclamation, "GPE": Exit Sub If ListBox1.ListCount = 0 Then MsgBox ("ban chua cap nhat Noi dung vao Listbox"), vbExclamation, "GPE": Exit Sub Application.ScreenUpdating = False 'giup code chay. nhanh hon With Sheet5 irow = .Range("E65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu For i = 0 To ListBox1.ListCount - 1 .Cells(irow + i, 1) = irow + i - 3 .Cells(irow + i, 2) = CDate(ngay1.Value) .Cells(irow + i, 2).NumberFormat = "m/d/yyyy" .Cells(irow + i, 3) = UCase(spn1.Value) .Cells(irow + i, 4) = ListBox1.List(i, 1) 'ten nhan cung cap .Cells(irow + i, 5) = ListBox1.List(i, 2) 'ten hang hoa .Cells(irow + i, 6) = ListBox1.List(i, 3) 'don vi tinh .Cells(irow + i, 7) = ListBox1.List(i, 4) 'so luong .Cells(irow + i, 8) = ListBox1.List(i, 5) 'ten theo KT Next .Cells(irow, 7).Resize(i).NumberFormat = "#,##0.00" .Cells(irow, 1).Resize(i, 8).Borders.LineStyle = 1 .Cells(irow, 1).Resize(i, 8).Borders.ThemeColor = 5 End With Sheet5.Range("B4:H4").Resize(i).Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom ngay1 = "": spn1 = "": cb_NCC1 = "": cb_thh1 = "": dvt1 = "": sln1 = "" ListBox1.Clear Application.ScreenUpdating = True ngay1.SetFocus End Sub
Mã:Sheet5.Range("B4:H4").Resize(i).Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
Theo VBA thì khi ra khỏi vòng lặp sẽ có i = ListBox1.ListCount.Trong cả dòng, chỉ có i là một biến. Đặt code đưa nó ra xem là cái gì.
MsgBox i
Sheet5.Range("B4:H4").Resize(i).Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
Private Sub Luu1_Click()
Dim i As Integer, irow As Long
If ngay1 = "" Or spn1 = "" Then MsgBox ("Ban chua nhap ngay hoac so phieu nhap"), vbExclamation, "GPE": Exit Sub
If ListBox1.ListCount = 0 Then MsgBox ("ban chua cap nhat Noi dung vao Listbox"), vbExclamation, "GPE": Exit Sub
Application.ScreenUpdating = False 'giup code chay. nhanh hon
With Sheet5
irow = .Range("E65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu
For i = 0 To ListBox1.ListCount - 1
.Cells(irow + i, 1) = irow + i - 3
.Cells(irow + i, 2) = CDate(ngay1.Value)
.Cells(irow + i, 2).NumberFormat = "m/d/yyyy"
.Cells(irow + i, 3) = UCase(spn1.Value)
.Cells(irow + i, 4) = ListBox1.List(i, 1) 'ten nhan cung cap
.Cells(irow + i, 5) = ListBox1.List(i, 2) 'ten hang hoa
.Cells(irow + i, 6) = ListBox1.List(i, 3) 'don vi tinh
.Cells(irow + i, 7) = ListBox1.List(i, 4) 'so luong
.Cells(irow + i, 8) = ListBox1.List(i, 5) 'ten theo KT
Next
.Cells(irow, 7).Resize(i).NumberFormat = "#,##0.00"
.Cells(irow, 1).Resize(i, 8).Borders.LineStyle = 1
.Cells(irow, 1).Resize(i, 8).Borders.ThemeColor = 5
.Range("B4:H4").Resize(i).Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
End With
ngay1 = "": spn1 = "": cb_NCC1 = "": cb_thh1 = "": dvt1 = "": sln1 = ""
ListBox1.Clear
Application.ScreenUpdating = True
ngay1.SetFocus
End Sub
Private Sub Luu1_Click()
Dim i As Integer, irow As Long
If ngay1 = "" Or spn1 = "" Then MsgBox ("Ban chua nhap ngay hoac so phieu nhap"), vbExclamation, "GPE": Exit Sub
If ListBox1.ListCount = 0 Then MsgBox ("ban chua cap nhat Noi dung vao Listbox"), vbExclamation, "GPE": Exit Sub
Application.ScreenUpdating = False 'giup code chay. nhanh hon
With Sheet5
irow = .Range("E65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu
For i = 0 To ListBox1.ListCount - 1
.Cells(irow + i, 1) = irow + i - 3
.Cells(irow + i, 2) = CDate(ngay1.Value)
.Cells(irow + i, 2).NumberFormat = "m/d/yyyy"
.Cells(irow + i, 3) = UCase(spn1.Value)
.Cells(irow + i, 4) = ListBox1.List(i, 1) 'ten nhan cung cap
.Cells(irow + i, 5) = ListBox1.List(i, 2) 'ten hang hoa
.Cells(irow + i, 6) = ListBox1.List(i, 3) 'don vi tinh
.Cells(irow + i, 7) = ListBox1.List(i, 4) 'so luong
.Cells(irow + i, 8) = ListBox1.List(i, 5) 'ten theo KT
Next
.Cells(irow, 7).Resize(i).NumberFormat = "#,##0.00"
.Cells(irow, 1).Resize(i, 8).Borders.LineStyle = 1
.Cells(irow, 1).Resize(i, 8).Borders.ThemeColor = 5
irow = .Range("E65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu
.Range("B4:H"&irow").Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
End With
ngay1 = "": spn1 = "": cb_NCC1 = "": cb_thh1 = "": dvt1 = "": sln1 = ""
ListBox1.Clear
Application.ScreenUpdating = True
ngay1.SetFocus
End Sub
1. Nếu cố tình dùng Range("B4:H4").Resize(...) thìCám ơn Thầy @batman1, nhưng em "đầu to mà óc như trái nho", nên chổ này
"<số dòng có dữ liệu kể từ dòng 4 trước khi thêm dữ liệu từ ListBox> bằng bao nhiêu? Cái này quá dễ."
đối với em là không dễ ạ.
irow = .Range("E65536").End(xlUp).Offset(1).Row
.Range("B4:H4").Resize(irow - 4 + ListBox1.ListCount).Sort ...
irow = .Range("E65536").End(xlUp).Offset(1).Row
.Range("B4:H4").Resize(irow - 4 + ListBox1.ListCount).Sort ...
irow = .Range("E65536").End(xlUp).Offset(1).Row
irow = .Range("E65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu
.Range("B4:H"&irow").Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
End With
.Range("B4:H4").Resize(irow - 4 + ListBox1.ListCount).Sort key1:=.Cells(4, "B"), order1:=xlAscending, _
Header:=xlNo, Orientation:=xlTopToBottom
End With
Bỏ: .Offset(1)Sao em thử code của anh Hiếu không được vậy, nó báo lỗi "1004", mà irow đã khai báo ở trên rồi sao còn khai báo tiếp vậy anh?
Bài đã được tự động gộp:
Code của anh Hiếu thì lỗi
Mã:irow = .Range("E65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu .Range("B4:H"&irow").Sort key1:=.Cells(4, "B"), order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom End With
irow trước FOR là xác định dòng trống đầu tiên (không phải là dòng cuoi cung` chua' du~ lieu như chú thích ghi) trước khi thêm từ ListBox.Sao em thử code của anh Hiếu không được vậy, nó báo lỗi "1004", mà irow đã khai báo ở trên rồi sao còn khai báo tiếp vậy anh?
Code của anh Hiếu thì lỗi
DIỄN ĐÀN GIẢI PHÁP EXCEL Group 1
DIỄN ĐÀN GIẢI PHÁP EXCEL Group 2