Filter cột dữ liệu dạng vừa text vừa số bằng VBA (1 người xem)

Liên hệ QC

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

kobebryant

Thành viên thường trực
Tham gia
7/8/09
Bài viết
248
Được thích
28
Mình có cột dữ liệu dạng:
7301GTA
730HHH-32
730001
730POI-612
730910

Mình dùng đoạn code sau nhưng nó chỉ lọc được dạng số có lẫn text thôi
Rich (BB code):
With Sheets("New")
    .Range("A8:P" & Range("MaKH").Row).AutoFilter Field:=1, Criteria1:="730*"
End With

Kết quả là nó không filter được 730001, 730910 vì đây là dạng số
Mình ko muốn dùng Advanced Filter thì có cách nào ko ạ
 
Giải pháp

Filter ở cột A thì chỉ tạo filter ở cột A thôi, mắc gì mà lọc tới cột P cho đau khộ, nặng nhọc.

PHP:
Sub vidu
Const sCellDK = "A1" 'Nhập điều kiện lọc vào ô A1
Const sColRef = "A"
Const iRowMin = 8  'Dòng tiêu đề của vùng lọc'
Dim strDK as string, lastRow as long, listDK as variant, data as variant, str as String, i as long, ii as long

With Sheets("New")
If .FilterMode = True Then .ShowAllData
lastRow = .Range(sColRef & .Rows.Count).end(xlup).Row
if lastRow <= iRowMin then
msgbox "Khong co du lieu!"
Exit sub
End if
strDK  = .Range(sCellDK).value2
data =  .Range(sColRef & iRowMin +1).Resize(lastRow - iRowMin ).value2
lastRow = ubound(data, 1)
redim listDK(1 to lastRow)
for i=1 to lastRow
str=data(i,1)
if...
Mình có cột dữ liệu dạng:
7301GTA
730HHH-32
730001
730POI-612
730910

Mình dùng đoạn code sau nhưng nó chỉ lọc được dạng số có lẫn text thôi
Rich (BB code):
With Sheets("New")
    .Range("A8:P" & Range("MaKH").Row).AutoFilter Field:=1, Criteria1:="730*"
End With

Kết quả là nó không filter được 730001, 730910 vì đây là dạng số
Mình ko muốn dùng Advanced Filter thì có cách nào ko ạ
Ý bạn muốn dùng cách nào?

Dữ liệu kiểu số không thể dùng ký tự đại diện.

.
 
Upvote 0
vì dữ liệu của mình vừa có thể lẫn text và số, vì 730 là 1 mã kho còn đuôi phía sau là mã hàng. Mà mã hàng thì có thể là text, có thể là số,...
Code vẫn dùng Advanced Filter được, chỉ cần dùng 2 ô làm điều kiện.

Nếu bạn không dùng nó, phải tạo 1 cột phụ hoặc dùng vòng lặp.

.
 
Upvote 0
Thì bạn biến hết chúng thành kiểu 'Text' trước khi xài vòng lặp xem sao!
 
Upvote 0

Filter ở cột A thì chỉ tạo filter ở cột A thôi, mắc gì mà lọc tới cột P cho đau khộ, nặng nhọc.

PHP:
Sub vidu
Const sCellDK = "A1" 'Nhập điều kiện lọc vào ô A1
Const sColRef = "A"
Const iRowMin = 8  'Dòng tiêu đề của vùng lọc'
Dim strDK as string, lastRow as long, listDK as variant, data as variant, str as String, i as long, ii as long

With Sheets("New")
If .FilterMode = True Then .ShowAllData
lastRow = .Range(sColRef & .Rows.Count).end(xlup).Row
if lastRow <= iRowMin then
msgbox "Khong co du lieu!"
Exit sub
End if
strDK  = .Range(sCellDK).value2
data =  .Range(sColRef & iRowMin +1).Resize(lastRow - iRowMin ).value2
lastRow = ubound(data, 1)
redim listDK(1 to lastRow)
for i=1 to lastRow
str=data(i,1)
if vba.instr(1, str, strDK)>0 then
ii=ii+1
listDK(ii)=str
end if
next i
if ii>0 then
redim preserve listDK (1 to ii)

.Range(sColRef & iRowMin ).Resize(lastRow - iRowMin +1).AutoFilter Field:=1, Criteria1:=listDK , Operator:=xlFilterValues
End if
End With
 
Lần chỉnh sửa cuối:
Upvote 0
Giải pháp
Filter ở cột A thì chỉ tạo filter ở cột A thôi, mắc gì mà lọc tới cột P cho đau khộ, nặng nhọc.

PHP:
Sub vidu
Const sCellDK = "A1" 'Nhập điều kiện lọc vào ô A1
Const sColRef = "A"
Const iRowMin = 8  'Dòng tiêu đề của vùng lọc'
Dim strDK as string, lastRow as long, listDK as variant, data as variant, str as String, i as long, ii as long

With Sheets("New")
If .FilterMode = True Then .ShowAllData
lastRow = .Range(sColRef & .Rows.Count).end(xlup).Row
if lastRow <= iRowMin then
msgbox "Khong co du lieu!"
Exit sub
End if
strDK  = .Range(sCellDK).value2
data =  .Range(sColRef & iMinRow+1).Resize(lastRow - iMinRow).value2
lastRow = ubound(data, 1)
redim listDK(1 to lastRow)
for i=1 to lastRow
str=data(i,1)
if vba.instr(1, str, strDK)>0 then
ii=ii+1
listDK(ii)=str
end if
next i
if ii>0 then
redim preserve listDK (1 to ii)

.Range(sColRef & iMinRow).Resize(lastRow - iMinRow+1).AutoFilter Field:=1, Criteria1:=listDK , Operator:=xlFilterValues
End if
End With
Mình bị báo lỗi dòng này
.Range(sColRef & iMinRow).Resize(lastRow - iMinRow+1).AutoFilter Field:=1, Criteria1:=listDK , Operator:=xlFilterValues
 
Upvote 0
Mình mới test thì bị lỗi chỗ này: Khi mình lọc ký tự đầu tiên là 810 thì mã nào có 810 đều được chọn.
Ví dụ: 810GTS712, 810NLV nó lọc thì ok nhưng nó cũng lọc luôn mã như 6203T810
Thay dòng:
if vba.instr(1, str, strDK)>0 then

bằng:
if vba.instr(1, str, strDK) = 1 then
 
Upvote 0
mã hàng thì có thể là text, có thể là số,...
Bạn có thể chuyển mã hàng là số về text, rồi lọc bình thường.

PHP:
Public Sub Loc()
Dim lastRow As Long, i As Long, arr
With Sheets("New")
    If .FilterMode = True Then .ShowAllData
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    If lastRow <= 8 Then Exit Sub
    arr = .Range("A8:A" & lastRow).Value
    For i = 1 To UBound(arr, 1)
        If IsNumeric(arr(i, 1)) Then arr(i, 1) = "'" & arr(i, 1)
    Next i
    With .Range("A8:A" & lastRow)
        .NumberFormat = "@"
        .Value = arr
        .AutoFilter Field:=1, Criteria1:="730*"
    End With
End With
End Sub
 
Upvote 0
Web KT

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

Back
Top Bottom