code VBA đập dữ liệu vào sheet không đúng.

Liên hệ QC

NguyenthiH

Thành viên mới đăng ký
Tham gia
11/12/16
Bài viết
940
Được thích
171
Giới tính
Nữ
Chào các anh chị!!!
Em có code để nạp dữ liệu vào sheet từ form, mà code chạy không đúng, mong các anh chị giúp dùm:
Em xin giải thích code:
1/ code trong Form Intialize
Mã:
Private Sub UserForm_Initialize()
Dim dw As Long, hwnd As Long, result()
    dw = &H84080080
    hwnd = FindWindow("ThunderDFrame", Me.Caption)
    SetWindowLong hwnd, -16, dw
    Me.Height = Me.Height + 1: Me.Height = Me.Height - 20     
    With Cb_DVNH2
        .List = Sheet1.Range(Sheet1.[AH65536].End(xlUp), Sheet1.[AH4]).Value
    End With
    With Me.ListBox2
        .ColumnCount = 5  ' chổ này em cho listbox chỉ hiện 5 cột, cột thứ 6 ẩn
        .ColumnWidths = "40;170;270;60;60"
     End With 
    With cb_thh2
        .ColumnCount = 1
        .ColumnWidths = "240"
         result = Sheet1.Range(Sheet1.[B65536].End(xlUp), Sheet1.[E4]).Value
        .List = result
    End With  
End Sub
2/code đập vào listbox:
Mã:
Private Sub ghilistbox2()
    If Trim(cb_thh2) = "" Or Trim(slx2) = "" Then
        MsgBox ("Ma so, SL chua day` du?"), vbInformation, "GPE"
        Exit Sub
    End If
    slx2.Value = Evaluate(slx2.Value)
    If slx2 <= 0 Then
        MsgBox ("SL phai > 0"), vbInformation
        Exit Sub
    End If
   
    With ListBox2
        .AddItem .ListCount + 1
        .List(.ListCount - 1, 1) = dh2
        .List(.ListCount - 1, 2) = cb_thh2
        .List(.ListCount - 1, 3) = dvt2
        .List(.ListCount - 1, 4) = Format(slx2, "#,##0.00")
        .List(.ListCount - 1, 5) = cb_thh2.Column(3) ' cột thứ 6 của listbox bằng cột 4 của combobox
        .ListIndex = .ListCount - 1 'chon gia tri cuoi cung
    End With
    cb_thh2 = Empty
    dvt2 = Empty
    slx2 = Empty
    dh2.SetFocus
    With dh2
    .SelStart = 0: .SelLength = Len(.Text)
    End With
