Vấn đề VBA tìm kiếm

Liên hệ QC

nhamvantrieu

Thành viên mới
Tham gia
19/4/17
Bài viết
39
Được thích
4
Chào mọi người!
Em không biết về VBA có 1 bạn đã giúp em viết nhưng vẫn chưa dùng được, đó là khi tìm kiếm ở ô S24 bằng hàm Vlookup để ra số tìm kiếm nhưng lệnh không chạy. Nhưng khi điền trực tiếp vào ô S24 thì hàm mới chạy.
Mọi người giúp khi giá trị ô S24 thay đổi theo lệnh tìm kiếm hàm vẫn chạy mà không phải ghi số trực tiếp vào ô S24
1111.JPG
 

File đính kèm

Chào mọi người!
Em không biết về VBA có 1 bạn đã giúp em viết nhưng vẫn chưa dùng được, đó là khi tìm kiếm ở ô S24 bằng hàm Vlookup để ra số tìm kiếm nhưng lệnh không chạy. Nhưng khi điền trực tiếp vào ô S24 thì hàm mới chạy.
Mọi người giúp khi giá trị ô S24 thay đổi theo lệnh tìm kiếm hàm vẫn chạy mà không phải ghi số trực tiếp vào ô S24
View attachment 211136
Thử chỉnh dòng lệnh
If Target.Address = "$S$24" Then
Thành
If Target.Address = "$S$11" Then
 
Upvote 0
Em không biết về ngôn ngữ lập trình VBA. Em đã thử thay theo anh nói nhưng không được
Chạy thử
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$S$11" Then
        Application.EnableEvents = False
        With Range("B26:E35")
            .UnMerge
           .Borders(xlInsideHorizontal).LineStyle = 1
           .WrapText = True
           .Resize(Range("s24").Value).Merge
        End With
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Chạy thử
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$S$11" Then
        Application.EnableEvents = False
        With Range("B26:E35")
            .UnMerge
           .Borders(xlInsideHorizontal).LineStyle = 1
           .WrapText = True
           .Resize(Range("s24").Value).Merge
        End With
        Application.EnableEvents = True
    End If
End Sub
Anh ơi em gõ ô S11 từ 1 tới 10 thì chạy ngon. từ lơn hơn 10 nó báo lỗi anh à
 
Upvote 0
Anh ơi em gõ ô S11 từ 1 tới 10 thì chạy ngon. từ lơn hơn 10 nó báo lỗi anh à
Thêm bẩy lổi
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$S$11" Then
      If IsNumeric(Range("S24")) Then
        Application.EnableEvents = False
        With Range("B26:E35")
            .UnMerge
           .Borders(xlInsideHorizontal).LineStyle = 1
           .WrapText = True
           .Resize(Range("s24").Value).Merge
        End With
        Application.EnableEvents = True
      End If
    End If
End Sub
Lổi là do vùng dữ liệu tham chiếu bị thiếu
=VLOOKUP($S$11,Nguồn!B3:Z11,25,0)
chỉnh Nguồn!B3:Z11 cho đủ dòng
 
Upvote 0
Thêm bẩy lổi
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$S$11" Then
      If IsNumeric(Range("S24")) Then
        Application.EnableEvents = False
        With Range("B26:E35")
            .UnMerge
           .Borders(xlInsideHorizontal).LineStyle = 1
           .WrapText = True
           .Resize(Range("s24").Value).Merge
        End With
        Application.EnableEvents = True
      End If
    End If
End Sub
Lổi là do vùng dữ liệu tham chiếu bị thiếu
=VLOOKUP($S$11,Nguồn!B3:Z11,25,0)
chỉnh Nguồn!B3:Z11 cho đủ dòng
Cám ơn anh rất nhiều
 
Upvote 0
Hoàn chỉnh code cho bạn
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim sArr(), sRow As Long, tVal As Long, i As Long, j As Long
  If Target.Address = "$S$11" Then
    sRow = 0
    tVal = Target.Value
    For j = 1 To ThisWorkbook.Sheets.Count
      If Sheets(j).Name Like "Ngu?n" Then
        With Sheets(j)
          i = .Range("B1040000").End(xlUp).Row
          If i > 2 Then
            sArr = .Range("B3:Z" & i).Value
            For i = 1 To UBound(sArr)
              If sArr(i, 1) = tVal Then
                sRow = sArr(i, 25): Exit For
              End If
            Next i
          End If
        End With
        Exit For
      End If
    Next j
    If sRow > 0 Then
      Range("S24").Value = sRow
      Application.EnableEvents = False
      Application.ScreenUpdating = False
      With Range("B26:E35")
        .UnMerge
        .Borders(xlInsideHorizontal).LineStyle = 1
        .WrapText = True
        .Resize(sRow).Merge
      End With
      Application.ScreenUpdating = True
      Application.EnableEvents = True
    Else
      Range("S24").Value = Empty
      MsgBox "Khong tìm thay du lieu o sheet Nguon"
    End If
  End If
End Sub
 

File đính kèm

Upvote 0
Web KT

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

Back
Top Bottom