Hỏi về hiện giá trị từ cột A đến cột U trong VBA

Liên hệ QC

abu123

Thành viên mới
Tham gia
26/7/20
Bài viết
8
Được thích
3
Trong đoạn Code của mình bấm nút tìm kiếm chỉ hiện được giá trị từ 0 tới 5 có nghĩa là từ cột A tới cột F
Mình muốn hiện giá trị từ cột A đến cột U luôn thì mình sửa code lại như thế nào ạ ???
thank các bạn !!!!

Mã:
Private Sub Userform_intialize() '// thiet lap form de tim kiem
Me.txtSearch.SetFocus

End Sub
End Sub




End Sub


Private Sub btnSearch_Click() '// tim kiem du lieu

Me.ListBox1.Clear
Me.ListBox1.AddItem Sheet1.Cells(1, "A")
For B = 2 To 6
Me.ListBox1.List(ListBox1.ListCount - 1, B - 1) = Sheet1.Cells(1, B)
Next B
Me.ListBox1.Selected(0) = True
Dim i As Long
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
For X = 1 To 6
A = Len(Me.txtSearch.Text)
If Left(Sheet1.Cells(i, X).Value, A) = Me.txtSearch.Text And Me.txtSearch.Text <> "" Then
       
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
For c = 1 To 6
Me.ListBox1.List(ListBox1.ListCount - 1, c) = Sheet1.Cells(i, c + 1).Value
Next c
End If
Next X
Next i
End Sub


Private Sub ListBox1_Click()


Me.txtTangca.Text = ListBox1.List(ListBox1.ListIndex, 0)
Me.txtSongayphep.Text = ListBox1.List(ListBox1.ListIndex, 1)
Me.txtTentre.Text = ListBox1.List(ListBox1.ListIndex, 2)
Me.txtThem.Text = ListBox1.List(ListBox1.ListIndex, 3)
Me.txtCam.Text = ListBox1.List(ListBox1.ListIndex, 4)
Me.txtNd.Text = ListBox1.List(ListBox1.ListIndex, 5)




End Sub
 

File đính kèm

  • hoidap.xlsm
    38.9 KB · Đọc: 4
  • KOOOL.png
    KOOOL.png
    309.3 KB · Đọc: 9
Trong đoạn Code của mình bấm nút tìm kiếm chỉ hiện được giá trị từ 0 tới 5 có nghĩa là từ cột A tới cột F
Mình muốn hiện giá trị từ cột A đến cột U luôn thì mình sửa code lại như thế nào ạ ???
thank các bạn !!!!

Mã:
Private Sub Userform_intialize() '// thiet lap form de tim kiem
Me.txtSearch.SetFocus

End Sub
End Sub




End Sub


Private Sub btnSearch_Click() '// tim kiem du lieu

Me.ListBox1.Clear
Me.ListBox1.AddItem Sheet1.Cells(1, "A")
For B = 2 To 6
Me.ListBox1.List(ListBox1.ListCount - 1, B - 1) = Sheet1.Cells(1, B)
Next B
Me.ListBox1.Selected(0) = True
Dim i As Long
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
For X = 1 To 6
A = Len(Me.txtSearch.Text)
If Left(Sheet1.Cells(i, X).Value, A) = Me.txtSearch.Text And Me.txtSearch.Text <> "" Then
      
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
For c = 1 To 6
Me.ListBox1.List(ListBox1.ListCount - 1, c) = Sheet1.Cells(i, c + 1).Value
Next c
End If
Next X
Next i
End Sub


Private Sub ListBox1_Click()


Me.txtTangca.Text = ListBox1.List(ListBox1.ListIndex, 0)
Me.txtSongayphep.Text = ListBox1.List(ListBox1.ListIndex, 1)
Me.txtTentre.Text = ListBox1.List(ListBox1.ListIndex, 2)
Me.txtThem.Text = ListBox1.List(ListBox1.ListIndex, 3)
Me.txtCam.Text = ListBox1.List(ListBox1.ListIndex, 4)
Me.txtNd.Text = ListBox1.List(ListBox1.ListIndex, 5)




End Sub
Bạn viết được code sao lại không sửa được code nhỉ? Thật khó hiểu quá đi.
===\.===\.===\.===\.===\.===\.===\.===\.===\.===\.
 
Upvote 0
.Mình tìm lỗi mãi không ra, chỉ hiện được 6 cột đầu tiên trong excel, bạn giúp mình nhé !! Cảm ơn bạn
Bạn thử cho for chạy tới 8 xem
Mã:
For B = 2 To 8
Và thuộc tính ColumnCount của Listbox bạn sửa thành 8.
Hinh.png
Từ đó ngộ ra vấn đề của mình là gì
 
