Cần giúp đỡ về Sort dữ liệu trong Combo Box (dữ liệu được import từ file .txt) (1 người xem)

Liên hệ QC

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

hoi_joker

Thành viên mới
Tham gia
14/9/07
Bài viết
41
Được thích
17
Mình có combobox1 trên form được add dữ liệu = code sau
Mã:
Private Sub UserForm_Initialize()
Dim InFile As Integer
InFile = FreeFile
Open ThisWorkbook.Path & "\" & "Doc.txt" For Input As InFile '[COLOR=#ff0000]file doc.txt nằm cùng folder với file excel[/COLOR]
While Not EOF(InFile)
  Line Input #InFile, NextLine
   ComboBox1.AddItem NextLine
Wend
Close InFile
End Sub
* Làm thế nào để sort dữ liệu trong combobox1 theo a, b, c ...? (file Doc.txt không có tiếng việt)
* Làm thế nào để loại bỏ dòng trắng ko có dữ liệu trong combobox mà không phải xoá thủ công trên file doc.txt

Many tks
 

File đính kèm

Lần chỉnh sửa cuối:
Mình có combobox1 trên form được add dữ liệu = code sau
Mã:
Private Sub UserForm_Initialize()
Dim InFile As Integer
InFile = FreeFile
Open ThisWorkbook.Path & "\" & "Doc.txt" For Input As InFile '[COLOR=#ff0000]file doc.txt nằm cùng folder với file excel[/COLOR]
While Not EOF(InFile)
  Line Input #InFile, NextLine
   ComboBox1.AddItem NextLine
Wend
Close InFile
End Sub
* Làm thế nào để sort dữ liệu trong combobox1 theo a, b, c ...? (file Doc.txt không có tiếng việt)
* Làm thế nào để loại bỏ dòng trắng ko có dữ liệu trong combobox mà không phải xoá thủ công trên file doc.txt

Many tks

Thử vầy xem nha:
1> Code trong Module:
Mã:
Function Sort1DArray(ByVal arr, Optional ByVal isText As Boolean = False, Optional ByVal isDESC As Boolean = False)
  Dim sCommand As String
  sCommand = "('" & Join(arr, vbBack) & "').split('" & vbBack & "').sort("
  If isText Then
    sCommand = sCommand & ")"
  Else
    sCommand = sCommand & "function(a,b){return (a-b)})"
  End If
  If isDESC Then sCommand = sCommand & ".reverse()"
  sCommand = sCommand & ".join('" & vbBack & "')"
  With CreateObject("MSScriptControl.ScriptControl")
    .Language = "JavaScript"
    Sort1DArray = Split(.Eval(sCommand), vbBack)
  End With
End Function
2> Code trong UserForm
Mã:
Private Sub UserForm_Initialize()
  Dim arr, tmp As String, txtFile As String
  On Error Resume Next
  txtFile = ThisWorkbook.Path & "\" & "Doc.txt"
  With CreateObject("Scripting.FileSystemObject")
    If .FileExists(txtFile) Then
      With .OpenTextFile(txtFile, 1, , -2)
        tmp = .ReadAll
        .Close
      End With
    End If
  End With
  If Len(tmp) Then
    tmp = WorksheetFunction.Trim(tmp)
    tmp = Replace(tmp, " ", Chr(1))
    tmp = Replace(tmp, vbCrLf, " ")
    tmp = WorksheetFunction.Trim(tmp)
    tmp = Replace(tmp, " ", vbCrLf)
    tmp = Replace(tmp, Chr(1), " ")
    arr = Split(tmp, vbCrLf)
    arr = Sort1DArray(arr, True, False)
    ComboBox1.List() = arr
  End If
End Sub
 

File đính kèm

Upvote 0
Lúc đọc file, thay vì đưa thẳng vào comboBox thì đưa vào ArrayList. (Nếu muốn loại dòng trắng thì chỉ việc xét và tránh không đưa vào)
Sorrt ArrayList (phương thức sort có sẵn)
Đưa trở vào comboBox
 
Upvote 0
Bạn thử dùng Code này xem sao

Mã:
Private Sub UserForm_Initialize()
Dim InFile As Integer, Tmp(), i, j, Tg
ReDim Tmp(0): i = 0
InFile = FreeFile
 Open ThisWorkbook.Path & "\" & "Doc.txt" For Input As InFile
  While Not EOF(InFile)
   Line Input #InFile, nexttip
    If Len(nexttip) > 0 Then
     ReDim Preserve Tmp(i)
      Tmp(i) = nexttip
        i = i + 1
         End If
          Wend
           Close InFile
For i = 0 To UBound(Tmp)
  For j = i + 1 To UBound(Tmp)
    If Tmp(i) > Tmp(j) Then _
      Tg = Tmp(i): Tmp(i) = Tmp(j): Tmp(j) = Tg
        Next j
         Next i
           Me.ComboBox1.List() = Tmp
End Sub
 
Upvote 0
Bạn thử dùng Code này xem sao

Mã:
For i = 0 To UBound(Tmp)
  For j = i + 1 To UBound(Tmp)
    If Tmp(i) > Tmp(j) Then _
      Tg = Tmp(i): Tmp(i) = Tmp(j): Tmp(j) = Tg
        Next j
         Next i
           Me.ComboBox1.List() = Tmp
End Sub
Sort kiểu này xưa rồi anh ơi! Cở 10000 dòng dữ liệu thì sort đến khi nào mới xong?
Cách em sort ở trên cho tốc độ tương đối. Em nhớ anh siwtom còn có 1 hàm sort khác cho tốc độ thuộc dạng vô địch (hình như tên là QuickSort)
 
Upvote 0

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

Back
Top Bottom