End Sub
3/ và code đập dữ liệu vào Sheet:
Mã:
Private Sub Luu2_Click()
Dim i As Integer, irow As Long
    If Trim(ngay2) = "" Or Trim(spx2) = "" Or Trim(Cb_DVNH2) = "" Or Trim(dh2) = "" Then MsgBox ("Ban chua nhap day du"), vbExclamation, "GPE": Exit Sub
    If ListBox2.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 Sheet3
    irow = .Range("B65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu
    For i = 0 To ListBox2.ListCount - 1
        .Cells(irow + i, 1) = irow + i - 3
        .Cells(irow + i, 2) = CDate(ngay2.Value)
        .Cells(irow + i, 2).NumberFormat = "m/d/yyyy"
        .Cells(irow + i, 3) = UCase(spx2.Value)
        .Cells(irow + i, 4) = Cb_DVNH2.Value
       
        .Cells(irow + i, 5) = ListBox2.List(i, 1)           'don hang
        .Cells(irow + i, 6) = ListBox2.List(i, 2)           'ten phu lieu
        .Cells(irow + i, 7) = ListBox2.List(i, 3)           'don vi tinh
        .Cells(irow + i, 8) = ListBox2.List(i, 4)           'so luong
        .Cells(irow + i, 8).NumberFormat = "#,##0.00"
        .Cells(irow + 1, 9) = ListBox2.List(i, 5)  ' đập giá trị cột thứ 6 của lisbox vào cột 9 của sheet.
          Next
        .Cells(irow, 8).Resize(i).NumberFormat = "#,##0.00"
        .Cells(irow, 1).Resize(i, 12).Borders.LineStyle = 1
        .Cells(irow, 1).Resize(i, 12).Borders.ThemeColor = 5
        End With
ngay2 = "": spx2 = "":  Cb_DVNH2 = "":  dh2 = "":  cb_thh2 = ""
dvt2 = "":  slx2 = "":  ListBox2.Clear
Application.ScreenUpdating = True
MsgBox ("da Luu xong"), , "GPE"
ngay2.SetFocus
End Sub
thế nhưng nó cứ đập thiếu ạ, ví dụ em nhập vào ListBox 3 dòng dữ liệu, thì đập cột thứ 6 của ListBox vào sheet chỉ có 2 dòng (dòng đầu tiên không có, chỉ có dòng 2 với dòng 3), các cột từ 1-> 5 của ListBox thì nhập đủ 3 dòng
SttĐơn hàngTên hàng hoaĐvtSL XuấtTên theo kế toán
1PO1245Thẻ Bài BasicPcs2000
2PO1245Dây Treo Thẻ BàiPcs2000Dây treo thẻ bài
3PO1245Thun 2.5Cm ĐenMts2020Thun 25mm
 
Chào các anh chị!!!
Em có code để nạp dữ liệu vào sheet từ form, mà code chạy không đúng, mong các anh chị giúp dùm:
Em xin giải thích code:
1/ code trong Form Intialize
Mã:
Private Sub UserForm_Initialize()
Dim dw As Long, hwnd As Long, result()
    dw = &H84080080
    hwnd = FindWindow("ThunderDFrame", Me.Caption)
    SetWindowLong hwnd, -16, dw
    Me.Height = Me.Height + 1: Me.Height = Me.Height - 20    
    With Cb_DVNH2
        .List = Sheet1.Range(Sheet1.[AH65536].End(xlUp), Sheet1.[AH4]).Value
    End With
    With Me.ListBox2
        .ColumnCount = 5  ' chổ này em cho listbox chỉ hiện 5 cột, cột thứ 6 ẩn
        .ColumnWidths = "40;170;270;60;60"
     End With
    With cb_thh2
        .ColumnCount = 1
        .ColumnWidths = "240"
         result = Sheet1.Range(Sheet1.[B65536].End(xlUp), Sheet1.[E4]).Value
        .List = result
    End With 
End Sub
2/code đập vào listbox:
Mã:
Private Sub ghilistbox2()
    If Trim(cb_thh2) = "" Or Trim(slx2) = "" Then
        MsgBox ("Ma so, SL chua day` du?"), vbInformation, "GPE"
        Exit Sub
    End If
    slx2.Value = Evaluate(slx2.Value)
    If slx2 <= 0 Then
        MsgBox ("SL phai > 0"), vbInformation
        Exit Sub
    End If
  
    With ListBox2
        .AddItem .ListCount + 1
        .List(.ListCount - 1, 1) = dh2
        .List(.ListCount - 1, 2) = cb_thh2
        .List(.ListCount - 1, 3) = dvt2
        .List(.ListCount - 1, 4) = Format(slx2, "#,##0.00")
        .List(.ListCount - 1, 5) = cb_thh2.Column(3) ' cột thứ 6 của listbox bằng cột 4 của combobox
        .ListIndex = .ListCount - 1 'chon gia tri cuoi cung
    End With
    cb_thh2 = Empty
    dvt2 = Empty
    slx2 = Empty
    dh2.SetFocus
    With dh2
    .SelStart = 0: .SelLength = Len(.Text)
    End With
End Sub
3/ và code đập dữ liệu vào Sheet:
Mã:
Private Sub Luu2_Click()
Dim i As Integer, irow As Long
    If Trim(ngay2) = "" Or Trim(spx2) = "" Or Trim(Cb_DVNH2) = "" Or Trim(dh2) = "" Then MsgBox ("Ban chua nhap day du"), vbExclamation, "GPE": Exit Sub
    If ListBox2.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 Sheet3
    irow = .Range("B65536").End(xlUp).Offset(1).Row 'xac dinh row cuoi cung` chua' du~ lieu
    For i = 0 To ListBox2.ListCount - 1
        .Cells(irow + i, 1) = irow + i - 3
        .Cells(irow + i, 2) = CDate(ngay2.Value)
        .Cells(irow + i, 2).NumberFormat = "m/d/yyyy"
        .Cells(irow + i, 3) = UCase(spx2.Value)
        .Cells(irow + i, 4) = Cb_DVNH2.Value
      
        .Cells(irow + i, 5) = ListBox2.List(i, 1)           'don hang
        .Cells(irow + i, 6) = ListBox2.List(i, 2)           'ten phu lieu
        .Cells(irow + i, 7) = ListBox2.List(i, 3)           'don vi tinh
        .Cells(irow + i, 8) = ListBox2.List(i, 4)           'so luong
        .Cells(irow + i, 8).NumberFormat = "#,##0.00"
        .Cells(irow + 1, 9) = ListBox2.List(i, 5)  ' đập giá trị cột thứ 6 của lisbox vào cột 9 của sheet.
          Next
        .Cells(irow, 8).Resize(i).NumberFormat = "#,##0.00"
        .Cells(irow, 1).Resize(i, 12).Borders.LineStyle = 1
        .Cells(irow, 1).Resize(i, 12).Borders.ThemeColor = 5
        End With
ngay2 = "": spx2 = "":  Cb_DVNH2 = "":  dh2 = "":  cb_thh2 = ""
dvt2 = "":  slx2 = "":  ListBox2.Clear
Application.ScreenUpdating = True
MsgBox ("da Luu xong"), , "GPE"
ngay2.SetFocus
End Sub
thế nhưng nó cứ đập thiếu ạ, ví dụ em nhập vào ListBox 3 dòng dữ liệu, thì đập cột thứ 6 của ListBox vào sheet chỉ có 2 dòng (dòng đầu tiên không có, chỉ có dòng 2 với dòng 3), các cột từ 1-> 5 của ListBox thì nhập đủ 3 dòng
SttĐơn hàngTên hàng hoaĐvtSL XuấtTên theo kế toán
1PO1245Thẻ Bài BasicPcs2000
2PO1245Dây Treo Thẻ BàiPcs2000Dây treo thẻ bài
3PO1245Thun 2.5Cm ĐenMts2020Thun 25mm

Gửi cái file đi bạn. Có gì bà con học hỏi tý luôn
 
Upvote 0
à!!!!! Mắt bỏ kiếng ra nhìn chữ i mà tưởng số 1, thế là gõ số 1.
Cám ơn bạn @nguoiconxunui nhiều hen!!!!!
 
Upvote 0
Web KT
Back
Top Bottom