Upvote 0
Mình cho B chạy tới 20 luôn nhưng vẫn không được bạn ơi
mình muốn lấy giá trị ở cột C, E,F,G,H,Q,R,S nhưng bị lỗi ????

Mã:
Private Sub Userform_intialize() '// thiet lap form de tim kiem
Me.txtSearch.SetFocus

End Sub
End Sub




End Sub


Private Sub btnSearch_Click() '// tim kiem du lieu

Me.ListBox1.Clear
Me.ListBox1.AddItem Sheet1.Cells(1, "A")
For B = 2 To 20
Me.ListBox1.List(ListBox1.ListCount - 1, B - 1) = Sheet1.Cells(2, B)
Next B
Me.ListBox1.Selected(0) = True
Dim i As Long
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
For X = 1 To 6
A = Len(Me.txtSearch.Text)
If Left(Sheet1.Cells(i, X).Value, A) = Me.txtSearch.Text And Me.txtSearch.Text <> "" Then
        
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
For c = 1 To 6
Me.ListBox1.List(ListBox1.ListCount - 1, c) = Sheet1.Cells(i, c + 1).Value
Next c
End If
Next X
Next i
End Sub


Private Sub ListBox1_Click()

Me.txtTentre.Text = ListBox1.List(ListBox1.ListIndex, 2)
Me.txtTangca.Text = ListBox1.List(ListBox1.ListIndex, 5)
Me.txtSongayphep.Text = ListBox1.List(ListBox1.ListIndex, 7)
Me.txtThem.Text = ListBox1.List(ListBox1.ListIndex, 6)
Me.txtCam.Text = ListBox1.List(ListBox1.ListIndex, 4)
Me.txtNd.Text = ListBox1.List(ListBox1.ListIndex, 18)
Me.txtTd.Text = ListBox1.List(ListBox1.ListIndex, 19)
Me.txtY.Text = ListBox1.List(ListBox1.ListIndex, 20)



End Sub

Private Sub txtSearch_Change()

End Sub

Private Sub txtThem_Change()

End Sub


Bạn thử cho for chạy tới 8 xem
Mã:
For B = 2 To 8
Và thuộc tính ColumnCount của Listbox bạn sửa thành 8.
View attachment 241920
Từ đó ngộ ra vấn đề của mình là gì
 

File đính kèm

  • 20.png
    20.png
    15.3 KB · Đọc: 6
  • hoidap.xlsm
    42.1 KB · Đọc: 3
Upvote 0
Kiểu viết Code của chủ bài đăng khó ưa, nên không thể làm gì hơn là tạm biệt.
 
Upvote 0
Mình cho B chạy tới 20 luôn nhưng vẫn không được bạn ơi
mình muốn lấy giá trị ở cột C, E,F,G,H,Q,R,S nhưng bị lỗi ????

Mã:
Private Sub Userform_intialize() '// thiet lap form de tim kiem
Me.txtSearch.SetFocus

End Sub
End Sub




End Sub


Private Sub btnSearch_Click() '// tim kiem du lieu

Me.ListBox1.Clear
Me.ListBox1.AddItem Sheet1.Cells(1, "A")
For B = 2 To 20
Me.ListBox1.List(ListBox1.ListCount - 1, B - 1) = Sheet1.Cells(2, B)
Next B
Me.ListBox1.Selected(0) = True
Dim i As Long
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
For X = 1 To 6
A = Len(Me.txtSearch.Text)
If Left(Sheet1.Cells(i, X).Value, A) = Me.txtSearch.Text And Me.txtSearch.Text <> "" Then
       
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
For c = 1 To 6
Me.ListBox1.List(ListBox1.ListCount - 1, c) = Sheet1.Cells(i, c + 1).Value
Next c
End If
Next X
Next i
End Sub


Private Sub ListBox1_Click()

Me.txtTentre.Text = ListBox1.List(ListBox1.ListIndex, 2)
Me.txtTangca.Text = ListBox1.List(ListBox1.ListIndex, 5)
Me.txtSongayphep.Text = ListBox1.List(ListBox1.ListIndex, 7)
Me.txtThem.Text = ListBox1.List(ListBox1.ListIndex, 6)
Me.txtCam.Text = ListBox1.List(ListBox1.ListIndex, 4)
Me.txtNd.Text = ListBox1.List(ListBox1.ListIndex, 18)
Me.txtTd.Text = ListBox1.List(ListBox1.ListIndex, 19)
Me.txtY.Text = ListBox1.List(ListBox1.ListIndex, 20)



End Sub

Private Sub txtSearch_Change()

End Sub

Private Sub txtThem_Change()

End Sub
Mình không biết có giới hạn gì không? Vụ này chắc nhờ thành viên khác giúp rồi.
 
Upvote 0
Web KT
Back
Top Bottom